Skip to content

Instantly share code, notes, and snippets.

@nephilim
Last active December 17, 2015 11:59
Show Gist options
  • Save nephilim/5606333 to your computer and use it in GitHub Desktop.
Save nephilim/5606333 to your computer and use it in GitHub Desktop.
Project Euler 05: Smallest Multiple
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