Done puzzle 64
[project-euler.git] / euler-3.rb
1 #!/usr/bin/ruby -w
2
3 require 'primes.rb'
4
5 if ARGV[0].to_i != 0
6 $TARGET = ARGV[0].to_i
7 else
8 $TARGET = 600851475143
9 end
10
11 primes = Primes.instance
12
13 quotient = $TARGET
14 prime_factors = []
15 prime_index = 0
16
17 while quotient > 1
18 # puts "trying factor #{primes[prime_index]} with quotient #{quotient}"
19 if (quotient % primes[prime_index]) == 0
20 prime_factors << primes[prime_index]
21 quotient = quotient.div primes[prime_index]
22 else
23 prime_index += 1
24 end
25 end
26
27 if __FILE__==$0
28 puts "Prime factors of #{$TARGET} are #{prime_factors.join(', ')}; largest is #{prime_factors.max}"
29 end
30