# Help Me - whats the correct way to priint primes

Printable View

• 10-23-2002
superfly
Help Me - whats the correct way to priint primes
bool IsPrime( int arr[], int size)

{
bool prime = true;
for( int i = 0; i < size; i++ )
{
for(int j = 2; j < arr[i]-1; j++)
{
if((arr[i] % j ) == 0 )
prime = false;

break;
}
if (prime)

return false;

}

}

void PrintPrime( int arr[], int size)
{
for ( int i = 0 ; i < size; i++ )

if ( IsPrime (arr,size) == false )
{
cout<<arr[i];

}

}
• 10-24-2002
bonkey
There are a few ways to calculate primes.

1) Write a recursive function that tries to devide a number by every number less that it.

2) (2^n)-1

2 to the power of n minus 1 is always a prime number, but you will skip some.
• 10-24-2002
pianorain
Quote:

2 to the power of n minus 1 is always a prime number, but you will skip some.
Try again there, tiger. 2^4 = 16 - 1 = 15. There is no algebraic formula to compute the primes.
• 10-24-2002
*ClownPimp*
how about...

a_0 = 2
a_n = a_(n-1)*a_(n-2)*...*a_0 + 1

ex
a_1 = 2+1 = 3
a_2 = 3*2+1 = 7
a_3 = 7*3*2+1 = 43
a_4 = 43*7*3*2+1 = 1807
.
.
.
• 10-24-2002
pianorain
Quote:

<snip>
a_4 = 43*7*3*2+1 = 1807

1807 = 13 * 139. No, really, there isn't one. ;)
• 10-24-2002
Magos
Quote:

Try again there, tiger. 2^4 = 16 - 1 = 15.
Not that it matters, but your equation isn't correct. You say that 2^4 = 15, which isn't true :).

Anyway, there is no algebraic function to calculate the n:th prime number, but I'm pretty sure there is a function that can calculate a number which is prime (though it will skip a lot of prime numbers when doing so).
I'll reply if I find it.
• 10-24-2002
pianorain
Quote:

Originally posted by Magos
Not that it matters, but your equation isn't correct. You say that 2^4 = 15, which isn't true :).
Aye...I should have split it up into two equations, but I was lazy. :rolleyes:

I really think your search will be fruitless. People have been trying for centuries to find some pattern to the prime numbers. I think the fact that we're still trying says something about human intelligence.
• 10-28-2002
superfly
thanks for you help
I am use the program I originally wrote..

Whats wrong with the code below..its not compiling
Code:

```bool IsPrime( int arr *, int size)   {                 {           bool prime = true;           for( int i = 0; i < size; i++ )         {         for(int j = 2; j < arr[i]-1; j++)         {         if((arr[i] % j ) == 0 )         prime = false;         break;         }         if (prime)                 return false;         } } void PrintPrime( int arr *, int size) {                 {                 for ( int i = 0 ; i < size; i++ )         if ( IsPrime (arr,size) == false )         {         cout<<arr[i];         }         } }```
I the numbers aren't being passed correctly in the array
so when primes print..I see all the numbers..instead of just the primes

&#91;code]&#91;/code]tagged by Salem
• 10-28-2002
swoopy
Your IsPrime() function should look something like this.
Code:

```bool IsPrime(int num) {   for (int j=2; j<=num-1; j++)   {       if (num%j == 0)         return false;   }   return true; }```
And if you want to speed it up, the for-loop need only run to num/2 or even sqrt(num).
• 10-28-2002
superfly
Thank You For your help