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

psimp.py 787B

12345678910111213141516171819202122232425262728293031323334
  1. pto100 = [
  2. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
  3. 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
  4. ]
  5. def in_100(x):
  6. """Check if x is in the primes less than 100 i.e., in the first 25 primes.
  7. Useful for e.g. the first step in Baillie–PSW.
  8. """
  9. return x in pto100
  10. def hund_div(x):
  11. """Check if x is divisible by any of the 25 primes less than 100. Returns 0
  12. if there is no such divisor, otherwise returns the first divisor.
  13. """
  14. for v in pto100:
  15. if v >= x:
  16. return 0
  17. if x % v == 0:
  18. return v
  19. return 0
  20. def trd(x):
  21. """Express x as 2^r * d. Returns two values, r and d.
  22. """
  23. count = 0
  24. xc = x
  25. while xc % 2 == 0:
  26. count = count + 1
  27. xc = xc // 2
  28. return count, xc