samskivert: Euler 63

21 August 2010

Problem 063: (source):

object Euler063 extends EulerApp {
  def pows (n :Int) = Stream.from(1) prefixLength(p => BigInt(n).pow(p).toString.length == p)
  def answer = 1 to 9 map(pows) sum

The main observation here is that the number of digits of ax, for a ≥ 10, is guaranteed to exceed x. So we can restrict ourselves to looking only at the numbers from 1 to 9. Furthermore, the number of digits of ax, for 1 ≤ a ≤ 9, will equal x up to some maximum x, and then be less than x for all higher x. That enough xs for you?

©1999–2015 Michael Bayne