| | |
Help on Prime Number problem!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
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!!!
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!!
cpp Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main() { double newn; char ans; do { cout << "This is a program that will help you find all the prime numbers between 3 and 100.\n"; cout << "This numbers are: \n"; for (int n = 3; n <= 100; n++) { for (int i = 2; n < i; ++i) { newn = n % i; while(newn > 0) { cout << n << " "; } } } cout << endl; cout << "Do you want to run this program again? Press 'Y' for yes, or 'N' for no\n"; cin >> ans; } while (ans == 'Y' || ans == 'y'); cout << "Have a nice day!\n"; return 0; }
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!!
Hmm something looks fishy here...
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.
c++ Syntax (Toggle Plain Text)
for (int n = 3; n <= 100; n++) { for (int i = 2; n < i; ++i) { newn = n % i; while(newn > 0) { cout << n << " "; } } }
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.
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
•
•
•
•
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.
Here's a solution, though it is not efficient at all.
c++ Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; using std::endl; using std::ostream; template< unsigned int max > inline ostream& primeNumbers(ostream& out = cout){ for(int i = 2; i <= max; i++){ bool isPrime = true; for(int j = 2; j <= i; j++){ if( ( (i%j) == 0) && (j != i)) isPrime = false; if((j == i) && (isPrime == true)) out << i << " "; } } return out; } int main(){ primeNumbers<100>() << endl; cin.ignore(); cin.get(); return 0; }
Last edited by Alex Edwards; Oct 13th, 2008 at 12:35 am.
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
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"
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
Here's a solution, though it is not efficient at all.
c++ Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; using std::endl; using std::ostream; template< unsigned int max > inline ostream& primeNumbers(ostream& out = cout){ for(int i = 2; i <= max; i++){ bool isPrime = true; for(int j = 2; j <= i; j++){ if( ( (i%j) == 0) && (j != i)) isPrime = false; if((j == i) && (isPrime == true)) out << i << " "; } } return out; } int main(){ primeNumbers<100>() << endl; cin.ignore(); cin.get(); return 0; }
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...
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...
•
•
Join Date: Oct 2008
Posts: 32
Reputation:
Solved Threads: 4
I was able to come up with this
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.
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main() { double newn; char ans; do { cout << "This is a program that will help you find all the prime numbers between 3 and 100." << endl; cout << "Theses numbers are: " << endl; for (int n = 3; n <= 100; n++) { newn = 1; for (int i = n-1; i>=2 && newn ; i--) newn = n%i; if ( newn != 0 ) cout << n << " "; } cout << endl; cout << "Do you want to run this program again? Press 'Y' for yes, or 'N' for no\n"; cin >> ans; } while (ans == 'Y' || ans == 'y'); cout << "Have a nice day!\n"; return 0; };
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.
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 ::
if changed to
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
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 ::
cpp Syntax (Toggle Plain Text)
for (int n = 3; n <= 100; n++) { for (int i = 2; n < i; ++i) { newn = n % i; while(newn > 0) { cout << n << " "; } } }
if changed to
cpp Syntax (Toggle Plain Text)
int flag=1; for (int n = 3; n <= 100; n++) { for (int i = 2;i<= n/2; ++i) { newn = n % i; if(newn==0) { flag=0; break; } } if (flag==1) { cout<<" "<<n; } flag=1; }
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.
![]() |
Similar Threads
- Prime Number (Help Plz!) (VB.NET)
- Urgent: Plz Help me,Prime Number Program Problem (C++)
- Beginner C++ Prime Function Problem (C++)
- find 100 first prime number (C++)
- My prime Number generator (Python)
- Prime Numbers - Help its for college (Visual Basic 4 / 5 / 6)
- prime number problem (Legacy and Other Languages)
- Recursive prime number f(x) (C)
Other Threads in the C++ Forum
- Previous Thread: decimal to hexidecimal!
- Next Thread: Program runs through VC++ but no alone
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






