Skip to content

Commit

Permalink
Updated the fibonacci algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
sohilladhani committed Apr 25, 2019
1 parent 967161f commit 6600804
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions chapter_03/exercises/fibonacci/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::io;

fn nth_fibonacci(n: i32) -> i32 {
let mut fibonacci_sum = 1;
if n <= 2 {
return n - 1;
fn nth_fibonacci(n: usize) -> usize {
if n <= 1 {
return n;
} else {
for element in 2..n - 2 {
fibonacci_sum += element;
}
return nth_fibonacci(n-1) + nth_fibonacci(n-2);
}
return fibonacci_sum;
}

fn main() {
Expand All @@ -20,7 +16,7 @@ fn main() {

io::stdin().read_line(&mut n).expect("Failed to read line");

let n: i32 = match n.trim().parse() {
let n: usize = match n.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
Expand Down

0 comments on commit 6600804

Please sign in to comment.