-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.scala
More file actions
31 lines (25 loc) · 794 Bytes
/
Problem2.scala
File metadata and controls
31 lines (25 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import scala.annotation.tailrec
object Main extends App {
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0)*0.000000001 + "s")
result
}
time {
/**
* Basic algorithm with O(n) speed
*/
def basicAlgo(until: Int = 4000000) = {
@tailrec
def fibonacciRecursion(listOfFibs: List[Int] = List(), first: Int = 1, second: Int = 1): List[Int] = {
if (second >= until) listOfFibs
else if (second % 2 == 0) fibonacciRecursion(listOfFibs :+ second, second, first + second)
else fibonacciRecursion(listOfFibs, second, first + second)
}
fibonacciRecursion()
}
println(basicAlgo().sum)
}
}