Skip to content

Instantly share code, notes, and snippets.

@daclouds
Last active December 16, 2015 07:49
Show Gist options
  • Save daclouds/db0789c2d90dedf3483b to your computer and use it in GitHub Desktop.
Save daclouds/db0789c2d90dedf3483b to your computer and use it in GitHub Desktop.
/*
* 피보나치 수열의 각 항은 바로 앞의 항 두 개를 더한 것이 됩니다. 1과 2로 시작하는 경우 이 수열은 아래와 같습니다.
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
* 짝수이면서 4백만 이하인 모든 항을 더하면 얼마가 됩니까?
*/
object Euler02 extends App {
def fibonacci(i: Int): Int = i match {
case 0 => 1
case 1 => 2
case _ => fibonacci(i - 2) + fibonacci(i - 1)
}
var i = 0
var sum = 0
while (fibonacci(i) <= 4000000) {
if (fibonacci(i) % 2 == 0) sum = sum + fibonacci(i)
i = i + 1
}
println(sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment