#!/usr/bin/env ruby
# Solution to Project Euler problem 63
# Author: David J. Oftedal.
# Find the limit beyond which a^b can no longer have b digits.
# 10^b is always b+1 digits long, so the limit is 9^n for some n.
lim = 0
while (9**(lim+1)).to_s.length >= (lim+1)
lim += 1
end
lim = 9**lim
# Count combinations of a^b for which a^b has b digits.
# a is always lower than 10, and a^b is at most equal to lim.
count = 0
(1..9).each do |a|
b = 1
loop do
n = a**b
count += 1 if n.to_s.length == b
# Break after reaching the limit. 1^n is always 1.
break if n > lim || a == 1
b += 1
end
end
puts "There are #{count} numbers for which a^b has b digits."