TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
primes_up_to_billion.cpp
Go to the documentation of this file.
1
6
#include <cstring>
7
#include <iostream>
8
10
char
prime
[100000000];
11
13
void
Sieve
(int64_t n) {
14
memset(
prime
,
'1'
,
sizeof
(
prime
));
// intitize '1' to every index
15
prime
[0] =
'0'
;
// 0 is not prime
16
prime
[1] =
'0'
;
// 1 is not prime
17
for
(int64_t p = 2; p * p <= n; p++) {
18
if
(
prime
[p] ==
'1'
) {
19
for
(int64_t i = p * p; i <= n; i += p)
20
prime
[i] =
'0'
;
// set all multiples of p to false
21
}
22
}
23
}
24
26
int
main
() {
27
Sieve
(100000000);
28
int64_t n;
29
std::cin >> n;
// 10006187
30
if
(
prime
[n] ==
'1'
)
31
std::cout <<
"YES\n"
;
32
else
33
std::cout <<
"NO\n"
;
34
35
return
0;
36
}
Sieve
void Sieve(int64_t n)
Definition
primes_up_to_billion.cpp:13
prime
char prime[100000000]
Definition
primes_up_to_billion.cpp:10
main
int main()
Definition
primes_up_to_billion.cpp:26
math
primes_up_to_billion.cpp
Generated by
1.12.0