944,164 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2840
  • C++ RSS
Feb 6th, 2007
0

prime number generator does nothing.

Expand Post »
First of all, let me exaplain that I only really know python. I thought I would give c++ a try and since then (about two days ago), I have read a few tutorials and have made a guess number game. I thought I would be able to rewrite one of my python programs, a prime number generator into c++, I know I probably did it horribly wrong, but I really do appreciate all help. Heres the code:
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int counter = 3;
  10. double sq = 0;
  11. int i = 0;
  12. int prime = 1;
  13. cout << "2" << endl;
  14.  
  15. for(;;)
  16. {
  17. prime = 1;
  18. sq = (sqrt(counter)) + 1;
  19. for(i = 1; i <= sq; i++)
  20. {
  21. if(counter % 2L)
  22. {
  23. prime = 2;
  24. break;
  25. }
  26. }
  27. if(prime == 1)
  28. {
  29. cout << counter << endl;
  30. counter += 2;
  31. }
  32. else if(prime == 2)
  33. counter += 2;
  34. }
  35. }
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Feb 6th, 2007
0

Re: prime number generator does nothing.

You seem to be making this program needlessly complicated.

Consider doing the following things:
  • Nesting 2 loops inside each other
  • The first loop goes (y) through all the numbers until x
  • The second loop (z) goes from 2 to y-1
  • Inside the second loop, check if y can be divided evenly by z.
  • If false, the number is prime
  • If true, number is not prime; break out of loop
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 6th, 2007
0

Re: prime number generator does nothing.

Ok I got it to work identicle to it's python counterpart. However this one executes about 10 times faster it seems. I'm unsure about most of the stuff you said to consider doing, unsure what you mean by them.

Nesting 2 loops inside each other - how do I do that?

If false, the number is prime - I understand how to set it as false (ex. prime = false, or prime = true) but how do I declare a variable that will be used like that? I use that in the python version but in c++ it said the variable was undeclared and I couldn't figure out what to declare it as.

thanks.

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. double sq = 0.0;
  10. int i2 = 0;
  11. int i = 0;
  12. int prime = 1;
  13. cout << "2" << endl;
  14.  
  15. for(i2 = 3; ;i2 = i2 + 2)
  16. {
  17. prime = 1;
  18. sq = (sqrt(i2));
  19. for(i = 3; i < sq; i = i + 2)
  20. {
  21. if((i2 % i) == 0)
  22. {
  23. prime = 2;
  24. break;
  25. }
  26. }
  27. if(prime == 1)
  28. {
  29. cout << i2 << endl;
  30. }
  31. }
  32. system("PAUSE");
  33. }
Last edited by Matt Tacular; Feb 6th, 2007 at 2:10 am.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Feb 6th, 2007
0

Re: prime number generator does nothing.

A very simple algorithm for your needs:

• Declare variables to hold the prime limit (the number till you want the prime numbers to be generated) and a flag which indicates whether a prime number was found or not (a boolean variable).

• Since while checking for primes we normally say that if a number is divisible by only 2 and itself then its a prime number. We can use a short cut here by considering that if a number is not divisible by any number between 2 and its square root, its a prime number.

• To achieve the above task, we would be needing two loops, one to generate all the numbers and the other for testing the numbers.

• Run the outer loop from 2 to the LIMIT and let the control variable (loop variable) be i.

• Run an inner loop with control variable j, where j starts from 2 and the loop runs till the square of i is less than or equal to j (short cut method)

• Inside the nested loops test whether i is completely divisible by j ( i % j = 0 ) .

• If the above condition is true, then we can conclude that the number is not prime. In this case, set the prime found flag to false and break out of the inner loop.

• After breaking out of the loop test whether we ducked out of the loop due to the condition failure or because of natural loop completion.

• If we broke out of the loop naturally, then the number i under consideration is prime. So print it.

• If we broke out of loop because of condition failure then the number "i" is not prime and so reset the flag.

• At the end of both the loops we would have a list of all the prime numbers.

Here is a sample:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main ()
  4. {
  5. using namespace std ;
  6.  
  7. int limit = 0, prime_count = 0 ;
  8. bool is_prime = true ;
  9.  
  10. cout << "Prime numbers till what? " ;
  11. cin >> limit ;
  12. getchar( ) ;
  13.  
  14. for( int i = 2; i < limit; ++i )
  15. {
  16. for( int j = 2; j * j <= i; ++j )
  17. {
  18. if ( i % j == 0 )
  19. {
  20. is_prime = false ;
  21. break ;
  22. }
  23. }
  24. if( is_prime )
  25. {
  26. ++prime_count ;
  27. cout << i << '\n' ;
  28. }
  29. else
  30. {
  31. is_prime = true ;
  32. }
  33. }
  34.  
  35. cout << "\nTotal of " << prime_count << " prime numbers were found." ;
  36.  
  37. getchar( ) ;
  38. return 0 ;
  39. }
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 6th, 2007
0

Re: prime number generator does nothing.

but what's the point in checking if any even number is a prime? we already know it won't be, it will be divisible by 2. that's why the first loop started at three and went up by twos. and if we aren't checking if even numbers are primes, then why divide by even numbers? which is why the second loop also starts at 3 and goes up by twos. It was just the c++ programming i didn't understand. And I learned alot about it from your post, like how to declare a boolean variable, for which I am very greatful.

And I made it have no limit because I just wanted it to keep going until you exit it.

A also thank you for this
C++ Syntax (Toggle Plain Text)
  1. getchar();
because I would always use
C++ Syntax (Toggle Plain Text)
  1. system("PAUSE");
which would display that stupid "press any key to continue"

C++ Syntax (Toggle Plain Text)
  1. for(int j = 2; j * j <= i; ++j)
That's cool because it's easier than finding the square root first.

Thanks for all your help everyone.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Exceptions
Next Thread in C++ Forum Timeline: Solve a 2x2 linear equation matrix with Cramer's Rule





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC