Browse Source

Legendere symbol; start Jacobi

Petra Lamborn 5 years ago
parent
commit
8a63faa020
1 changed files with 45 additions and 1 deletions
  1. 45
    1
      lucas.py

+ 45
- 1
lucas.py View File

@@ -1,5 +1,5 @@
1 1
 # Finding lucas pseudoprimes
2
-from math import sqrt
2
+from math import sqrt, gcd
3 3
 
4 4
 def isqrt(n):
5 5
     """ Find the integer square root of n via newton's method, code via
@@ -21,4 +21,48 @@ def hasIntSQRT(n):
21 21
     isq = isqrt(n)
22 22
     return isq * isq == n
23 23
 
24
+def Dsequence():
25
+    """Generate sequence 5, -7, 9, -11, 13, -15...
26
+    """
27
+    val = 5
28
+    while True:
29
+        if val % 4 == 1:
30
+            yield val
31
+        else:
32
+            yield -val
33
+        val = val + 2
34
+
35
+def Legendere(a, p):
36
+    if (p % 2 == 0):
37
+        raise ValueError("p must be odd, is {}".format(p))
38
+    lv =  pow(a, (p-1)//2, p)
39
+    if lv == p - 1:
40
+        lv = -1
41
+    return lv
42
+
43
+def Jacobi(a, n):
44
+    if (n % 2 == 0):
45
+        raise ValueError("n must be odd, is {}".format(n))
46
+    a = a % n
47
+    mv = 1
48
+    if (a % 2 == 0):
49
+        a = a // 2
50
+        nm8 = n % 8
51
+        if (nm8 == 3 or nm8 == 5):
52
+            mv = -1
53
+    if n == 1:
54
+        return mv * 1
55
+    if gcd(a, n) != 1:
56
+        return 0
57
+    return mv * Jacobi(n, a)
58
+
59
+for n in range(1, 21, 2):
60
+    print("{}:\t{}".format(n, Jacobi(3, n)))
61
+
62
+
63
+
64
+
65
+
66
+
67
+
24 68