Prime number program help?

This is a discussion on Prime number program help? within the C++ Programming forums, part of the General Programming Boards category; Hey I was wonderin if someone could help me with my program I am having trouble on how to get ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Question Prime number program help?

    Hey I was wonderin if someone could help me with my program I am having trouble on how to get it to work properly...so this is what I have so far

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        const int numberOfPrimesToFind=10;
    
        int discoveredPrimes[numberOfPrimesToFind];
    
    
    	discoveredPrimes[0]=2;
    	int noOfdiscoveredPrimes=1;
    	
    	int numberUnderTest=3; // the remainder
    
    	bool isDivisible =0;
    	int i;
    	for(i=0;i<noOfdiscoveredPrimes;i++){
    	   isDivisible = !(numberUnderTest%discoveredPrimes[i]);//! is an invert (just the opposite) 
    	   if (isDivisible) break;
    	}
    	if(!isDivisible){
    	    discoveredPrimes[i]=numberUnderTest;
            noOfdiscoveredPrimes++;
    	}
    
    	for (i=0; i<noOfdiscoveredPrimes; i++){
             cout<<"Prime Numberis divisible by "<<i<<"= "<<discoveredPrimes[i]<<endl;
    	}
        
    	
    (i NEED HELP ON THIS PART BELOW) 
    
    while (i<=numberOfPrimesToFind; i++;)) {
            cout<<"Number of Primes"<<i<<" ="<<numberUnderTest<<endl;
            
    
        }      
    
    
    return 0;
    }
    The instrcutions were

    Write a program to find the first 10 prime numbers.
    Hint: Prime numbers are numbers that can not be divided by
    any other prime number other than 1.
    Hint: Once you have determined that a number is prime,
    store it in an array to try it for future tests.

    Can someone help me with the while loop?? to make the numbers prime

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    You might also want to check out the contest forum, we had a prime number generating contest awhile back with some good algorithms.

    As for your program, you need to reinitialize i before the while loop. A better option woudl be to use a for loop instead of that while loop, which looks liek an odd way to simply print out each member of an array.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, I'm not really sure what you were thinking when you wrote your code. There are numerous errors with it, including for-like semicolons inside while loops, if statements outside loops, access to an uninitialized array, etc.

    That hint isn't particularly helpful, either, because you don't really need to store previous primes. I'm assuming it's referring to the use of an algorithm like this: Sieve of Eratosthenes - Wikipedia, the free encyclopedia

    But if you're just trying to find the first ten prime numbers, you can use a nested for loop, the basic structure of which could look like this:
    Code:
    loop until ten prime numbers are found
        loop indefinitely on i from the last checked number
            loop on j from 0 to the square root of i
                if j divides i, this isn't a prime number, so start the i loop again
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    37
    Code:
    int c=0;
    int zr=30; // this is the range for the  primenumber  search
      for(int i=zr;i>0;i--)
    	{
    	c=0; // reset counter for the new number
    	for(int z=1;z<=zr;z++){
    			  if(i%z==0){ // lets look if i % z = 0
    		   c++;
    
    			}
    			if(c<=2&&z==i){
    //if the number can only be devided 2 times  and we are at the end of the number range  we found a 
    //prime number  as u remember the prime can only be devided by 1 and itself
    			printf(" (%d) ",i);
    			break;
    			}
    
    
    	  }
    
    
    	}
    Last edited by punkywow; 10-05-2009 at 01:02 AM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    37
    if u add
    Code:
    if(c>2){ //if c is bigger than 2 we dont need to look at rest of the number it cant be a prime 
    break;
    }
    it makes an performance boost, its good if u calculate primes from high numbers.
    l

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. Largest number program
    By rebel in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2005, 03:20 AM
  3. prime number program code
    By jd7joe in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2005, 09:14 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Replies: 4
    Last Post: 03-09-2002, 12:22 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21