Thread: Program produces different results on different computers

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    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. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Thanks for the replies!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program compiles, runs, but produces no output
    By happyclown in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 05:43 AM
  2. Running program on other computers
    By fighter92 in forum C++ Programming
    Replies: 7
    Last Post: 06-03-2008, 09:09 AM
  3. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  4. C Subroutine produces odd results
    By IGAU in forum C Programming
    Replies: 8
    Last Post: 11-24-2003, 09:47 AM
  5. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM