pocklington-pratt_primality_proving.sf 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 05 January 2020
  4. # https://github.com/trizen
  5. # Prove the primality of a number, using the Pocklington primality test recursively.
  6. # See also:
  7. # https://en.wikipedia.org/wiki/Pocklington_primality_test
  8. # https://en.wikipedia.org/wiki/Primality_certificate
  9. # https://mathworld.wolfram.com/PrattCertificate.html
  10. func pocklington_pratt_primality_test(n, lim=2**64) is cached {
  11. if ((n <= lim) || (n <= 2)) {
  12. return n.is_prime
  13. }
  14. n.is_prob_prime || return false
  15. var d = n-1
  16. var f = d.trial_factor(1e6)
  17. var B = f.pop
  18. if (__FUNC__(B)) {
  19. f << B
  20. B = 1
  21. }
  22. func prove_primality() {
  23. f.uniq.all {|p|
  24. 1..Inf -> any {
  25. var a = irand(2, d)
  26. n.is_strong_pseudoprime(a) || return false
  27. if (is_coprime(powmod(a, d/p, n) - 1, n)) {
  28. say [a, p]
  29. true
  30. }
  31. else {
  32. false
  33. }
  34. }
  35. }
  36. }
  37. loop {
  38. var A = f.prod
  39. if (A>B && is_coprime(A, B)) {
  40. say "\n:: Proving primality of: #{n}"
  41. return prove_primality()
  42. }
  43. var e = B.ecm_factor.grep(__FUNC__)
  44. f += e
  45. B /= e.prod
  46. }
  47. }
  48. say pocklington_pratt_primality_test(115792089237316195423570985008687907853269984665640564039457584007913129603823)
  49. __END__
  50. :: Proving primality of: 1202684276868524221513588244947
  51. [1144035226262155255816851107163, 2]
  52. [276453507044821683122757236393, 3]
  53. [533055736413590411441630243028, 192697]
  54. [772623052663915357147976998514, 5829139]
  55. [1198620610208329295067901446571, 59483944987587859]
  56. :: Proving primality of: 3201964079152361724098258636758155557
  57. [2313084224942214561921893626258798444, 2]
  58. [1693916761993490383400547587005461017, 13]
  59. [2263618819408762859220408720925669846, 51199]
  60. [2103416709313180536403627832549882073, 1202684276868524221513588244947]
  61. :: Proving primality of: 2848630210554880446022254608450222949126931851754251657020267
  62. [2336186877965705181223610367257376615859197600261422793430493, 2]
  63. [1241720120219776264682254065631971571331550672156703004094326, 7]
  64. [18307568000383431510493991654897777639917722928696705126173, 71]
  65. [1489831621568319391708979957772210592834553663557254011479744, 397]
  66. [1824425577446593185257046318868196863269017909587463235878344, 22483]
  67. [807398507928647255183839098063474859547898067727177143487761, 100274029791527]
  68. [2134990251409720802017140664124255058534215247184945285413251, 3201964079152361724098258636758155557]
  69. :: Proving primality of: 57896044618658097711785492504343953926634992332820282019728792003956564801911
  70. [35223411003114110927962766028347501904174744824558491409391205156094574988512, 2]
  71. [43706303540610111065034740159703946431199181276874121569813825440977188904287, 5]
  72. [4140586583236580449118971852137382265992219704057140377213024358453134888045, 19]
  73. [28382269783315586931299470473225360074904358956236329373293103791422688635474, 106969315701167]
  74. [6481591555917417607160527003167957443419742853878784194511937548702782864566, 2848630210554880446022254608450222949126931851754251657020267]
  75. :: Proving primality of: 115792089237316195423570985008687907853269984665640564039457584007913129603823
  76. [87251640571289373727381043576536706375458762721304644901612327104869487452319, 2]
  77. [59269750266327054665432205978695482013726314612623402701790265449453177692176, 57896044618658097711785492504343953926634992332820282019728792003956564801911]
  78. true