Prime numbers

So I need to write a program which tells the user if the number they input is prime or not. I have done so and included the code below. My problem is that I need to allow the user to input if they want to enter another number after the first one using y or n and I am having trouble figure it out. Any help would be appreciated. Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

    int number;
	
   
   do
   {
   cout <<"Please enter a positive integer:";
   cin>> number;

if((number%2==0) ||(number%3==0) || (number%4==0)||(number%5==0)||(number%6==0)|| (number%7==0)||(number%8==0)||(number%9==0))
    cout <<"The number "<<number<<" is not a prime number" << endl;
    
else 
    if((number/1==number) && (number/number==1))
    cout << "The number "<<number<<" is a prime number" << endl;

	} 

   while(number>0);
    cout << "Please enter a positive integer" << endl;
    system("PAUSE");
    return (0);
}
put all of your code in a giant while loop and test for if they entered y or n
closed account (2AoiNwbp)
 
(number%9==0)
This test is never reached, since if you test let's say number 81
 
(number%3==0)
will be tested first to true, leaving out the rest of the comparisons.
On the other hand, what about other numbers divisibles by other prime numbers like 121? (divisible by 11)

regards,
Alejandro
Last edited on
Hi,

The main idea of a prime number is that it will divide only with 1 and itself. The key-word is ONLY, as ANY number will divide with 1 and itself. So there isn't any point in doing this:
1
2
3
4
else 
    if((number/1==number) && (number/number==1))
    cout << "The number "<<number<<" is a prime number" << endl;
.

Regarding prime numbers, there also is a rule(axiom) that states that when checking a number if is prime or not, you should check if the number divides to any number at least equal ( <= ) to it's half (the number to be checked / 2 -->> because 2 it's the smallest number that will make your number non-prime).

The simplest way is to create a function like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//header

bool IsPrime(int Number)
{
for(int i = 2; i < Number / 2 + 1; i++) 
    if(Number % i == 0)
             return false;
// as I said, you don't need to check no. > than 1/2 of your no.
//however, if your no. is odd - like 11 - you will need to check 1 no. 
//more after the half of you initial no.-> i < Number / 2 + 1

return true;
}

int main()
{
int number;
cin>> number;
if (IsPrime(Number))
       cout<< "Prime!";
else 
       cout<<"Not prime";
return 0;
}


This is the simplest and rapid way, not the best one.

Best rgds
Last edited on
Only need to go up to the square root instead of half value. Another way using recursion:
1
2
3
4
5
6
7
constexpr bool recursivePrimeCheck (int number, int div) {
	return (div*div > number) ? true : (number % div == 0) ? false : recursivePrimeCheck (number, div+1);
} 

constexpr bool isPrime (int number) {
	return (number <= 1) ? false : recursivePrimeCheck (number, 2);
}


An advantage here is that this is computed at compile time if 'number' is an integral constant, hence the constexpr.
If you already have an array smallPrimes[] of small prime numbers up to that square root, then you could use smallPrimes[i] instead of div, with int i as parameter. That would speed up the calculation.
Last edited on
You should use a prime sieve for finding the primes.
Topic archived. No new replies allowed.