1. Martin Klinke
  2. ScalaEuler

Commits

Martin Klinke  committed e8a6338

improved solution

  • Participants
  • Parent commits ea2ebe3
  • Branches default

Comments (0)

Files changed (1)

File src/com/mklinke/euler/Problem33.scala

View file
 
 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.
         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
 
   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
   }
 }