15Mar/102
Project Euler : Problem 29 in Ruby
I spent some time playing around with a way to reduce calculations by constructing something akin to a sieve, (2^16 is the same as 4^8 and 16^4), but it turns out that the brute force solutions runs in under a second on my machine so it seemed silly to spend any more time with it.
Ruby even minds the big numbers for me, so the solution is quite trivial:
How many distinct terms are in the sequence generated by a^(b) for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
MIN, MAX = 2,100
values = []
(MIN..MAX).each do |base|
(MIN..MAX).each do |power|
values << base**power
end
end
puts values.uniq.length



























June 25th, 2011 - 17:12
I wrote a solution in JavaScript using the JSDB enviroment.
By calculating the logarithm for each element and storing that in an array, then sorting the array and counting the duplicate elements, I got it to run in 59 milliseconds.
June 27th, 2011 - 21:21
Nice!!