Created
May 27, 2011 21:56
-
-
Save johana-star/996269 to your computer and use it in GitHub Desktop.
Euler Project Problem #048
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution to Project Euler's forty-eighth problem | |
# http://projecteuler.net/index.php?section=problems&id=48 | |
# Find the last ten digits of the sum of the sequence 1^1, 2^2, 3^3 ... 1000^1000. | |
def generate_array(number=1000) | |
array = (1..number).to_a | |
array.each_with_index do |exp, i| | |
array[i] = exp**exp | |
end | |
return array | |
end | |
def sum_array(array) | |
sum = 0 | |
array.each {|n| sum = sum + n} | |
return sum | |
end | |
array = generate_array | |
sum = sum_array(array) | |
p sum.to_s.slice(-10..-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment