Fibonacci Function in Rust
The following function calculates the Fibonacci number at a given position n:
fn fibonacci(n: i32) -> i32 {
if n == 0i32 {
return 0i32;
} else if n == 1i32 {
return 1i32;
}
let mut a = 0i32;
let mut b = 1i32;
for _ in 2i32..=n {
let temp = a + b;
a = b;
b = temp;
}
b
}
fn main() {
let n = 10i32;
println!("Fibonacci({}) = {}", n, fibonacci(n));
}
Explanation:
- If
nis0, the function returns0. - If
nis1, the function returns1. - Otherwise, it initializes two variables,
aandb, with the first two Fibonacci numbers (0and1). - It iterates from
2ton, updating the values to calculate the Fibonacci sequence iteratively. - Finally, it returns the
n-th Fibonacci number.
Printing the Fibonacci Sequence up to a Limit
The following function prints the Fibonacci sequence up to a given number n:
fn fibonacci_sequence(n: i32) {
let mut a = 0i32;
let mut b = 1i32;
print!("Fibonacci sequence up to {}: {} {}", n, a, b);
while a + b <= n {
let temp = a + b;
print!(" {}", temp);
a = b;
b = temp;
}
println!();
}
fn main() {
let n = 50i32;
fibonacci_sequence(n);
}
Explanation:
- Initializes
aandbas0and1, the first two Fibonacci numbers. - Prints the initial values.
- Uses a
whileloop to generate and print Fibonacci numbers until the sum exceedsn. - This efficiently prints all Fibonacci numbers up to
n.