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

lucas.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Legendre(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. if a % n != a:
  41. return Jacobi(a%n, n)
  42. if a != 0 and a % 2 == 0:
  43. nm8 = n % 8
  44. if (nm8 == 3 or nm8 == 5):
  45. return -1 * Jacobi(a//2, n)
  46. else:
  47. return Jacobi(a//2, n)
  48. if a == 1:
  49. return 1
  50. if gcd(a, n) != 1:
  51. return 0
  52. if a == 0 and n == 1:
  53. return 1
  54. if n % 4 == 3 and a % 4 == 3:
  55. return -1 * Jacobi(n, a)
  56. return Jacobi(n, a)
  57. print(" ", end=" ")
  58. for k in range(1, 10):
  59. print("{}".format(k).rjust(2, " "), end=" ")
  60. print()
  61. for n in range(1, 21, 2):
  62. print("{}:".format(n).rjust(3, " "), end=" ")
  63. for k in range(1, 10):
  64. print("{}".format(Jacobi(k, n)).rjust(2, " "), end=" ")
  65. print()