Bladeren bron

FirstD now returns 0 if value can't be found

(Rather than raising an error.)
Petra Lamborn 5 jaren geleden
bovenliggende
commit
193fc36d59
1 gewijzigde bestanden met toevoegingen van 3 en 2 verwijderingen
  1. 3
    2
      lucas.py

+ 3
- 2
lucas.py Bestand weergeven

82
 
82
 
83
 def FirstD(n):
83
 def FirstD(n):
84
     """Return first D in the sequence 5, -7, 9, -11, 13, -15... for which the
84
     """Return first D in the sequence 5, -7, 9, -11, 13, -15... for which the
85
-    Jacobi symbol (D/n) is -1
85
+    Jacobi symbol (D/n) is -1. Returns 0 if the number is a perfect square, or
86
+    otherwise the value can't be found
86
     """
87
     """
87
     if n < 1 or n % 2 == 0:
88
     if n < 1 or n % 2 == 0:
88
         raise ValueError("n must be a positive odd number")
89
         raise ValueError("n must be a positive odd number")
95
         # This is supposed to be faster
96
         # This is supposed to be faster
96
         if D > 30 and not haschecked:
97
         if D > 30 and not haschecked:
97
             if hasIntSQRT(n):
98
             if hasIntSQRT(n):
98
-                raise ValueError("n must not be a square")
99
+                return 0
99
     # Shouldn't fire
100
     # Shouldn't fire
100
     return 0
101
     return 0
101
 
102