-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_prime.cpp
More file actions
43 lines (30 loc) · 767 Bytes
/
simple_prime.cpp
File metadata and controls
43 lines (30 loc) · 767 Bytes
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
#include <iostream>
bool isprime(int num)
{
if(num <= 1) return false;
if(num <= 3) return true;
if(num % 2 == 0 || num % 3 == 0) return false;
for(int i = 5; i * i <= num; i +=6)
{
if(num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
int main()
{
while(true)
{
int max;
std::cout<<"Range: ";
std::cin>>max;
if(max == 0) break;
int count = 0;
for(int i = 2; i <= max; i++)
{
if(isprime(i)) count++;
}
std::cout<<"\nIn range of 0 - "<<max<<", there are total of "<<count<<" prime numbers.";
std::cout<<"\n\n";
}
return 0;
}