batch_gcd_factorization.sf 556 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/ruby
  2. # Find factors of a list of integers, using the batch-GCD algorithm.
  3. # See also:
  4. # https://facthacks.cr.yp.to/batchgcd.html
  5. # Submit the factors to:
  6. # http://factordb.com/search.php
  7. var arr = []
  8. ARGF.each {|line|
  9. if (line.len > 100) {
  10. line = line.words.last.to_i
  11. line || next
  12. arr << line
  13. }
  14. }
  15. zip([arr, Math.batch_gcd(arr...)]).each_2d {|a,b|
  16. next if (b == 1)
  17. next if (a == b)
  18. next if (b.len < 20)
  19. next if (a/b -> len < 20)
  20. assert_eq(a%b, 0)
  21. say "#{a} = #{a/b} * #{b}"
  22. }