From dd5fef744a98521bc586b149272580456405ec8f Mon Sep 17 00:00:00 2001 From: David Ewert Date: Mon, 24 Oct 2022 15:42:38 -0700 Subject: Added Fib --- entries/dewert99/fib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 entries/dewert99/fib.rs (limited to 'entries/dewert99/fib.rs') diff --git a/entries/dewert99/fib.rs b/entries/dewert99/fib.rs new file mode 100644 index 0000000..b1744d5 --- /dev/null +++ b/entries/dewert99/fib.rs @@ -0,0 +1,22 @@ +const MAX: usize = 94; + +const fn fibc() -> [u64; N] { + let mut res = [0; N]; + res[1] = 1; + let mut i = 2; + while i < N { + res[i] = res[i - 1] + res[i - 2]; + i += 1; + } + res +} + +static FIB: [u64; MAX] = fibc(); + +pub fn fib(n: usize) -> Option { + if n > MAX { + None // Fib of n wouldn't fit in 64-bits + } else { + Some(FIB[n]) + } +} -- cgit v1.2.3-70-g09d2 From b707a7103789116363427bdd248007b61d527196 Mon Sep 17 00:00:00 2001 From: David Ewert Date: Mon, 24 Oct 2022 16:01:26 -0700 Subject: >= --- entries/dewert99/fib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'entries/dewert99/fib.rs') diff --git a/entries/dewert99/fib.rs b/entries/dewert99/fib.rs index b1744d5..ecf0a85 100644 --- a/entries/dewert99/fib.rs +++ b/entries/dewert99/fib.rs @@ -14,7 +14,7 @@ const fn fibc() -> [u64; N] { static FIB: [u64; MAX] = fibc(); pub fn fib(n: usize) -> Option { - if n > MAX { + if n >= MAX { None // Fib of n wouldn't fit in 64-bits } else { Some(FIB[n]) -- cgit v1.2.3-70-g09d2