# Thread: Program produces different results on different computers

1. ## Program produces different results on different computers

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

2. Your guess of different architectures is more or less correct. It looks like computer A is a 32-bit system (or at least it's running a 32-bit OS), so the maximum value that an int can store is around ~2,000,000,000 (~2 billion). Note, the correct answer is 142,913,828,922 (~142 billion), which wont fit in a regular int. You are seeing the effects of the summation "wrapping back around to 0". Computer B seems to be a 64-bit system, so a regular int can hold up to 9,223,372,036,854,775,807 (9 quadrillion), which is enough for the correct answer.

You could do it on computer A if you use C's fixed-width integer type int64_t (make sure you #include <stdint.h>).

If you ever want to make this more extensible, to sum primes beyond 2 million, you will need a big num library, like The GNU MP Bignum Library.

3. 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

As anduril462 says, watch your machine architecture.

4. Thanks for the replies!