/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# Project Euler - Problem 56 # 7th April 2009 # # A googol (10^(100)) is a massive number: one followed by one-hundred zeros; # 100^(100) is almost unimaginably large: one followed by two-hundred zeros. # Despite their size, the sum of the digits in each number is only 1. # # Considering natural numbers of the form, a^(b), where a, b < 100, what is the maximum digital sum? maxsum = 0 # For all numbers between 90-100 (guesswork) for a in 90..100 for b in 90..100 # Set the sum to 0 sum = 0 # Perform the exponent sum number = a ** b # Split the number into an array of digits digit_array = number.to_s.split('') # Sum the array of digits digit_array.each do |digit| sum += digit.to_i end # If new sum is largest yet set to maximum maxsum = [sum, maxsum].max end end # Display maximum sum puts maxsum