Help on Prime Number problem!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 20
Reputation: mrrko is an unknown quantity at this point 
Solved Threads: 0
mrrko mrrko is offline Offline
Newbie Poster

Help on Prime Number problem!!

 
0
  #1
Oct 12th, 2008
I did all the coding, and I think that it is correct, but I dont understand why the program does not output the prime numbers!!!

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4.  
  5. {
  6. double newn;
  7. char ans;
  8.  
  9. do
  10. {
  11. cout << "This is a program that will help you find all the prime numbers between 3 and 100.\n";
  12. cout << "This numbers are: \n";
  13.  
  14. for (int n = 3; n <= 100; n++)
  15. {
  16. for (int i = 2; n < i; ++i)
  17. {
  18. newn = n % i;
  19. while(newn > 0)
  20. {
  21. cout << n << " ";
  22. }
  23. }
  24. }
  25. cout << endl;
  26.  
  27. cout << "Do you want to run this program again? Press 'Y' for yes, or 'N' for no\n";
  28. cin >> ans;
  29.  
  30. } while (ans == 'Y' || ans == 'y');
  31.  
  32. cout << "Have a nice day!\n";
  33.  
  34. return 0;
  35. }

What did I do wrong??? When I compile it and run it, this is what it says:

This is a program that will help you find all the prime numbers between 3 and 100.
This numbers are:

Do you want to run this program again? Press 'Y' for yes, or 'N' for no


And it does not output any number!!! Help PLZ!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Help on Prime Number problem!!

 
0
  #2
Oct 12th, 2008
Hmm something looks fishy here...

  1. for (int n = 3; n <= 100; n++)
  2. {
  3. for (int i = 2; n < i; ++i)
  4. {
  5. newn = n % i;
  6. while(newn > 0)
  7. {
  8. cout << n << " ";
  9. }
  10. }
  11. }

In the above chunk of code, it seems as if the n < i statement evaluates to be false so the loop never executes.

n is always 3 <= n <= 100, and i is initialized to 2 each time the nested for loop is evaluated, but n < i will always return false because of the range of n.

You'll have to modify your nested for loop to accommodate for this logic error. I'm no prime-number professional, but I can at least see the boolean error in your code.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: mrrko is an unknown quantity at this point 
Solved Threads: 0
mrrko mrrko is offline Offline
Newbie Poster

Re: Help on Prime Number problem!!

 
0
  #3
Oct 12th, 2008
Thanks a lot, without a doubt that is my error...

But I tried n > i... and it give me an infinite loop... so now I dont know what to do
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help on Prime Number problem!!

 
0
  #4
Oct 12th, 2008
Originally Posted by mrrko View Post
Thanks a lot, without a doubt that is my error...

But I tried n > i... and it give me an infinite loop... so now I dont know what to do

Well what are you TRYING to do with this loop? Go through all the numbers greater than or equal to n, but less than or equal to 100 or something similar? Go through numbers that are less than n? I don't understand the algorithm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Help on Prime Number problem!!

 
0
  #5
Oct 13th, 2008
Here's a solution, though it is not efficient at all.

  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. using std::ostream;
  7.  
  8. template< unsigned int max >
  9. inline ostream& primeNumbers(ostream& out = cout){
  10.  
  11. for(int i = 2; i <= max; i++){
  12. bool isPrime = true;
  13. for(int j = 2; j <= i; j++){
  14. if( ( (i%j) == 0) && (j != i))
  15. isPrime = false;
  16.  
  17. if((j == i) && (isPrime == true))
  18. out << i << " ";
  19. }
  20. }
  21. return out;
  22. }
  23.  
  24. int main(){
  25.  
  26. primeNumbers<100>() << endl;
  27. cin.ignore();
  28. cin.get();
  29. return 0;
  30. }
Last edited by Alex Edwards; Oct 13th, 2008 at 12:35 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: mrrko is an unknown quantity at this point 
Solved Threads: 0
mrrko mrrko is offline Offline
Newbie Poster

Re: Help on Prime Number problem!!

 
0
  #6
Oct 13th, 2008
Originally Posted by VernonDozier View Post
Well what are you TRYING to do with this loop? Go through all the numbers greater than or equal to n, but less than or equal to 100 or something similar? Go through numbers that are less than n? I don't understand the algorithm.
To prove that the number is a prime number or not, I should divide it by every number from 2 to n-1... so if the number (in that case 3) should be divided by 2... if the number is 4, then it should be divided by 2 and 3... that's why it is used in the loop the "i is less than n"
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: mrrko is an unknown quantity at this point 
Solved Threads: 0
mrrko mrrko is offline Offline
Newbie Poster

