Homepage   Forum     WEB     BBS  
Thread List  
Forum Guide Map  
Thread Content             
   
Finding Prime numbers with C
Author btcdtc   Views 17   Posted at 2008/8/27 05:28  [View In Forum]
Hi,wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
I have another assignment, for my C programming class im having difficulty with.wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
The program is suppose to find the Nth prime number, im suppose to use pointers and functions, but im trying to just get it to work without any of that yet. Below is what I have come up with, I thought it would work, but it doesnt. Once I enter in a number, is doesnt do anything.wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
Can anyone help?wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
#include <stdio.h>wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
int main(void)wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
{wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
int choice;wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
int count=2;wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
printf("PRIME NUMBER CALCULATOR\n");wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
printf("\nChoose a Prime number=>");wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
scanf_s("%d",&choice);wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
while(1){wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
if(choice==1 || choice==2)wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
{printf("The Number is %d",choice);wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
break;}wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
else if(count%2==0 || count%3==0 || count%5==0 || count%7==0 || count==0)wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
continue;wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
elsewtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
count =2;wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
if(count==choice)wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
break;wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
}wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
printf("The number is %d",count);wtsvxiwzhq
wtsvxiwzhq
Hi,wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
What you need to do is have a look in your help files on what the 'continue' statement does. Understanding this will let you know why the program is not doing what you want it to.wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
Try putting some break points in at the if and else statements in your while loop and watch what happens!wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
Cheers,wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
Nick wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
wtsvxiwzhq
Added after 8 minutes:
      

Reply No. Replier Total Replies 9   [View All Replies]  [New Reply] Replied at
1 lassa Hmm I read up on the continue statement, but im still kind of confused. If the remainder is equal to zero that means its not prime, so I thought my continue would just bypass the rest of the loop, and jump back up to the beginning? 2008/8/27 05:28
2 dongyea That's what the continue statement does, it is always returning to the start of the loop as the count is increasing.


Your count starts at 2 and avery loop you add 2 to it so the remainder of your modulus divide is always going to be 0!





edit,: infact, you are jumping to the start o fthe loop before you increment your count, so the count variable remains at 2.
2008/8/27 05:28
3 hgping Ahh I see what you mean. Im trying to fix my algorithm, hopefully it'll work!!








Thanks



Added after 2 hours 5 minutes:
2008/8/27 05:28
4 saigu In the following line, I think you want '==' instead of '=':


if(choice=count)
2008/8/27 05:28
5 zunwang That helped somewhat, but my algorithm is still wrong. For the 7th prime number its giving me 13861, I guess I just dont see it?





Im about ready to throw my computer at the wall!
2008/8/27 05:28
6 wyslnhhh56 Google "eratosthenes sieve" for an algorithm.



Added after 20 minutes:
2008/8/27 05:28
7 anuo2008 Thanks for all of your help!





Only thing is, is that we havent used arrays yet, so I cant use them. And the long identifier, we haven't used that either.





I think im just gonna throw in the towel, ive got the pointers to work, so Il try some more, but not too much!





Thanks again!!!!
2008/8/27 05:28
8 hualeyan Your algorithm is puzzling. It doesn't look much like a prime number algorithm.





Here's a simple brute-force approach (not very efficient). It tests a number for primeness by checking to see if it's evenly divisible by any integer between 2 and the number's square root:


2008/8/27 05:28
9 netlgc And if you are not allowed to use math.h (a library for basic mathematical operations) you can use this verion: 2008/8/27 05:28
 Total Replies 9  Thread Per Page 10
Page 1/1  |<  <<   1   >>  >| 
Power by rfeda