From 4c48c1b0af2952a495fa1feb00171201e0efca28 Mon Sep 17 00:00:00 2001 From: adnanioricce Date: Sat, 28 Dec 2024 07:59:26 -0300 Subject: [PATCH] [Exercise] - Adding fibonacci program --- fib/main.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 fib/main.rs diff --git a/fib/main.rs b/fib/main.rs new file mode 100644 index 0000000..200e34f --- /dev/null +++ b/fib/main.rs @@ -0,0 +1,22 @@ +const GOLDEN_RATIO:f32 = 1.6180339887; +const GOLDEN_CONJUGATE:f32 = -0.6180339887; +const FIVE_SQRT:f32 = 2.236067977;//√5 +fn fib(n:f32) -> f32 { + return (GOLDEN_RATIO.powf(n) - GOLDEN_CONJUGATE.powf(n)) / FIVE_SQRT; +} +fn main(){ + let args: Vec = std::env::args().collect(); + let input = args.get(1); + match input { + Some (inStr) => { + match inStr.trim().parse() { + Ok (n) => { + let result = fib(n); + println!("fib({}) = {:.2}",n,result); + }, + Err(err) => println!("can't parse value to float: {}",err) + } + }, + None => println!("fib [LIMIT]") + }; +}