Skip to content

Commit

Permalink
math: add fib, fib_seq functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Dec 22, 2023
1 parent 0d73edb commit a7dd9da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions math/fib.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// O(2^n).
pub fn fib(i: int): int {
if i == 0 || i == 1 {
ret i
}

ret fib(i - 1) + fib(i - 2)
}

// O(n * 2^n)
pub fn fib_seq(mut n: int): []int {
let mut s: []int

let mut i = n
for i > 0 {
s = append(s, fib(n - i + 1))
i--
}

ret s
}
9 changes: 9 additions & 0 deletions math/fib_test.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#build test

use std::testing::{T}

#test
fn test_fib(t: &T) {
t.assert(fib(6) == 8, "index 6 of fib. should be 8")
t.assert(fib_seq(6) == [1, 1, 2, 3, 5, 8], "a fib. sequence up to index 6 should be 1, 1, 2, 3, 5, 8")
}

0 comments on commit a7dd9da

Please sign in to comment.