power_of_2_plus_5_primes_lucas-lehmer.sf 627 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/ruby
  2. # Primality test for primes of the form 2^n + 5.
  3. # First few exponents of such primes, are:
  4. # 1, 3, 5, 11, 47, 53, 141, 143, 191, 273, 341, 16541, 34001, 34763, 42167, ...
  5. # The primality test was derived from the Lucas-Lehmer primality test for Mersenne primes.
  6. # See also:
  7. # https://oeis.org/A059242
  8. func is_pow2_plus5_prime(n) { # defined for n >= 11
  9. var M = (1<<n + 5)
  10. var S = 4
  11. M % 3 == 0 && return false # divisible by 3 and 9
  12. n.times {
  13. S = (powmod(S, 2, M) - 2)
  14. }
  15. S == 194
  16. }
  17. for n in (11..400) {
  18. if (is_pow2_plus5_prime(n)) {
  19. say n
  20. }
  21. }