Problem 56

Problem 56

A googol (10[sup]100[/sup]) is a massive number: one followed by one-hundred zeros; 100[sup]100[/sup] 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[sup]b[/sup], where a, b < 100, what is the maximum digital sum?

#!/usr/bin/env ruby t = (1...100).collect do |a| (1...100).collect { |b| (a**b).to_s.split("").collect { |c| c.to_i }.inject(:+) } end.flatten p t.max

[code=ruby]max = 1
(1…100).each do |a|
(1…100).each do |b|
sum = (a**b).to_s.split(’’).collect(&:to_i).inject(:+)
max = sum if max < sum
end
end

p max[/code]

max = 0 (1..100).each do |a| (1..100).each do |b| w = (a ** b).to_s.split('').map {|x| x.to_i}.inject {|sum,n| sum + n} max = w if w > max end end p max