Thread: Help Me - whats the correct way to priint primes

  1. #1
    superfly
    Guest

    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];

    }



    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    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.
    Best Regards,

    Bonkey

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    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
    .
    .
    .

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401

    <snip>
    a_4 = 43*7*3*2+1 = 1807
    1807 = 13 * 139. No, really, there isn't one.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.

    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    superfly
    Guest
    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

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #10
    superfly
    Guest
    Thank You For your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Messene Primes
    By mrsirpoopsalot in forum C++ Programming
    Replies: 9
    Last Post: 01-31-2009, 10:32 PM
  3. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  4. correct malloc use
    By s_siouris in forum C Programming
    Replies: 10
    Last Post: 05-28-2008, 10:49 PM
  5. primes
    By godfather in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2002, 01:44 PM