Return to Snippet

Revision: 20943
at December 1, 2009 16:03 by theboylatham


Initial Code
# 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

Initial URL

                                

Initial Description

                                

Initial Title
Project Euler - Problem 56

Initial Tags

                                

Initial Language
Ruby