1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- # Finding lucas pseudoprimes
- from math import sqrt, gcd
-
- def isqrt(n):
- """ Find the integer square root of n via newton's method, code via
- stackoverflow:
-
- (https://stackoverflow.com/questions/15390807/integer-square-root-in-python)
- """
- x = n
- y = (x + 1) // 2
- while y < x:
- x = y
- y = (x + n // x) // 2
- return x
-
- def hasIntSQRT(n):
- """Detect whether the square root of n is an integer,
- i.e. whether the isqrt(n) is the true square root.
- """
- isq = isqrt(n)
- return isq * isq == n
-
- def Dsequence():
- """Generate sequence 5, -7, 9, -11, 13, -15...
- """
- val = 5
- while True:
- if val % 4 == 1:
- yield val
- else:
- yield -val
- val = val + 2
-
- def Legendere(a, p):
- if (p % 2 == 0):
- raise ValueError("p must be odd, is {}".format(p))
- lv = pow(a, (p-1)//2, p)
- if lv == p - 1:
- lv = -1
- return lv
-
- def Jacobi(a, n):
- if (n % 2 == 0):
- raise ValueError("n must be odd, is {}".format(n))
- a = a % n
- mv = 1
- if (a % 2 == 0):
- a = a // 2
- nm8 = n % 8
- if (nm8 == 3 or nm8 == 5):
- mv = -1
- if n == 1:
- return mv * 1
- if gcd(a, n) != 1:
- return 0
- return mv * Jacobi(n, a)
-
- for n in range(1, 21, 2):
- print("{}:\t{}".format(n, Jacobi(3, n)))
-
-
-
-
-
-
-
|