{
31bool 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 62static void tests() {
63assert(
math::is_prime(1) ==
false);
64assert(
math::is_prime(2) ==
true);
65assert(
math::is_prime(3) ==
true);
66assert(
math::is_prime(4) ==
false);
67assert(
math::is_prime(-4) ==
false);
68assert(
math::is_prime(7) ==
true);
69assert(
math::is_prime(-7) ==
false);
70assert(
math::is_prime(19) ==
true);
71assert(
math::is_prime(50) ==
false);
72assert(
math::is_prime(115249) ==
true);
73 74std::cout <<
"All tests have successfully passed!"<< std::endl;
75}
76 81int main() {
82 tests();
// perform self-tests implementations 83 return0;
84}
testsstatic void tests()
Self-test implementations.
Definition check_prime.cpp:62 mainint main()
Main function.
Definition check_prime.cpp:81 mathfor assert
math::is_primebool is_prime(int64_t num)
Function to check if the given number is prime or not.
Definition check_prime.cpp:31RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4