Skip to content

Commit

Permalink
chore: add fibonacci
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBoji authored Jan 26, 2024
1 parent 72a76a0 commit 7bdd680
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/day07_to_day09/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
fn fibonacci(n: u32) -> Vec<u32> {
let mut sequence = Vec::with_capacity(n as usize);

for i in 0..n {
if i == 0 {
sequence.push(0);
} else if i == 1 {
sequence.push(1);
} else {
let last = sequence[i as usize - 1];
let second_last = sequence[i as usize - 2];
sequence.push(last + second_last);
}
}

sequence
}

pub fn run() {
let n = 10;
let fib_sequence = fibonacci(n);
println!("First {} Fibonacci numbers: {:?}", n, fib_sequence);
}
1 change: 1 addition & 0 deletions src/day07_to_day09/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod fibonacci;
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ pub mod day01_to_day03;

//Day04 to day06
pub mod day04_to_day06;
use day04_to_day06::shadowing;
use day04_to_day06::ownership;
use day04_to_day06::borrowing;
// use day04_to_day06::shadowing;
// use day04_to_day06::ownership;
// use day04_to_day06::borrowing;


//Day 07 to day 09
pub mod day07_to_day09;
use day07_to_day09::fibonacci;



fn main() {
Expand All @@ -45,6 +51,8 @@ fn main() {
//Day04 to day06
// shadowing::run();
// ownership::run();
borrowing::run();
// borrowing::run();
fibonacci::run();


}

0 comments on commit 7bdd680

Please sign in to comment.