batch_gcd_factor_2.sf 566 B

1234567891011121314151617181920
  1. #!/usr/bin/ruby
  2. # Factorize a list of numbers for FactorDB, given a main list and several other
  3. # lists of numbers that may have a common factor with the numbers in the main list.
  4. var nums = File(ARGV.shift).open_r.lines.map{.nums.last}.grep { defined(_) }.uniq
  5. var gcds = Math.batch_gcd(
  6. [
  7. nums...,
  8. ARGV.map{ File(_).open_r.lines.map{.nums.last}.grep { defined(_) && (.len\\0 > 25) } }.flat...
  9. ].uniq.sort...
  10. ).uniq.sort
  11. for n in (nums) {
  12. var f = Math.gcd_factors(n, gcds).first(-1)
  13. f || next
  14. say "#{n} = #{f.join(' * ')}"
  15. }