Some(number) of ways to calculate a Fibonacci Number in Rust
Tags: rust, crates, learning, algorithms
Published: 2020-07-22 21:31:51

---
title: Some(number) of ways to calculate a Fibonacci Number in Rust
published: true
date: 2020-07-22 21:15:49 UTC
tags: Rust,Crates,Learning,algorithm,
canonical_url: https://rust.graystorm.com/2020/07/22/somenumber-of-ways-to-calculate-a-fibonacci-number-in-rust/
cover_image: https://rust.graystorm.com/wp-content/uploads/2019/06/plant-wood-old-rust-equipment-gear-1278587-pxhere.com_.jpg
---

[![A Fibonacci spiral found in nature, in a nautilus shell.](https://i1.wp.com/rust.graystorm.com/wp-content/uploads/2020/07/sea-spiral-animal-red-holiday-memory-1255334-pxhere.com_.jpg?w=840&ssl=1)](https://en.wikipedia.org/wiki/Golden_ratio)<figcaption>A Fibonacci number divided by the previous one approaches Phi, the <a aria-label="undefined (opens in a new tab)" href="https://en.wikipedia.org/wiki/Golden_ratio" target="_blank" rel="noreferrer noopener">Golden Ratio</a>. That’s an Internet hole that can last for hours, if you let it! However, <a aria-label="undefined (opens in a new tab)" href="https://en.wikipedia.org/wiki/Golden_ratio#Disputed_observations" target="_blank" rel="noreferrer noopener">don’t believe everything</a> you read about Phi.</figcaption>

Solving Fibonacci Numbers is the start of Chapter 3 on Dynamic Programming (more on that below) in Erickson’s [Algorithm](http://jeffe.cs.illinois.edu/teaching/algorithms/) book. I had started with [Peasant Multiplication](https://rust.graystorm.com/2020/07/14/algorithmic-rust/), then the [merge sort](https://rust.graystorm.com/2020/07/15/merge-sort-with-rust/), and most fun – the [n Queens Problem](https://rust.graystorm.com/2020/07/16/next-algorithm-backtracking-into-the-n-queens-problem/). With Fibonacci Number calculating though, there are a couple of obvious ways to code this and some presented questions for me with Rust.

I became interested in comparing a couple of these ways – both in thinking about the Rust code and looking at the timing. Then my eyes popped out when I saw the difference in the release build times!

![](https://i0.wp.com/rust.graystorm.com/wp-content/uploads/2020/07/Screen-Shot-2020-07-22-at-5.08.27-PM.png?w=840&ssl=1)<figcaption><em>Pseudo code, Page 97 from Algorithm by Jeff Erickson</em><br>Clean, Simple Recursion<br></figcaption>

First is probably closest to the pseudo code for the classic solution for Fibonacci. Define what 0 and 1 return and solve for the given number, recursing all the way back to 2 (which adds the fixed answers for 0 and 1):

## Recursive Backtracing

```
fn backtrace_fib(fib_num: u128) -> u128 {
    if fib_num == 0 || fib_num == 1 {
        return fib_num;
    }
    backtrace_fib(fib_num - 1) + backtrace_fib(fib_num - 2)
}
```

Next, especially important if your function will be called multiple times, is to add memoization. That is, once you calculate an answer for a given input, remember than answer so you can quickly respond with it later, skipping the math.

I don’t like that, with the way I did this, I have to pass in my HashMap where I’m storing the answers. Is there any way to have some 'static living private variable that only this function would see, but would last the whole run of the program?? There are other places I could store away data, like memcache and redis.

## Backtracing With Memoization

```
fn backtrace_memo_fib(memo: &mut HashMap<u128, u128>, fib_num: u128) -> u128 {
    match memo.get(&fib_num).map(|answer| answer.clone()) {
        Some(result) => result,
        None => {
            let result = match fib_num {
                0 | 1 => fib_num,
                n => backtrace_memo_fib(memo, n - 1) + backtrace_memo_fib(memo, n - 2),
            };
            memo.insert(fib_num, result.clone());
            result
        }
    }
}
```

So, Jeff Erickson, author of the [Algorithms](http://jeffe.cs.illinois.edu/teaching/algorithms/) book, explains another way solve for a Fibonacci Number involves using dynamic programming. But this IS your grandfathers “programming” (not ours).

> The word 'programming' does not refer to writing code, but rather to the older sense of _planning_ or _scheduling_, typically by filling in a table. For example, sports programs and theater programs are schedules of important events (with ads); television programming involves filling each available time slot with a show (and ads); degree programs are schedules of classes to be taken (with ads).
> 
> <cite>Erickson, Jeff (2018, December 29). Algorithms. pp. 100. Retrieved from Algorithms: http://algorithms.wtf</cite>

For this one, I just kept the memoization inside the function. We then purposely fill the HashMap on our way up, referencing already-calculated answers, and so never descend into levels of recursion.

![](https://i0.wp.com/rust.graystorm.com/wp-content/uploads/2020/07/Screen-Shot-2020-07-22-at-5.08.47-PM-1.png?w=840&ssl=1)<figcaption><em>Pseudo code, Page 99 from Algorithm by Jeff Erickson</em></figcaption>

## Dynamic Programming

```
fn dynamic_fib(fib_num: u128) -> u128 {
    let mut memo = HashMap::new();
    memo.insert(0, 0);
    memo.insert(1, 1);
    match fib_num {
        0 | 1 => {} // already set
        n => {
            for i in 2..=n {
                let result = *memo.get(&(i - 1)).unwrap() + *memo.get(&(i - 2)).unwrap();
                memo.insert(i, result);
            }
        }
    };
    *memo.get(&fib_num).unwrap()
}
```

So, as I was trying to figure out how to store some data in a function for future calls (if that is even possible), I came across the [cached crate](http://cached%20=%20%220.16.0%22). “`cached` provides implementations of several caching structures as well as a handy macro for defining memoized functions.” How interesting – just what I needed and the memoization is even hidden from ME. Ok, I’ll do the simple, recursive, backtracing function but cache the results.

## Cached Backtracing Function

```
#[cached(size = 200)]
fn cached_fib(fib_num: u128) -> u128 {
    if fib_num == 0 || fib_num == 1 {
        return fib_num;
    }
    cached_fib(fib_num - 1) + cached_fib(fib_num - 2)
}
```

## Fibonacci Fight

So, I wrap all of those up into main() so I can start comparing them. That code is pretty boring, so check it out in the repo. Mostly I run each flavor of the algorithm, timing the result and print it all out. The first one looks like:

```
    let now = Instant::now();
    let _ = backtrace_fib(fib_num);
    let elapsed = now.elapsed();
    print_results(fib_num, "simple backtracing/recursion", elapsed);
```

I also added a test, just to be sure each is giving the correct answer.

```
#[test]
fn test_each_version() {
    assert_eq!(backtrace_fib(20), 6765);
    assert_eq!(backtrace_memo_fib(&mut HashMap::new(), 20), 6765);
    assert_eq!(dynamic_fib(20), 6765);
    assert_eq!(cached_fib(20), 6765);
}

```

## Fibonacci Racing

And then some code to time each version, and call it a second and third time, so we can see the memoization at work.

Of course, your results will vary by CPU, but look at this run for Fibonacci Number 50 with the debug build (where the differences are huge). This is on an Amazon t2.medium EC2 server:

```
The first time solving will be the slowest

  Solving fib:50 with simple backtracing/recursion                       took    287244288946 ns
  Solving fib:50 with backtracing/recursion with memoization             took          211289 ns
  Solving fib:50 with dynamic programming with memoization               took          167291 ns
  Solving fib:50 with cached function                                    took          214175 ns

What about solving it a second or third time, anyone faster this time?

  Solving fib:50 with simple backtracing/recursion                       took    287262389809 ns
  Solving fib:50 with backtracing/recursion with memoization             took          202481 ns
  Solving fib:50 with dynamic programming with memoization               took          162527 ns
  Solving fib:50 with cached function                                    took            6285 ns

  Solving fib:50 with simple backtracing/recursion                       took    287273113221 ns
  Solving fib:50 with backtracing/recursion with memoization             took          185609 ns
  Solving fib:50 with dynamic programming with memoization               took          158492 ns
  Solving fib:50 with cached function                                    took            6496 ns

By the way, Fibonacci Number 50 is 12586269025 which (divided by Fib Num 49) approxmates phi as 1.618033988749895
```

But wow, check out these numbers with a release build!!

```
The first time solving will be the slowest

  Solving fib:50 with simple backtracing/recursion                       took             806 ns
  Solving fib:50 with backtracing/recursion with memoization             took           16128 ns
  Solving fib:50 with dynamic programming with memoization               took            8052 ns
  Solving fib:50 with cached function                                    took           19495 ns

What about solving it a second or third time, anyone faster this time?

  Solving fib:50 with simple backtracing/recursion                       took             596 ns
  Solving fib:50 with backtracing/recursion with memoization             took            8142 ns
  Solving fib:50 with dynamic programming with memoization               took            7576 ns
  Solving fib:50 with cached function                                    took             724 ns

  Solving fib:50 with simple backtracing/recursion                       took             596 ns
  Solving fib:50 with backtracing/recursion with memoization             took            7549 ns
  Solving fib:50 with dynamic programming with memoization               took            7327 ns
  Solving fib:50 with cached function                                    took             746 ns

By the way, Fibonacci Number 50 is 12586269025 which (divided by Fib Num 49) approxmates phi as 1.618033988749895
```

## A Really Big Number

And it handles a Fibonacci Number of 186, though any higher and casting the answers as **f64** s to divide and get Phi breaks. The fastest “first” calculation for Fibonacci Number 186 was **simple backtracing/recursion** and took only 820 ns and it’s kinda big:

```
By the way, Fibonacci Number 186 is 332825110087067562321196029789634457848 which (divided by Fib Num 185) approximates phi as 1.6180339887498947
```

The post [Some(number) of ways to calculate a Fibonacci Number in Rust](https://rust.graystorm.com/2020/07/22/somenumber-of-ways-to-calculate-a-fibonacci-number-in-rust/) appeared first on [Learning Rust](https://rust.graystorm.com).