cyclotomic_factorization_method.sf 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 10 February 2020
  4. # https://github.com/trizen
  5. # Find factors of a number of special form, using the cyclotomic
  6. # polynomials C(n, b), for n = 1..floor(1+log_b(n)) and b fixed.
  7. # See also:
  8. # https://en.wikipedia.org/wiki/Cyclotomic_polynomial
  9. func cyclotomic_factorization(n, base=10) {
  10. var lim = n.ilog(base)+1
  11. gather {
  12. for k in (1..lim) {
  13. var t = cyclotomicmod(k, base, n)
  14. var g = gcd(n, t)
  15. g.is_between(2, n-1) || next
  16. while (g.divides(n)) {
  17. n /= g
  18. take([k, g])
  19. }
  20. }
  21. }
  22. }
  23. var n = ((10**258 - 1)/9 - 10**(258/2) - 1)
  24. cyclotomic_factorization(n).each { .say }
  25. __END__
  26. [2, 11]
  27. [2, 11]
  28. [4, 101]
  29. [6, 91]
  30. [8, 10001]
  31. [16, 100000001]
  32. [32, 10000000000000001]
  33. [64, 100000000000000000000000000000001]
  34. [86, 909090909090909090909090909090909090909091]
  35. [128, 10000000000000000000000000000000000000000000000000000000000000001]
  36. [258, 1098901098901098901098901098901098901098900989010989010989010989010989010989010989011]