# HG changeset patch # User Martin Klinke # Date 1299170885 -3600 # Node ID e8a6338e8a4596982287ec888bc808344aeeb43c # Parent ea2ebe307aafe5d8102f5fb43884922f9952804e improved solution diff --git a/src/com/mklinke/euler/Problem33.scala b/src/com/mklinke/euler/Problem33.scala --- a/src/com/mklinke/euler/Problem33.scala +++ b/src/com/mklinke/euler/Problem33.scala @@ -31,7 +31,7 @@ object Problem33 { - def check(rat: Rational) = { + def check(rat: Rational): Boolean = { val numerChars = rat.numer.toString().toCharArray() val denomChars = rat.denom.toString().toCharArray() val commonChars = numerChars.filter(char => denomChars. @@ -43,10 +43,7 @@ val source = rat.simplify val target = new Rational(simpleNumerChars(0) - 48, simpleDenomChars(0) - 48).simplify - val equal = source.numer == target.numer && source.denom == target.denom - if (equal) - Console println rat - equal + return source.numer == target.numer && source.denom == target.denom } } false @@ -54,15 +51,13 @@ def main(args: Array[String]) { - var result = (1 to 99).map(num => + val result = (1 to 99).map(num => num to 99 map (denom => new Rational(num, denom)) filter (rational => check(rational))) - val product = result.map(entry => if(entry.length > 0) entry.reduceLeft(_*_) else new Rational(1,1)).reduceLeft(_*_) - - Console println product - - //REMARK: No beautiful solution... how can the list be flattened? - Console print (new Rational(16, 64) * new Rational(19, 95) * new Rational(26, 65) * new Rational(49, 98)).simplify + val filtered = result.filter(_.length > 0) + val mapped = filtered.map(entry => entry.reduceLeft(_ * _)) + val product = mapped.reduceLeft(_ * _).simplify + Console println product } } \ No newline at end of file