求个算法。

不知道perl运行要多久,我用C++,很快,大约1秒给出结果,和那位老兄结果一致。

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

long F(unsigned long n)
{
  unsigned long sum = 0;
  for(unsigned long i=1; i<=n; ++i)
    sum += i;
  
  return sum;
}

int CountYueShu(unsigned long sum)
{
  unsigned long sq = sqrt(sum);
  int count = 0;
  
  for(unsigned long i=1; i<sq; ++i)
    if(sum % i == 0)
      count ++;
  
  count = count * 2;
  
  if(sum % sq == 0)
    count ++;
  
  return count;
}

int main(int argc, char *argv[])
{
    unsigned long sum=0;
    unsigned long n=1;
   
    while(1)
    {
      sum = F(n);
      if( CountYueShu(sum) >= 500 )
      {
        cout<< "The n is " <<n <<endl;
        cout<< "The F(n) is " << sum <<endl;
        break;
      }
      ++n;
    }
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
在我的机器上Perl跑了10多秒,而C++是1秒到2秒
my $t = eval { map { my $t = $_; my $i = scalar (grep {$_}(map { $t % $_ ? 0 : 1 } (1 .. $t))); if ($i >= 500) {return $t; }} (map { ($_ + 1) * $_ / 2 } (1 .. 100000))};

map {if ((($_ + 1) * $_ / 2) == $t) {print $_-1; exit;}} (1 .. 100000);

---------------------

玩玩
没运行,只是测试过 50