Parcourir la source

Legendere symbol; start Jacobi

Petra Lamborn il y a 5 ans
Parent
révision
8a63faa020
1 fichiers modifiés avec 45 ajouts et 1 suppressions
  1. 45
    1
      lucas.py

+ 45
- 1
lucas.py Voir le fichier

1
 # Finding lucas pseudoprimes
1
 # Finding lucas pseudoprimes
2
-from math import sqrt
2
+from math import sqrt, gcd
3
 
3
 
4
 def isqrt(n):
4
 def isqrt(n):
5
     """ Find the integer square root of n via newton's method, code via
5
     """ Find the integer square root of n via newton's method, code via
21
     isq = isqrt(n)
21
     isq = isqrt(n)
22
     return isq * isq == n
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