Last active
December 17, 2015 11:59
-
-
Save nephilim/5606333 to your computer and use it in GitHub Desktop.
Project Euler 05: Smallest Multiple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ysl.p05 | |
object SmallestMultple { | |
def discompose(n:Int):List[Int] = n match { | |
case 1 => Nil | |
case _ => { | |
val process = List.range(2,n +1).dropWhile( n % _ != 0) | |
val dividen = process.head | |
dividen :: discompose(n / dividen) | |
} | |
} //> discompose: (n: Int)List[Int] | |
def getSmallestMultiple(number:Int):Int = { | |
import scala.collection.mutable.Map | |
val result:Map[Int,Int] = Map() | |
for(i <- 1 to number) { | |
val distMap = discompose(i).groupBy(identity).mapValues(_.size) | |
distMap.foreach{ case (n,count) => | |
result.get(n) match { | |
case None => result += (n -> count) | |
case Some(orig) => if (orig < count) result(n) = count | |
} | |
}} | |
result.foldLeft(1){ case (total, (n, count)) => scala.math.pow(n, count).toInt} | |
} //> getSmallestMultiple: (number: Int)Int | |
getSmallestMultiple(10) //> res0: Int = 2560 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment