Created
April 25, 2012 20:00
-
-
Save ryanoneill/2492909 to your computer and use it in GitHub Desktop.
Project Euler - Problem 05 - What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
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
// http://projecteuler.net/problem=5 | |
// 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. | |
// What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? | |
object Problem05 { | |
def main(args: Array[String]) { | |
val numbersToTen = (1 to 10).toList | |
println("Lowest Common Multiple of 1 to 10: " + lcmList(numbersToTen)) | |
val numbersToTwenty = (1 to 20).toList | |
println("Lowest Common Multiple of 1 to 20: " + lcmList(numbersToTwenty)) | |
} | |
def lcmList(numbers: List[Int]) = | |
numbers.foldLeft(1)(lcm) | |
def lcm(a: Int, b: Int) = | |
((a.toLong * b.toLong) / gcd(a, b)).toInt | |
def gcd(a: Int, b: Int) : Int = | |
if (a == b) a else if (a > b) gcd(a - b, a) else gcd(a, b - a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment