// 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; }