When I tried to compile your code on Ubuntu 10.04, I got a segmentation error when main () is running because you are declaring many megabytes of variables on the stack. I was able to compile it after I moved most of your variables outside of main to make them global.
Then I got the following results:
The obvious approach y = x*x took, 0.000000 seconds The sum of differences approach y1 = y0 + 2x - 1 took, 0.020000 seconds The difference of sums approach y = sum1 - sum0, sum = (x - 1)x(2(x - 1) + 1)/6 took, 0.000000 seconds
The program runs so fast that itโs hard for her to believe that she really did something. I turned on the "-O0" option to disable the optimization, but it is possible that GCC could still optimize all the calculations. Therefore, I tried to add a โvolatileโ qualifier to your arrays, but still got similar results.
Where I stopped working on this. In conclusion, I do not know what is happening with your code, but it is possible that something is wrong.
source share