Created
October 24, 2015 09:18
-
-
Save to4iki/160c6d4173b6447f5201 to your computer and use it in GitHub Desktop.
projecteuler
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
object Euler { | |
// http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%205 | |
def main(args: Array[String]) { | |
// val r1 = test(10) | |
val r = (1L to 20L).reduceLeft(lcm) | |
println(r) | |
} | |
// def test(n: Int) = (20 to Int.MaxValue).find(x => (2 to n).forall(x % _ == 0)) | |
// 最小公倍数(least common multiple) | |
def lcm(a: Long, b: Long): Long = | |
a * b / gcd(a, b) | |
// 最大公約数(greatest common divisor) | |
// Euclidean Algorithm | |
@scala.annotation.tailrec | |
def gcd(a: Long, b: Long): Long = | |
if (b == 0) a | |
else gcd(b, a % b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment