This code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long long limit = 2e9;
long long loopLimitI = sqrt(limit);
long long product = 0;
for(long long i = 2; i < loopLimitI; i++){
long long loopLimitJ = limit / i;
for(long long j = 2; j < loopLimitJ; j++){
product = i * j;
}
}
cout << product;
}
takes 12.1 seconds on my 3.4GHz Athlon64 machine (using g++ 4.6.3 in 64-bit mode - it's slower in 32-bit mode, for natural reasons, since all the 64-bit operations take twice as many steps, it takes just short of twice as long). If I remove the cout << product;
line, it takes 0.004s.
If I put the cout
line back in and do -funroll-all-loops
it takes it down to 4.5s.
I don't have a Java environment to run the Java code, but I don't see believe it would run faster than the C++ code.