Aug
17
In my attempt to learn Ruby out in the open, here’s my solution for Project Euler Problem 48.
I challenged myself to complete the first 50 Euler Problems in Ruby by tomorrow. Having such an easy problem show up this late in the game gave me great hope that I’d meet my goal. Coming up with this solution took no time at all and unfortunately I don’t think I learned anything about Ruby over the 5 or so lines of code.
As always, any feedback is welcome.
# Euler 48
# http://projecteuler.net/index.php?section=problems&id=48
# The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
#
# Find the last ten digits of the series,
# 1^1 + 2^2 + 3^3 + ... + 1000^1000.
timer_start = Time.now
sum = (1..1000).reduce{ |agg, i| agg + i**i}.to_s
puts sum[sum.length - 10, 10]
puts "Elapsed Time: #{(Time.now - timer_start)*1000} milliseconds"