A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/db/d93/check__prime_8cpp_source.html below:

TheAlgorithms/C++: math/check_prime.cpp Source File

1 16 17#include <cassert> 18#include <iostream> 19 24namespace math

{

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

() {

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 81int 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

RetroSearch 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