aboutsummaryrefslogtreecommitdiff
path: root/entries/dewert99/fib.rs
diff options
context:
space:
mode:
authorfunemy2022-10-25 07:14:52 +0000
committerfunemy2022-10-25 07:14:52 +0000
commit998ef837b43203319397f191036a97a5adc42220 (patch)
tree920558bd60d282c663fd2da1cd4144cac1849a1d /entries/dewert99/fib.rs
parentfdac6c60e115297a58f5b81da0c4b7f18ac758f2 (diff)
parentce7544a6db594f7d3dfad0d7dc65d01515e57ad6 (diff)
Merge branch 'main' of github.com:braxtonhall/fib
Diffstat (limited to 'entries/dewert99/fib.rs')
-rw-r--r--entries/dewert99/fib.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/entries/dewert99/fib.rs b/entries/dewert99/fib.rs
new file mode 100644
index 0000000..ecf0a85
--- /dev/null
+++ b/entries/dewert99/fib.rs
@@ -0,0 +1,22 @@
+const MAX: usize = 94;
+
+const fn fibc<const N: usize>() -> [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<u64> {
+ if n >= MAX {
+ None // Fib of n wouldn't fit in 64-bits
+ } else {
+ Some(FIB[n])
+ }
+}