Create Fibonacci sequence calculator
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -1,3 +1,24 @@
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
println!("Rust Fibonacci: Calculate the nth element of the Fibonacci sequence.");
|
||||
|
||||
let mut n = String::new();
|
||||
|
||||
println!("What would you like n to equal? ");
|
||||
io::stdin()
|
||||
.read_line(&mut n)
|
||||
.expect("Enter an integer.");
|
||||
|
||||
let n = n.trim().parse().expect("Failed to parse.");
|
||||
|
||||
let mut f_previous = 0;
|
||||
let mut f = 1;
|
||||
|
||||
for _each in 1..n {
|
||||
println!("{}", f + f_previous);
|
||||
let f_placeholder = f_previous;
|
||||
f_previous = f;
|
||||
f = f + f_placeholder;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user