fork(1) download
  1. # http://e...content-available-to-author-only...s.org/index.php?title=Miller-Rabin_primality_test_(Ruby)&diff=16299&oldid=16298
  2. def modPow(x, r, m)
  3. y = r
  4. z = x
  5. v = 1
  6. while y > 0
  7. u = y % 2
  8. t = y / 2
  9. if u == 1
  10. v = (v * z) % m
  11. end
  12. z = z * z % m
  13. y = t
  14. end
  15. return v
  16. end
  17.  
  18.  
  19. def miller_rabin_pass(a, n)
  20. d = n - 1
  21. s = 0
  22. while d % 2 == 0 do
  23. d >>= 1
  24. s += 1
  25. end
  26.  
  27.  
  28. a_to_power = modPow(a, d, n)
  29. if a_to_power == 1
  30. return true
  31. end
  32. for i in 0...s do
  33. if a_to_power == n - 1
  34. return true
  35. end
  36. a_to_power = (a_to_power * a_to_power) % n
  37. end
  38. return (a_to_power == n - 1)
  39. end
  40.  
  41.  
  42. def miller_rabin(n)
  43. k = 20
  44. for i in 0...k do
  45. a = 0
  46. while a == 0
  47. a = rand(n)
  48. end
  49. if (!miller_rabin_pass(a, n))
  50. return false
  51. end
  52. end
  53. return true
  54. end
  55.  
  56. s = (2 ** 80).to_s
  57. init = s[0, s.size / 2].to_i
  58. n = init
  59. while n > 0
  60. 9.downto(0).each do |i|
  61. s = n.to_s
  62. ss = "#{s}#{i}#{s.reverse}"
  63. if miller_rabin(ss.to_i)
  64. puts ss
  65. exit
  66. end
  67. end
  68. n -= 1
  69. end
Success #stdin #stdout 0.02s 7440KB
stdin
Standard input is empty
stdout
1208925819613169185298021