http://projecteuler.net/problem=12 [Answer:76576500]

本文探讨了利用C++实现计算三角形面积和因子计数的方法,并通过实例展示了其应用。
 

// triangle(n) = n * (n + 1) / 2
// factor((p1^a1)*(p2^a2)*(pm^am))=(a1+1)*(a2+1)*(am+1)
#include <iostream>
#include <vector>

using namespace std;

void InitFactorCount( vector<int>& count, int n )
{
 count.assign( n, 1 );
 vector<int> primes;
 for ( int i = 2; i < n; ++i )
 {
  int tmp = i;
  for ( size_t j = 0; j < primes.size(); ++j )
  {
   int x = 1;
   while ( tmp % primes[j] == 0 )
   {
    tmp /= primes[j];
    ++x;
   }
   count[i] *= x;
  }
  if ( tmp != 1 )
  {
   count[i] *= 2;
   if ( tmp == i)
   {
    primes.push_back( i );
   }
  }
 }
}

int main()
{
 const int N = 13000;
 vector<int> count;
 InitFactorCount( count, N );

 const int M = 500;
 for ( int n = 1; n < N / 2; ++n )
 {
  if ( count[n+n-1] * count[n] > M )
  {
   cout << (n + n - 1) * n << endl;
   break;
  }

  if ( count[n] * count[n+n+1] > M )
  {
   cout << n * (n + n + 1) << endl;
   break;
  }
 }

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值