I was doing problem 10 on project euler Project Euler
The following code produces two different answers on different systems:
Code:
#define INPUT 2000000
Bool isPrime(const int number)
{
int i, sqr = sqrt(n);
if(n%2==0) return 0;
for(i=3;i<=sqr; i+=2)
{
if (n%i==0) return 0;
}
return 1;
}
int main()
{
int i;
int sum = 0;
for( i=0; i <= INPUT; ++i )
{
if( isPrime( i ) )
{
sum = sum + i;
}
}
printf( "The sum of prime number less than %d is: %d.\n", INPUT, sum );
}
Computer A:
Linux 3.0.0-30-generic #47-Ubuntu SMP Wed Jan 2 22:39:01 UTC 2013 i686 i686 i386 GNU/Linux
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Ubuntu 11.10
Computer B:
Linux 3.2.0-31-virtual #50-Ubuntu SMP Fri Sep 7 16:36:36 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Ubuntu 12.04
Compiled by: gcc program.c -o run -lm
Computer A generates this answer: 1179908154 (wrong)
Computer B generates this answer: 142913828922 (right)
I was hoping someone could explain why this is. My guess might be because they are running on different architectures. But I really don't know.
Thank you