Skip to content

Commit

Permalink
math: add fib, fib_seq functions (#34)
Browse files Browse the repository at this point in the history
* math: add fib, fib_seq functions

* update: directory

* update: remove dot
  • Loading branch information
lareii authored Dec 23, 2023
1 parent 0d73edb commit 6c1b2bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Median](math/median.jule)
- [Min](math/min.jule)
- [Sum](math/sum.jule)
- [Fib](math/fib.jule)

## Search
- [Binary Search](search/binary_search.jule)
Expand Down
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 6c1b2bb

Please sign in to comment.