Skip to content

Commit a72357b

Browse files
authored
Create fibonacci_iterative.rs
1 parent 513070d commit a72357b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/rust/fibonacci_iterative.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn fibonacci_iterative(n: u32) -> u64 {
2+
if n <= 1 {
3+
return n as u64;
4+
}
5+
6+
let mut a: u64 = 0;
7+
let mut b: u64 = 1;
8+
9+
for _ in 2..=n {
10+
let temp = a;
11+
a = b;
12+
b += temp;
13+
}
14+
15+
b
16+
}
17+
18+
fn main() {
19+
let index = 15;
20+
println!("Fibonacci (iterative) of {} is: {}", index, fibonacci_iterative(index));
21+
}

0 commit comments

Comments
 (0)