fennec
Loading...
Searching...
No Matches
primes.h
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef FENNEC_MATH_EXT_PRIMES_H
20#define FENNEC_MATH_EXT_PRIMES_H
21
23
24namespace fennec
25{
26
27template<typename genType>
28constexpr bool is_prime(genType x) {
29 // Base Cases Covers
30 if (x <= 1) return false; // Eliminates 0 and 1
31 if (x <= 3) return true; // Eliminates 2 and 3
32 if (x % 2 == 0 or x % 3 == 0) return false; // Eliminates even numbers and numbers divisible by 3
33 // 4, 6, 8, 9, 10, 12, 14, 15
34 if (x < 15) return true; // Covers 5, 7, 11, 13
35
36 // Due to the property of 15 being divisible by 5 and the nature of primes
37 // All prime numbers must end in 1, 3, 7, 9
38 // when x/15 is even, x%15 must be odd
39 // and when x/15 is odd, x%15 must be even
40 // To explain, when x/15 is even, 15*x/15 ends in 0, and when odd, ends in 5
41 // Thus when x/15 is even, 15*x/15 ends in 0, and the results are 1, 3, 7, 11, 13
42 // And when x/15 is even, 15*x/15 ends in 5, and the results are 2, 4, 8, 14
43 genType mod15 = x % 15;
44 genType mod15e = mod15 % 2;
45 genType div15e = (x / 15) % 2;
46 if (mod15e == div15e) return false;
47
48 // Iterate
49 genType limit = sqrt(x);
50 for (genType i = 5; i <= limit; i += 6) {
51 if (x % i == 0 || x % (i + 2) == 0) {
52 return false;
53 }
54 }
55
56 return true;
57}
58
59template<typename genType>
60constexpr genType next_prime(genType x) {
61 genType n = (x + 1) / 6 + 1;
62
63 while (true) {
64 x = (n++ * 6) - 1;
65 if (fennec::is_prime(x)) return x;
66 if (fennec::is_prime(x += 2)) return x;
67 }
68}
69
70template<typename genType>
71constexpr genType next_prime2(genType x) {
72 genType n = (x + 1) / 6;
73 n *= 2;
74
75 while (true) {
76 x = (n++ * 6) - 1;
77 if (fennec::is_prime(x)) return x;
78 if (fennec::is_prime(x += 2)) return x;
79 }
80}
81
82template<typename genType>
83constexpr genType prev_prime(genType x) {
84 genType n = (x + 1) / 6 - 1;
85
86 while (true) {
87 x = (n-- * 6) - 1;
88 if (fennec::is_prime(x)) return x;
89 if (fennec::is_prime(x += 2)) return x;
90 }
91}
92
93template<typename genType>
94constexpr genType prev_prime2(genType x) {
95 genType n = (x + 1) / 6;
96 n /= 2;
97
98 while (true) {
99 x = (n-- * 6) - 1;
100 if (fennec::is_prime(x)) return x;
101 if (fennec::is_prime(x += 2)) return x;
102 }
103}
104
105}
106
107#endif // FENNEC_MATH_EXT_PRIMES_H
Exponential
constexpr genType sqrt(genType x)
Returns .
Definition exponential.h:178