Re: Help on Prime Number problem!!

 
0
  #7
Oct 13th, 2008
Originally Posted by Alex Edwards View Post
Here's a solution, though it is not efficient at all.

  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. using std::ostream;
  7.  
  8. template< unsigned int max >
  9. inline ostream& primeNumbers(ostream& out = cout){
  10.  
  11. for(int i = 2; i <= max; i++){
  12. bool isPrime = true;
  13. for(int j = 2; j <= i; j++){
  14. if( ( (i%j) == 0) && (j != i))
  15. isPrime = false;
  16.  
  17. if((j == i) && (isPrime == true))
  18. out << i << " ";
  19. }
  20. }
  21. return out;
  22. }
  23.  
  24. int main(){
  25.  
  26. primeNumbers<100>() << endl;
  27. cin.ignore();
  28. cin.get();
  29. return 0;
  30. }
Thanks men, but that is a little too complicated to what they have showed us in the class
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Help on Prime Number problem!!

 
0
  #8
Oct 13th, 2008
The idea is simple enough...

For numbers 2- max, check the numbers between any one of these numbers...

If any number between the chosen number is a potential divisor of the number (and it isn't 1, nor the number itself) then the chosen number is not a prime number.

If no divisor can be found between 1 and the actual number, then it is a prime number.

I'm sure there is a better way of finding prime numbers than that approach. In fact, any time you have to use 2 for loops to solve a linear problem something is definitely wrong...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 32
Reputation: emotionalone is an unknown quantity at this point 
Solved Threads: 4
emotionalone emotionalone is offline Offline
Light Poster

Re: Help on Prime Number problem!!

 
0
  #9
Oct 13th, 2008
I was able to come up with this
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double newn;
  8. char ans;
  9. do
  10. {
  11. cout << "This is a program that will help you find all the prime numbers between 3 and 100." << endl;
  12. cout << "Theses numbers are: " << endl;
  13. for (int n = 3; n <= 100; n++)
  14. {
  15. newn = 1;
  16. for (int i = n-1; i>=2 && newn ; i--)
  17. newn = n%i;
  18. if ( newn != 0 )
  19. cout << n << " ";
  20. }
  21. cout << endl;
  22. cout << "Do you want to run this program again? Press 'Y' for yes, or 'N' for no\n";
  23. cin >> ans;
  24. }
  25. while (ans == 'Y' || ans == 'y');
  26. cout << "Have a nice day!\n";
  27. return 0;
  28. };

A number is a prime number if there are no exact divisions between that number and 2, excluding the number and 2. So I had newn start with a 1 so it would make the for() condition true from the start. I have newn as a condition in the for because we don't want to do more processes than we need to, if it's an exact division, then it's not a prime number and the loop stops.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 62
Reputation: minigweek is on a distinguished road 
Solved Threads: 4
minigweek's Avatar
minigweek minigweek is offline Offline
Junior Poster in Training

Re: Help on Prime Number problem!!

 
0
  #10
Oct 13th, 2008
Hello,

first a tip.

TO check whether a number 'n' is prime or not, it is just enuff to check whether n is divisible by any integer from 2 to n/2 . no need to loop till n-1.
As you will observe, for say a number 24, obviously no number greater than 12 can divide it. And that is true for all integers.

next, your code snippe ::

  1. for (int n = 3; n <= 100; n++)
  2. {
  3. for (int i = 2; n < i; ++i)
  4. {
  5. newn = n % i;
  6. while(newn > 0)
  7. {
  8. cout << n << " ";
  9. }
  10. }
  11. }

if changed to

  1. int flag=1;
  2. for (int n = 3; n <= 100; n++)
  3. {
  4. for (int i = 2;i<= n/2; ++i)
  5. {
  6. newn = n % i;
  7. if(newn==0)
  8. {
  9. flag=0;
  10. break;
  11. }
  12.  
  13. }
  14. if (flag==1)
  15. {
  16. cout<<" "<<n;
  17. }
  18. flag=1;
  19. }

The changes i made are ::
1. loop i from 2 till n/2
2. variable int flag.
-> set this to 1 at first , if none of the division gives a remainder 0, then it remains 1, meaning the number checked ( i ) is prime. else set it to 0 and break from inner loop.

it shud work.
regards
Last edited by minigweek; Oct 13th, 2008 at 2:53 am.
I was born Genius, but some loser Leeched it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC