PE45

/* Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle T(n) = n(n+1)/2 1, 3, 6, 10, 15, ... Pentagonal P(n) = n(3n-1)/2 1, 5, 12, 22, 35, ... Hexagonal H(n) = n(2n-1) 1, 6, 15, 28, 45, ... It can be verified that T285 = P165 = H143 = 40755. Find the next triangle number that is also pentagonal and hexagonal. */ #include <iostream> #include <windows.h> using namespace std; typedef unsigned long long int _INT64 ; const _INT64 nSIZE = 100000; int main() { __int32 startTime = GetTickCount(); _INT64 lastP = 166; for (_INT64 h = 144; h<nSIZE; h++) { _INT64 HN = h*(2*h-1); for (_INT64 p = lastP; ; p++) { _INT64 PN = p*(3*p-1)/2; if (HN==PN) { cout << HN << '/n'; __int32 endTime = GetTickCount(); cout << "the running time = " << endTime-startTime << " ms."; return 0; } if (PN > HN) { lastP = p; break; } } } return 0; } 

你可能感兴趣的:(PE45)