lucas_factorization_method_generalized.sf 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 17 May 2019
  4. # https://github.com/trizen
  5. # A new integer factorization method, using the modular Lucas U sequence.
  6. # It uses the smallest divisor `d` of `p - kronecker(D, n)`, such that `U_d(P,Q) = 0 (mod p)`.
  7. # By selecting a small bound B, we compute `k = lcm(1..B)`, hoping that `k` is a
  8. # multiple of `d`, then `gcd(U_k(P,Q) (mod n), n)` in a non-trivial factor of `n`.
  9. # This method is similar in flavor to Pollard's p-1 and Williams's p+1 methods.
  10. func lucas_factorization(n, B = n.ilog2**2, a = 1, b = 5) {
  11. var L = B.consecutive_lcm
  12. for P in (a .. b) { # P > 0, P < n
  13. var Q = ([1,-1].rand * irand(min(1e7, n-1)))
  14. var D = (P*P - 4*Q)
  15. D || next
  16. var F = lucasUmod(P, Q, L, n)
  17. var g = gcd(F, n)
  18. if (g.is_between(2, n-1)) {
  19. return g
  20. }
  21. }
  22. return 1
  23. }
  24. say lucas_factorization(257221 * 470783, 700); #=> 470783 (p+1 is 700-smooth)
  25. say lucas_factorization(333732865481 * 1632480277613, 3000); #=> 333732865481 (p-1 is 3000-smooth)
  26. say lucas_factorization(1124075136413 * 3556516507813, 4000); #=> 1124075136413 (p+1 is 4000-smooth)
  27. say lucas_factorization(6555457852399 * 7864885571993, 700); #=> 6555457852399 (p-1 is 700-smooth)
  28. say lucas_factorization(7553377229 * 588103349, 800); #=> 7553377229 (p+1 is 800-smooth)
  29. for k in (10..50) {
  30. var n = (random_prime(2**k) * random_prime(2**k))
  31. var B = 8*int(exp(sqrt(log(n) * log(log(n))) / 2))
  32. var p = lucas_factorization(n, B)
  33. if (p.is_prime) {
  34. say "#{n} = #{p} * #{n/p}"
  35. }
  36. }
  37. __END__
  38. 1098947 = 683 * 1609
  39. 3059429 = 1483 * 2063
  40. 6512993 = 821 * 7933
  41. 57606617 = 5431 * 10607
  42. 174907669 = 7937 * 22037
  43. 1759396517 = 46199 * 38083
  44. 2378045827 = 76679 * 31013
  45. 9236684929 = 41491 * 222619
  46. 26180302453 = 108967 * 240259
  47. 17777879953 = 98323 * 180811
  48. 2692088381837 = 1374257 * 1958941
  49. 2335955746093 = 4312961 * 541613
  50. 90707847818641 = 12850811 * 7058531
  51. 472153976836973 = 14571107 * 32403439
  52. 334066509182312441 = 1043357477 * 320184133
  53. 633497402663384509 = 876274397 * 722944097
  54. 9284234147413737557 = 2673658063 * 3472483739
  55. 158495270425670543821 = 14872678039 * 10656807739
  56. 167821505065613669143 = 10577372321 * 15866086583
  57. 211225651750010050357 = 4448313713 * 47484432389
  58. 11854320114182178323269 = 99693495271 * 118907658739
  59. 772323021695922630289 = 54803533259 * 14092577171
  60. 168233019237186593148037 = 487441215403 * 345134990479
  61. 49758229648443541997314349 = 7016837554687 * 7091261449427
  62. 6384031017948052159207031 = 2300064692551 * 2775587590481