Skip to content

Commit ece7f97

Browse files
committed
add find-nth-fibonacci mini project
1 parent 7478223 commit ece7f97

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "find-nth-fibonacci"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// use std::{}
2+
3+
// ============================================================================
4+
5+
const GOLDEN_RATIO: f64 = 1.618033988749895; // known as φ (phi)
6+
7+
fn main() {
8+
// just clear the console for us
9+
print!("\x1b[2J\x1b[1;1H");
10+
11+
let input = 6;
12+
13+
let nth_of_input = calculate_nth_fibonacci(input);
14+
15+
println!("the {input}th of fibonacci sequence is : {nth_of_input}");
16+
}
17+
18+
fn calculate_nth_fibonacci(nth: u32) -> f64 {
19+
let square_of_five: f64 = (5 as f64).sqrt();
20+
21+
let nth: f64 = nth.into();
22+
23+
(GOLDEN_RATIO.powf(nth) - (1.0 - GOLDEN_RATIO).powf(nth)) / square_of_five
24+
}

0 commit comments

Comments
 (0)