C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 7th, 2012, 10:27 AM
pratster pratster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 8 pratster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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; }

Reply With Quote
  #2  
Old September 7th, 2012, 12:12 PM
nebelung's Avatar
nebelung nebelung is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 222 nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 6 Days 20 h 45 m 40 sec
Reputation Power: 234
c Code:
Original - c Code
  1. unsigned long luiSum=2;
  2.  
  3. ....
  4.  
  5. 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:
Original - c Code
  1. %lu

Last edited by nebelung : September 8th, 2012 at 02:55 AM.

Reply With Quote
  #3  
Old September 8th, 2012, 11:25 AM
pratster pratster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 8 pratster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m
Reputation Power: 0
Unhappy Still the same answer!

Quote:
Originally Posted by nebelung
c Code:
Original - c Code
  1. unsigned long luiSum=2;
  2.  
  3. ....
  4.  
  5. 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:
Original - c Code
  1. %lu



I made the change in format specifier. But i'm still getting the same undesired output!!

Reply With Quote
  #4  
Old September 8th, 2012, 11:54 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 20 h 31 m 38 sec
Reputation Power: 1774
> 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

Reply With Quote
  #5  
Old September 8th, 2012, 12:13 PM
nebelung's Avatar
nebelung nebelung is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 222 nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 6 Days 20 h 45 m 40 sec
Reputation Power: 234
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?

Reply With Quote
  #6  
Old September 8th, 2012, 12:21 PM
pratster pratster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 8 pratster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old September 8th, 2012, 12:30 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 20 h 31 m 38 sec
Reputation Power: 1774
> 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.

Reply With Quote
  #8  
Old September 8th, 2012, 12:42 PM
pratster pratster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 8 pratster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #9  
Old September 8th, 2012, 12:54 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 20 h 31 m 38 sec
Reputation Power: 1774
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

Reply With Quote
  #10  
Old September 8th, 2012, 01:01 PM
pratster pratster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 8 pratster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #11  
Old September 8th, 2012, 01:10 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 20 h 31 m 38 sec
Reputation Power: 1774
The FAQ at http://www.mingw.org/ will tell you more.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Sum of Primes below 2000000

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap