Add a project for calculating Fibonacci
This commit is contained in:
parent
c6e001b549
commit
bca900b692
3 changed files with 30 additions and 0 deletions
7
fibonacci/Cargo.lock
generated
Normal file
7
fibonacci/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "fibonacci"
|
||||
version = "0.1.0"
|
6
fibonacci/Cargo.toml
Normal file
6
fibonacci/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "fibonacci"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
17
fibonacci/src/main.rs
Normal file
17
fibonacci/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use std::io;
|
||||
|
||||
fn main() {
|
||||
let mut input = String::new();
|
||||
println!("Please enter the number to calculate the Fibonacci sequence for.");
|
||||
io::stdin().read_line(&mut input).expect("An error occurred!");
|
||||
let value: u32 = input.trim().parse().unwrap();
|
||||
println!("Result: {}", fibonacci(value));
|
||||
}
|
||||
|
||||
fn fibonacci(end: u32) -> u32 {
|
||||
if end <= 1 {
|
||||
1
|
||||
} else {
|
||||
fibonacci(end - 2) + fibonacci(end - 1)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue