English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2006-10-26 00:21:17 · 7 answers · asked by saylee p 1 in Computers & Internet Programming & Design

7 answers

There are two ways you can approach the problem:
1. How to tell if a given number is prime:
bool isPrime(int n)
{
if (n==1)
return true;

int nMaxPossibleDivisor = sqrt(n);
for (int i = 2; i <= nMaxPossibleDivisor; ++i)
{
if (n%i==0)
return false;
}
return true;
}

That's pretty much the best way to determine if a given number is prime.

So, if your problem is to print all primes up to 100 let's say, you could write it as:
main()
{
for (int n= 1; n < 100; ++n)
if (isPrime(n))
display(n);
}

However, there is a more efficient way to solve that problem. It is called the Archimedes Sieve. It is more efficient because it figures out all the primes at once. However, it requires more space.

Basically the code looks like:
main()
{
int n;
bool primes[n];

int i,j;
for (i=1;i primes[i]=true;
for (i=2;i for(j=i;j<(n/i);++j)
primes[i*j]=false;

for (i=1;i if (primes[i])
display(primes[i]);
}

2006-10-26 04:20:32 · answer #1 · answered by Leo Z 2 · 0 1

We'll use a for loop, and store every prime numbers into an array. We'll start the counter from 3, and increment every loop with 2. This is because 2 is the only prime number which is even, so we can skip all even numbers after 2. For every loop, we have an inner loop which rotates through all our stored prime numbers, and we divide the counter with these numbers. If at least one of the numbers is divisible, then we continue with the next number. Else, we push into our prime number array. After the loop has been completed, the array will contain our prime numbers. Here's the code below. int MAX = 1000; int primes[MAX] = [2]; int c, p, x, flag; x=1; for ( c=3; c<=MAX; c += 2 ) { flag=1; for ( p=0; p < x; p++ ) { if ( c % primes[p] == 0 ) { flag=0; break; } } if ( flag == 1 ) { primes[x] = c; x++; } } // display all the prime numbers for ( int i=0; i

2016-05-21 21:51:32 · answer #2 · answered by Anonymous · 0 0

Prime number u can do it easily. I ll give u the logic.
start ur for loop from 2 till n-1 (where n is ur number given for prime number checking) inside that check a condition that whether ur number "n" is divisible by i(ur for loop variable).
Now u ll get the answer.
try the code by urself. It ll help u. Hope u will do...

2006-10-26 06:31:08 · answer #3 · answered by Sudha P 2 · 0 1

//created by nishant
//to find all prime no.s between 0 and 1000 using function
#include
#include
#include
void display(); //function declaration
void main()
{
clrscr();
cout<<"the prime numbers between 10 and 1000 are:"< cout<<"1"<<'\t'<<"2"<<'\t';
display();
getch();
}
void display() //function call
{
int n,x=1000;
for(int i=3;i<=x;i++)
{
for(int k=2;k<=i;k++)
{
n=i%k;
if(k==i)
cout< if(n==0)
k= i+1;
}
}
}


note: you can extend the range by substituting different values for x

2006-10-26 01:39:31 · answer #4 · answered by nfactor26 2 · 0 1

#include
void main()
{
int a,flag=1;
printf("Enter the number");
scanf("%d",&a);
for(int i=2;i { if(a%i==0)
{ flag=1;
break;}
else
flag=0;
}
if(a==2 ||a==1)
flag=0;
if(flag==0)
printf("The number is prime.");
else printf("The no. is not prime");
}

2006-10-26 01:54:47 · answer #5 · answered by priyank_jiet 1 · 0 1

dunno but prime numbers are numbers that only go into themselves and 1. 1 is not a prime number. 2 is the only even prime number.
2,3,5,7,9,11,13,17,19,23,27,29 etc.

2006-10-26 00:25:07 · answer #6 · answered by PeachyPies 3 · 0 1

Look here:

2006-10-26 00:28:06 · answer #7 · answered by Ritz 3 · 1 0

fedest.com, questions and answers