-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextPrimeNumber.cpp
58 lines (46 loc) · 1.02 KB
/
nextPrimeNumber.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Author: Joshua Vaughan
Simple program that computes prime numbers until user
indicates termination.
*/
#include <iostream>
using namespace std;
bool isPrime(int number)
{
if (number == 2 || number == 3)
return true;
if (number % 2 == 0 || number % 3 == 0)
return false;
int divisor = 6;
while (divisor * divisor - 2 * divisor + 1 <= number)
{
if (number % (divisor - 1) == 0)
return false;
if (number % (divisor + 1) == 0)
return false;
divisor += 6;
}
return true;
}
int nextPrime(int p)
{
while (!isPrime(++p))
{}
return p;
}
int main()
{
bool continueLoop = true;
int prime = 0;
char input;
while (continueLoop)
{
prime = nextPrime(prime);
cout << prime << " is a prime number." << endl;
cout << "Would you like to find another prime number? (Y/N) ";
cin >> input;
if (input == 'n' || input == 'N')
continueLoop = false;
}
return 0;
}