Data Structures Lerax  v3.0-104-ge031
Opinionated Data Structures & Algorithms
Loading...
Searching...
No Matches
prime.h
Go to the documentation of this file.
1#ifndef PRIME_H
2#define PRIME_H
3
4#include <stdbool.h>
5#include <math.h>
6
17static inline int is_prime(int n) {
18 if (n == 2) {
19 return true;
20 } else if (n <= 1 || n % 2 == 0) {
21 return false;
22 }
23
24 for (int k = 3; k <= floor(sqrt(n)); k += 2) {
25 if (n % k == 0) {
26 return false;
27 }
28 }
29
30 return true;
31}
32
42static inline int next_prime(int q) {
43 int i = q + 1;
44 while (!is_prime(i)) i++;
45 return i;
46}
47
48#endif