Skip to content

Commit

Permalink
add iterative and recursive fibonacci functions
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Nov 27, 2024
1 parent 522645e commit b3bc5c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions benchmarks/fib_dynamic.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fun fib(_ n: Int): Int {

access(all)
fun main() {
assert(fib(14) == 377)
}
assert(fib(23) == 28657)
}
19 changes: 19 additions & 0 deletions benchmarks/fib_iterative.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
access(all)
fun fib(_ n: Int): Int {
var fib1 = 1
var fib2 = 1
var fibonacci = fib1
var i = 2
while i < n {
fibonacci = fib1 + fib2
fib1 = fib2
fib2 = fibonacci
i = i + 1
}
return fibonacci
}

access(all)
fun main() {
assert(fib(23) == 28657)
}
12 changes: 12 additions & 0 deletions benchmarks/fib_recursive.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
access(all)
fun fib(_ n: Int): Int {
if n < 2 {
return n
}
return fib(n - 1) + fib(n - 2)
}

access(all)
fun main() {
assert(fib(23) == 28657)
}

0 comments on commit b3bc5c1

Please sign in to comment.