Skip to content

Instantly share code, notes, and snippets.

@zerosum
Created January 20, 2013 06:49
Show Gist options
  • Save zerosum/4577031 to your computer and use it in GitHub Desktop.
Save zerosum/4577031 to your computer and use it in GitHub Desktop.
Project Euler: Problem 5
object Euler0005 {
def main(args: Array[String]): Unit = {
val factors = (1 to 20).toList
println(getSmallestMultiple(factors))
}
private def getSmallestMultiple(factors: List[Int]): Int = {
getSmallestMultiple(factors, Nil).product
}
private def getSmallestMultiple(factors: List[Int], primeFactors: List[Int]): List[Int] = {
if(factors.isEmpty){
primeFactors
} else {
val h = factors.head
val l = factors.tail.map(x => if(x % h == 0) x/h else x)
getSmallestMultiple(l, h :: primeFactors)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment