TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
check_prime.cpp
Go to the documentation of this file.
1
17
#include <cassert>
18
#include <iostream>
19
24
namespace
math
{
31
bool
is_prime
(int64_t num) {
41
if
(num <= 1) {
42
return
false
;
43
}
else
if
(num == 2 || num == 3) {
44
return
true
;
45
}
else
if
(num % 2 == 0 || num % 3 == 0) {
46
return
false
;
47
}
else
{
48
for
(int64_t i = 5; i * i <= num; i = i + 6) {
49
if
(num % i == 0 || num % (i + 2) == 0) {
50
return
false
;
51
}
52
}
53
}
54
return
true
;
55
}
56
}
// namespace math
57
62
static
void
tests
() {
63
assert(
math::is_prime
(1) ==
false
);
64
assert(
math::is_prime
(2) ==
true
);
65
assert(
math::is_prime
(3) ==
true
);
66
assert(
math::is_prime
(4) ==
false
);
67
assert(
math::is_prime
(-4) ==
false
);
68
assert(
math::is_prime
(7) ==
true
);
69
assert(
math::is_prime
(-7) ==
false
);
70
assert(
math::is_prime
(19) ==
true
);
71
assert(
math::is_prime
(50) ==
false
);
72
assert(
math::is_prime
(115249) ==
true
);
73
74
std::cout <<
"All tests have successfully passed!"
<< std::endl;
75
}
76
81
int
main
() {
82
tests
();
// perform self-tests implementations
83
return
0;
84
}
tests
static void tests()
Self-test implementations.
Definition
check_prime.cpp:62
main
int main()
Main function.
Definition
check_prime.cpp:81
math
for assert
math::is_prime
bool is_prime(int64_t num)
Function to check if the given number is prime or not.
Definition
check_prime.cpp:31
math
check_prime.cpp
Generated by
1.12.0