Skip to content

Instantly share code, notes, and snippets.

@to4iki
Created October 24, 2015 09:18
Show Gist options
  • Save to4iki/160c6d4173b6447f5201 to your computer and use it in GitHub Desktop.
Save to4iki/160c6d4173b6447f5201 to your computer and use it in GitHub Desktop.
projecteuler
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