19#ifndef FENNEC_MATH_EXT_PRIMES_H
20#define FENNEC_MATH_EXT_PRIMES_H
27template<
typename genType>
28constexpr bool is_prime(genType x) {
30 if (x <= 1)
return false;
31 if (x <= 3)
return true;
32 if (x % 2 == 0 or x % 3 == 0)
return false;
34 if (x < 15)
return true;
43 genType mod15 = x % 15;
44 genType mod15e = mod15 % 2;
45 genType div15e = (x / 15) % 2;
46 if (mod15e == div15e)
return false;
49 genType limit =
sqrt(x);
50 for (genType i = 5; i <= limit; i += 6) {
51 if (x % i == 0 || x % (i + 2) == 0) {
59template<
typename genType>
60constexpr genType next_prime(genType x) {
61 genType n = (x + 1) / 6 + 1;
65 if (fennec::is_prime(x))
return x;
66 if (fennec::is_prime(x += 2))
return x;
70template<
typename genType>
71constexpr genType next_prime2(genType x) {
72 genType n = (x + 1) / 6;
77 if (fennec::is_prime(x))
return x;
78 if (fennec::is_prime(x += 2))
return x;
82template<
typename genType>
83constexpr genType prev_prime(genType x) {
84 genType n = (x + 1) / 6 - 1;
88 if (fennec::is_prime(x))
return x;
89 if (fennec::is_prime(x += 2))
return x;
93template<
typename genType>
94constexpr genType prev_prime2(genType x) {
95 genType n = (x + 1) / 6;
100 if (fennec::is_prime(x))
return x;
101 if (fennec::is_prime(x += 2))
return x;
constexpr genType sqrt(genType x)
Returns .
Definition exponential.h:178