浏览代码

Add docstrings; minor edits

Petra Lamborn 5 年前
父节点
当前提交
8fbe46a452
共有 1 个文件被更改,包括 19 次插入3 次删除
  1. 19
    3
      lucas.py

+ 19
- 3
lucas.py 查看文件

33
         val = val + 2
33
         val = val + 2
34
 
34
 
35
 def Legendre(a, p):
35
 def Legendre(a, p):
36
+    """Function for calculating Legendre symbol.
37
+
38
+    Note that this is only supposed to be defined if p is an odd prime, but we
39
+    don't know in advance if that is true - will therefore only throw and error
40
+    if p is even.
41
+
42
+    Uses original power definition from
43
+    <https://en.wikipedia.org/wiki/Legendre_symbol>
44
+    """
36
     if (p % 2 == 0):
45
     if (p % 2 == 0):
37
         raise ValueError("p must be odd, is {}".format(p))
46
         raise ValueError("p must be odd, is {}".format(p))
38
     lv =  pow(a, (p-1)//2, p)
47
     lv =  pow(a, (p-1)//2, p)
41
     return lv
50
     return lv
42
 
51
 
43
 def Jacobi(a, n):
52
 def Jacobi(a, n):
53
+    """Function for calculating Jacobi symbol.
54
+
55
+    Note that this is only defined for positive odd integers n.
56
+
57
+    Uses algorithm from
58
+    <https://en.wikipedia.org/wiki/Jacobi_symbol#Calculating_the_Jacobi_symbol>
59
+    """
60
+    if n < 1:
61
+        raise ValueError("n must be positive")
44
     if (n % 2 == 0):
62
     if (n % 2 == 0):
45
-        raise ValueError("n must be odd, is {}".format(n))
63
+        raise ValueError("n must be odd")
46
     if a % n != a:
64
     if a % n != a:
47
         return Jacobi(a%n, n)
65
         return Jacobi(a%n, n)
48
     if a != 0 and a % 2 == 0:
66
     if a != 0 and a % 2 == 0:
77
 
95
 
78
 
96
 
79
 
97
 
80
-
81
-