# how do i calculate prime number

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 01-11-2008
pimp
how do i calculate prime number
i'm a beginner using C.. i have a project on creating a souce that can detect prime number.
for example
if the user enter :10
they will print out all the prime nummber within 10
which mean it will print out:7,5,3,1.
can anybody give some tips on doing that ?
any help will be appreciate..
• 01-11-2008
dra
Try looking at this http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

There's a faster algorithm, but I think it's much more complicated
• 01-11-2008
pimp
i'm using Dev-C++
i looking at that link u give but i can't get anything from it..
anyway to help? like code for the program? i have to due the project in 2 days!
pls help
• 01-11-2008
manav
*lol*
Why everyone comes seeking help for completing their homework? :D
• 01-11-2008
pimp
lol.. i'm a beginner..and i'm stuck for 4hrs! and i can't figure out what wrong !
• 01-11-2008
dra
This is how the sieve works:

1. create a table of numbers, starting with 2
2. take the first element (let's call it "i"), and take out all of its multiples (eg. 2+2, 2+2+2, 2+2+2+2)
3. when you're done, whatever i is is a prime number.

then you just repeat the process for whatever number comes after i.
• 01-11-2008
pimp
i understand what it's talking about..
however i can't get it into the programming code.
• 01-11-2008
Quote:

Originally Posted by pimp
lol.. i'm a beginner..and i'm stuck for 4hrs! and i can't figure out what wrong !

THAT we could help with. What are you stuck ON? Post up whatever it is (and use the code tags so it looks good). ([code*] your code here [/code*] Just remove the asterisks.

Look up the "%" operator. That's the "mod" operator, and it's the key operator for a prime number test. If the mod result is zero, then the number being tested, is not prime number.

You know that 1, 2, and 3 are primes, so print them up first, maybe.

Then maybe use a while () or for() loop to test all the other numbers above 3 and see if they're prime or not.

Do you know how to put a loop inside another loop? That could be handy.

So get to it, and post up some code, and tell us whatever you're stuck on.
• 01-11-2008
pimp
how can i attach file?
cause the program itself is not just print prime number but there are more.
source 1
Code:

``` #include<conio.h> #include<stdlib.h> #include<stdio.h>      int main()  {     int minusONE,top,reminder,flag,value;     do     {         for(minusONE = top - 1; minusONE > 0; minusONE--)         {             reminder = top % minusONE;             printf("\n%d / %d reminder is %d", top, minusONE, reminder);             _sleep(200);                 }             }while(minusONE >=3&&flag !=0);     {                     minusONE--;                     flag=value%minusONE;     }        }```
and source 2
Code:

```#include<stdlib.h> #include<conio.h> #include<stdio.h> int main() {     int value,remainder,counter;     int numArray[100],numIndex,flag=0;         printf("pls enter limit");     scanf("%d",&value);         for(counter=1;counter;counter++)     {         remainder = value % (value-counter);                 printf("value of remainder is %d\n",remainder);         printf("value of value is %d\n",value);         printf("value of moduler is %d\n",value-counter);                 _sleep(1000);         printf("value of counter %d\n",counter);         if(remainder ==0)         {             value--;             flag=1;             system("pause");         }         /*else         {             for(numIndex=0;numIndex<=value;numIndex++)             {               numArray[numIndex]=value;             }            }*/                  }             system("pause");     return 0;    }```
i'm stuck with these.. i can't continue writing..
• 01-11-2008
There's no reason to "attach file". Finding a prime number is a very small program.

That code has several errors in it. Junk it, and let's start fresh. :)

Also, it's a lot easier to check your code out, if you start the numbers (both being tested, and the number
doing the testing), at the small end of the scale, not the top end.
• 01-11-2008
pimp
adak can u help me with the code?
any suggestion?
• 01-11-2008
Yeah, sent you a PM, did you read it yet?
• 01-11-2008
pimp
manv and adak thk for the guide.. i will figure it out and post it back..
• 01-11-2008
pimp
i still can't get it work! omg
• 01-11-2008
anon
The problem with private concelling is we have no how far you have got and where you are stuck. It is pointless to simply state that you are stuck.

There are several approaches.

You may use the Sieve. The link has a step-by-step explanation of the algorithm, pseudo-code and even an animation to demonstrate how it works. So you should be able to put at least something together that you can show us.

Or you could simply loop from 2 to input (note that 1 is not a prime) and perform the naive primeness test on each value (trial divisions to determine if it is divisible by any value). This approach is not as efficient but it is simpler to code.
Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last