I started out with a little program in python but it took ages to run through it so I switched over to c++. I have no earlier experience with this specific language (coded a lot in c# though) and started out in a web editor: https://www.onlinegdb.com/online_c++_compiler.
My C++ code is:
clock_t start, end;
/* Recording the starting clock tick.*/
start = clock();
int R = 0;
int x = 0;
for (R = 6; R <= 10000; R = R + 2) {
int X_min = ceil(0.5 * sqrt(2) * R);
int N_pairs = 0;
for (x = X_min; x < R; x++) {
float y = sqrt(pow(R, 2) - pow(x, 2));
if (rint(y) == y) {
N_pairs = N_pairs + 1;
}
}
if (N_pairs >= 4) {
//cout << R << ", " << N_pairs;
//cout << "\n";
}
}
end = clock();
//Calculating total time taken by the program.
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
cout << "Time taken by program is : " << time_taken;
cout << " sec " << endl;
//cout << "1" << "|" << "2" << "|" << "3 \n";
//cout << "4" << "|" << "5" << "|" << "6 \n";
//cout << "7" << "|" << "8" << "|" << "9 \n";
It all worked well, however the web editor seems to have a build-in maximum time boundary so at this point I decided to take it over to Visual Studio.
I copy pasted the code and run it:
- the web editor took 0.272273 sec to complete the code
- Visual Studio took 2.446 sec to run it.
I Tried updating VS from the 2017 version to the 2019 one but that had no effect.
Why does it take so much longer for VS to run the code?? and how can I fix it?