The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Sum of Primes below 2000000
Discuss Sum of Primes below 2000000 in the C Programming forum on Dev Shed. Sum of Primes below 2000000 C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 7th, 2012, 10:27 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 8
Time spent in forums: 1 h 11 m
Reputation 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; }
|

September 7th, 2012, 12:12 PM
|
 |
Contributing User
|
|
|
|
c Code:
Original
- c Code |
|
|
|
unsigned long luiSum=2; ....
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:
Original
- c Code |
|
|
|
Last edited by nebelung : September 8th, 2012 at 02:55 AM.
|

September 8th, 2012, 11:25 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 8
Time spent in forums: 1 h 11 m
Reputation Power: 0
|
|
Still the same answer!
Quote: | Originally Posted by nebelung
c Code:
Original
- c Code |
|
|
|
unsigned long luiSum=2; ....
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:
Original
- c Code |
|
|
|
|
I made the change in format specifier. But i'm still getting the same undesired output!! 
|

September 8th, 2012, 11:54 AM
|
 |
Contributed User
|
|
|
|
|
> 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?
|

September 8th, 2012, 12:13 PM
|
 |
Contributing User
|
|
|
|
|
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?
|

September 8th, 2012, 12:21 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 8
Time spent in forums: 1 h 11 m
Reputation 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.
|

September 8th, 2012, 12:30 PM
|
 |
Contributed User
|
|
|
|
|
> 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.
|

September 8th, 2012, 12:42 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 8
Time spent in forums: 1 h 11 m
Reputation 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?
|

September 8th, 2012, 12:54 PM
|
 |
Contributed User
|
|
|
|
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
|

September 8th, 2012, 01:01 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 8
Time spent in forums: 1 h 11 m
Reputation 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!
|

September 8th, 2012, 01:10 PM
|
 |
Contributed User
|
|
|
|
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|