您的位置:首页 > 其它

projecteuler problem 12

2009-05-05 15:17 323 查看
// Triangle.cpp : Defines the entry point for the console application.
//
/*
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

*/

#include "stdafx.h"
#include "windows.h"
#include <cmath>
#include <iostream>
using namespace std;

int Divisors(unsigned long num)
{
	int divs = 2;
	long tmp = sqrt((long double)num);
	for(int i = 2; i < tmp; i++)
	{
		if(num % i == 0)
		{
			divs += 2;// double divisors
		}
	}
	if((tmp * tmp) == num)
	{
		divs++;
	}
	return divs;
}
int _tmain(int argc, _TCHAR* argv[])
{
	LARGE_INTEGER EndTime ; 
	LARGE_INTEGER Frequency ; 
	LARGE_INTEGER BegainTime ;
	QueryPerformanceFrequency(&Frequency); 
	QueryPerformanceCounter(&BegainTime) ; 
	unsigned long t = 3;
	int divs = 2, i = 3;
	while(divs <= 500)
	{
		t += i;
		i++;
		divs = Divisors(t);
	}

	cout << t << endl;
	QueryPerformanceCounter(&EndTime) ; 
	cout << ( EndTime.QuadPart - BegainTime.QuadPart )*1000 / Frequency.QuadPart  << endl; 
	system("pause");
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: