Browse Source

Start lucus prime test algo

Petra Lamborn 5 years ago
parent
commit
5215e1afaf
1 changed files with 24 additions and 0 deletions
  1. 24
    0
      lucas.py

+ 24
- 0
lucas.py View File

@@ -0,0 +1,24 @@
1
+# Finding lucas pseudoprimes
2
+from math import sqrt
3
+
4
+def isqrt(n):
5
+    """ Find the integer square root of n via newton's method, code via
6
+    stackoverflow:
7
+
8
+    (https://stackoverflow.com/questions/15390807/integer-square-root-in-python)
9
+    """
10
+    x = n
11
+    y = (x + 1) // 2
12
+    while y < x:
13
+        x = y
14
+        y = (x + n // x) // 2
15
+    return x
16
+
17
+def hasIntSQRT(n):
18
+    """Detect whether the square root of n is an integer,
19
+    i.e. whether the isqrt(n) is the true square root.
20
+    """
21
+    isq = isqrt(n)
22
+    return isq * isq == n
23
+
24
+