aboutsummaryrefslogtreecommitdiff
path: root/gnu/usr.bin/groff/libgroff/prime.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/usr.bin/groff/libgroff/prime.cc')
-rw-r--r--gnu/usr.bin/groff/libgroff/prime.cc28
1 files changed, 28 insertions, 0 deletions
diff --git a/gnu/usr.bin/groff/libgroff/prime.cc b/gnu/usr.bin/groff/libgroff/prime.cc
new file mode 100644
index 000000000000..531856c2a59a
--- /dev/null
+++ b/gnu/usr.bin/groff/libgroff/prime.cc
@@ -0,0 +1,28 @@
+extern "C" {
+#include <math.h>
+}
+
+int is_prime(unsigned n)
+{
+ if (n <= 3)
+ return 1;
+ if (!(n & 1))
+ return 0;
+ if (n % 3 == 0)
+ return 0;
+ unsigned lim = unsigned(sqrt((double)n));
+ unsigned d = 5;
+ for (;;) {
+ if (d > lim)
+ break;
+ if (n % d == 0)
+ return 0;
+ d += 2;
+ if (d > lim)
+ break;
+ if (n % d == 0)
+ return 0;
+ d += 4;
+ }
+ return 1;
+}