Skip to content

Instantly share code, notes, and snippets.

@daiksy
Created November 23, 2012 12:20
Show Gist options
  • Select an option

  • Save daiksy/4135381 to your computer and use it in GitHub Desktop.

Select an option

Save daiksy/4135381 to your computer and use it in GitHub Desktop.
Project Euler Problem 12
/**
* http://projecteuler.net/problem=12
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2012
*/
def numbers(n: Long): Stream[Long] = n #:: numbers(n + 1L)
val triangle = numbers(2L).scanLeft(1L){(a,b) => a + b }
val divisors = (n: Long) => {
(1L to n / 2).filter(n % _ == 0) :+ n
}
val ret = triangle.find(divisors(_).size >= 501).getOrElse(0L)
println(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment