A simple program I wrote in C takes upwards of half an hour to run. I am surprised that C would take so long to run, because from what I can find on the internet C ( aside from C++ or Java ) is one of the faster languages.
// this is a program to find the first triangular number that is divisible by 500 factors int main() { int a; // for triangular num loop int b = 1; // limit for triangular num (1+2+3+......+b) int c; // factor counter int d; // divisor int e = 1; // ends loop long long int t = 0; // triangular number in use while( e != 0 ) { c = 0; // create triangular number t t = t + b; b++; // printf("%lld\n", t); // in case you want to see where it's at // counts factors for( d = 1 ; d != t ; d++ ) { if( t % d == 0 ) { c++; } } // test to see if condition is met if( c > 500 ) { break; } } printf("%lld is the first triangular number with more than 500 factors\n", t); getchar(); return 0; } Granted the program runs through a lot of data, but none of it is ever saved, just tested and passed over.
I am using the Tiny C Compiler on Windows 8.
Is there a reason this runs so slowly? What would be a faster way of achieving the same result?
Thank you!
PRO_TIP:If you don't know the mathematical insight, at least use a Binary Search technique. Best help I can give without spoiling the solution for you.e != 0is unnecessary, although propably optimised away by a clever compiler. Btw: Did you turn compile time optimisation on?