mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add fibonacci/rust/function
This commit is contained in:
parent
c8a7a6dcd4
commit
1553324b3a
@ -6,7 +6,7 @@ Created by [@Divlo](https://github.com/Divlo) on 5 July 2020.
|
||||
|
||||
The function should return an array of fibonacci numbers. The function takes a `number` as an argument to decide how many number of elements to produce.
|
||||
|
||||
**Note :** The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Each subsequent number is the sum of the previous two.
|
||||
**Note:** The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Each subsequent number is the sum of the previous two.
|
||||
|
||||
## Examples
|
||||
|
||||
|
7
challenges/fibonacci/solutions/rust/function/Cargo.lock
generated
Normal file
7
challenges/fibonacci/solutions/rust/function/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 = "rust_application"
|
||||
version = "1.0.0"
|
6
challenges/fibonacci/solutions/rust/function/Cargo.toml
Normal file
6
challenges/fibonacci/solutions/rust/function/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust_application"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
3
challenges/fibonacci/solutions/rust/function/README.md
Normal file
3
challenges/fibonacci/solutions/rust/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# fibonacci/rust/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 5 January 2023.
|
20
challenges/fibonacci/solutions/rust/function/src/main.rs
Normal file
20
challenges/fibonacci/solutions/rust/function/src/main.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use std::io;
|
||||
|
||||
pub fn fibonacci(number: u32) -> u32 {
|
||||
if number == 0 || number == 1 {
|
||||
return number;
|
||||
}
|
||||
return fibonacci(number - 1) + fibonacci(number - 2);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut number = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut number)
|
||||
.expect("Failed to read `stdin` line.");
|
||||
let number: u32 = number
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Failed to convert `number` as an `u32`.");
|
||||
println!("{}", fibonacci(number));
|
||||
}
|
Loading…
Reference in New Issue
Block a user