#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Sum of Primes below 2000000


    Hi All,

    The below code i have written to compute sum of all the primes below 2000000! However the program works fine for smaller numbers. Using CodeBlocks as the IDE with GCC Compiler.

    Went thru alot of debugging, the highest limit 2000000 is also passed and evaluated correctly but when i run the loop, i'm not getting the expected answer but it is showing the output as 1179908154.

    Kindly help me find the problem for the failure folks!


    #include <stdio.h>
    #include <stdlib.h>
    #include<math.h>
    //#define MAX (unsigned long int)100
    int main()
    {
    unsigned int luiLoopCount=0;
    unsigned long luiSum=2;
    for(luiLoopCount=3; luiLoopCount<=2000000 ; luiLoopCount++){

    if(check_prime(luiLoopCount)){
    luiSum+=luiLoopCount;
    }
    }
    printf("\n %u",luiSum);

    return 0;
    }
    int check_prime(unsigned int uiNumber){
    unsigned int luiLoopCount=0;

    if(uiNumber%2==0) return 0;
    long double limit=sqrtl(uiNumber);
    for(luiLoopCount=3;luiLoopCount<=(int)limit;luiLoopCount+=2){
    if(uiNumber%luiLoopCount==0) return 0;
    }
    return 1;
    }


    Code:
    #include <stdio.h> #include <stdlib.h> #include<math.h> //#define MAX (unsigned long int)100 int main() { unsigned int luiLoopCount=0; unsigned long luiSum=2; for(luiLoopCount=3; luiLoopCount<=2000000 ; luiLoopCount++){ if(check_prime(luiLoopCount)){ luiSum+=luiLoopCount; } } printf("\n %u",luiSum); return 0; } int check_prime(unsigned int uiNumber){ unsigned int luiLoopCount=0; if(uiNumber%2==0) return 0; long double limit=sqrtl(uiNumber); for(luiLoopCount=3;luiLoopCount<=(int)limit;luiLoopCount+=2){ if(uiNumber%luiLoopCount==0) return 0; } return 1; }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    222
    Rep Power
    236
    c Code:
    unsigned long luiSum=2;
     
    ....
     
    printf("\n %u",luiSum);


    notice that you defined luiSum variable as unsigned long but when you try to print it's value you;re printing it using the specifier %u suitable for unsigned int, so some kind of truncation is applied to the real value, try printing it using the correct format string, namely:
    c Code:
    %lu
    Last edited by nebelung; September 8th, 2012 at 02:55 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Unhappy Still the same answer!


    Originally Posted by nebelung
    c Code:
    unsigned long luiSum=2;
     
    ....
     
    printf("\n %u",luiSum);


    notice that you defined luiSum variable as unsigned long but when you try to print it's value you;re printing it using the specifier %u suitable for unsigned int, so some kind of truncation is applied to the real value, try printing it using the correct format string, namely:
    c Code:
    %lu

    I made the change in format specifier. But i'm still getting the same undesired output!! :confused:
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    > However the program works fine for smaller numbers.
    So this should be telling you that there could be an overflow in some arithmetic somewhere.

    Do you know what the proper answer should be?
    Does it look like it will fit in a 32-bit number?

    Fixing an overflow, I get the sum as being 142,913,828,922
    Is that right?
    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
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    222
    Rep Power
    236
    1. Please quote only the relevant parts, there's no need to quote the whole post.
    2. Using extra large bold font is bad for your health.

    On a brief look I cannot find anything particulary wrong with your program. What is the "desired" output?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0
    First of all i would like to thank you for your help folks!
    Yeah that's the right answer(142,913,828,922)!! But how did the program give you the answer? I mean where was the cause and fix for an Overflow?

    Could you please mark my mistake. Because i could not get the output on changing the format specifier.
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    > But how did the program give you the answer?
    Because I fixed the overflow.

    > I mean where was the cause and fix for an Overflow?
    Erm, there is only one value which is overflowing, it should be easy enough to spot.

    unsigned short;
    unsigned int;
    unsigned long;
    Can you work out what is next in this progression?

    Think about what a 64-bit type would be.

    > Using CodeBlocks as the IDE with GCC Compiler.
    Is this code::blocks + GCC on windows, or Linux?
    If it's windows, then you'll need a non-standard printf format to print it.
    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
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0
    Going through the next bigger datatype would be unsigned long long(But it also provides me the same answer).

    I'm using COde:Blocks 10.05 rev 6283 on WIndows 7 Home Basic 64-bit.

    I did not understand the last part of it.! Non-Standard printf? How? and Why?
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    unsigned long long is the way to go.

    Unfortunately for windows users, the MinGW port of GCC (which you're using) relies on the Microsoft C runtime libraries
    Hence, you need to use %I64u as the format for printing an unsigned long long
    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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0
    Thank you very much Salem, u solved my problem!!

    %I64u did work rightly. But what was the Microsoft C runtime libraries have to do with the gcc compiler?? How was the dependencies created. Can u explain me the problem in brief or send a link where i can get some insight?? I'm curious to know how this non standard format specifier worked !!
    Sorry aboutt the trouble caused!
    Thank you in this regard too!
  20. #11
  21. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    The FAQ at http://www.mingw.org/ will tell you more.
    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

IMN logo majestic logo threadwatch logo seochat tools logo