A simple prime-number bot, in python. WIP
mastodon
python
fediverse
bot
mathematics
prime-numbers

lucas.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Finding lucas pseudoprimes
  2. from math import sqrt, gcd
  3. def isqrt(n):
  4. """ Find the integer square root of n via newton's method, code via
  5. stackoverflow:
  6. (https://stackoverflow.com/questions/15390807/integer-square-root-in-python)
  7. """
  8. x = n
  9. y = (x + 1) // 2
  10. while y < x:
  11. x = y
  12. y = (x + n // x) // 2
  13. return x
  14. def hasIntSQRT(n):
  15. """Detect whether the square root of n is an integer,
  16. i.e. whether the isqrt(n) is the true square root.
  17. """
  18. isq = isqrt(n)
  19. return isq * isq == n
  20. def Dsequence():
  21. """Generate sequence 5, -7, 9, -11, 13, -15...
  22. """
  23. val = 5
  24. while True:
  25. if val % 4 == 1:
  26. yield val
  27. else:
  28. yield -val
  29. val = val + 2
  30. def Legendere(a, p):
  31. if (p % 2 == 0):
  32. raise ValueError("p must be odd, is {}".format(p))
  33. lv = pow(a, (p-1)//2, p)
  34. if lv == p - 1:
  35. lv = -1
  36. return lv
  37. def Jacobi(a, n):
  38. if (n % 2 == 0):
  39. raise ValueError("n must be odd, is {}".format(n))
  40. a = a % n
  41. mv = 1
  42. if (a % 2 == 0):
  43. a = a // 2
  44. nm8 = n % 8
  45. if (nm8 == 3 or nm8 == 5):
  46. mv = -1
  47. if n == 1:
  48. return mv * 1
  49. if gcd(a, n) != 1:
  50. return 0
  51. return mv * Jacobi(n, a)
  52. for n in range(1, 21, 2):
  53. print("{}:\t{}".format(n, Jacobi(3, n)))