Hi! People here are talking a lot about prime numbers, but i just couldn't find the right answer. I need to write program, that generates numbers from 1 to a (any input number) and then determine, which between these are prime and which are not, And this just doesn't work from the part where prime is calculated.
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
int a;
cin>>a;
for(int b=1; b<=a; b++);
{
bool prime;
for (int c=2; c<=b; c++);
if(b%c==0)
prime=false;
{ cout<<b <<"is not prime"<<endl;}
else if (b%c!==0)
prime=true;
{ cout<<b <<"is prime"<<endl;}
}
}
system ("pause");
return 0;
}
1) learn to modularise your program: routine to check number for prime attribute could be moved to it's own function bool isPrime(int n)
2) Your prime detection algorithm is wrong : first, prime variable is uninitialisated; second, c<=b is a disaster: when c==b c%b is always 0.
3) Identation would help.
4) you need to completely rewrite everything between and including two output statements. Remember: output should be decoupled from prime detection.
Each of these lines ends with a semicolon, which should not be there.
Also, there are mismatched opening and closing braces { }.
In the second loop, I would treat b==1 and b==2 as special cases (1 is not prime, 2 is prime). For the other numbers, the loop condition should be c<b instead of c<=b
#include<iostream>
#include<math.h>
#include<ctime>
using namespace std;
int main ()
{
srand (time(0));
int a;
cout<<"Input the number!"<<endl;
cin>>a;
for(int b=1; b<=a; b++)
{
bool prime;
if ((b==2)||(b==3)){prime=true; cout<<b<<" is prime"<<endl;}
else if ((b%2==0)||(b%3==0)) {prime=false;cout<<b<<" is not prime"<<endl;}
else {prime=true; cout<<b<< is prime"<<endl;}
}
system("pause");
return 0;
}
That does not go far. Is 121 a prime? How about 5?
You should keep a list of the primes you have already found, add each new prime to it, and use that list to determine whether the next value is a prime.
But the method is by it's nature an incomplete solution. It will always fail if you check a few more numbers. In order to succeed with this approach, your program would need to be infinitely long. There are better, more general ways of tackling the problem.
Latest version gives incorrect result for:
1, 5, 7, 121, 143, 169,187...
I tried to create a control number like (int i=2; i<30; i ++) and then check if (b%i==0), but then it checks the number 30 times and each times writes a comment which is wrong each time :(
1) learn to modularise your program: routine to check number for prime attribute could be moved to it's own function
4) ... Remember: output should be decoupled from prime detection.
#include <iostream>
#include <math.h>
usingnamespace std;
bool isPrime(int);
int main()
{
int max;
cout << "Enter max number: ";
cin >> max;
for(int x = 2; x <= max; x++) //1 is technically not a prime number. Don't ask me
{
if(isPrime(x))
cout << x << " is prime\n";
else
cout << x << " is not prime\n";
}
return 0;
}
bool isPrime(int num)
{
if(num <= 1)
returnfalse;
int root = sqrt((double)num);
//start at 2 because all numbers are divisible by 1
for(int x = 2; x <= root; x++) //You only need to check up to and including the root
{
if(num % x == 0)
returnfalse;
}
returntrue;
}
@MiiNiPaa
Did you compile your code? I got about 14 compiler errors.
@GRex2595 Your code looks sensible.
However, in this case rather than starting the loop at line 15 with x=2, you could start from 1, and insert after line 27