-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
math: add fib, fib_seq functions (#34)
* math: add fib, fib_seq functions * update: directory * update: remove dot
- Loading branch information
Showing
3 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |