Project Euler: Problem92
Scala の練習。
object Problem92 { def step(m : Int) : Int = { var result : Int = 0 var n : Int = m while ( n != 0 ) { result += (n % 10) * (n % 10) n /= 10 } return result } def loop(n: Int) : Boolean = { if ( n==1 ) return false else if ( n==89 ) return true loop(step(n)) } def main(args : Array[String]) : Unit = { var c : Int = 1 var counter : Int = 0 while ( c < 10000000 ) { if ( loop(c) ) counter += 1 c += 1 } System.out.println(counter) } }
Scala のほうがだいぶ速い。
scala Problem92 4.49s user 0.08s system 98% cpu 4.636 total