Generating the nth Fibonacci number

Overview

Generating the nth Fibonacci number

Image denoting the Fibonacci sequence


Per Wikipedia, "In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1".

The initial program I wrote was based on the Binet formula, (see below), which is considered an exact formula for computing the n-th term of the Fibonacci sequence.  After testing the program, I found that the precision was off around an input of 50.  The datatype used in the program was u128.  I assumed the precision loss was due to two mathematical computations; computing the square root and division.  Unsatisified with the outcome, I decided to do a little more research.

Binet formula
ₓ²   −   −  1 =  0 :  α  =  (1 + √5)  ÷ 2,  β  =  (1 − √5)  ÷ 2
ϝη = (αη − βη) ÷ (α − β)

I came across an interesting article in Medium on Memoization in Rust written by Andrew Pritchard.  Memoization is an optimization technique which is used to speed up the result of a program by storing the result of a computation for the inputted value and then returning the cached result when the same input occurs again.

Using the Fibonacci example in the article, one issue I ran into, again, was the u128 dataype.  In using the datatype with the memoization approach, I would receive a panic message of 'attempt to add with overflow' when inputting a value greater than 186.  Since I could not figure out how to eloquently handle this error, I hardcoded a fix which I wasn't completely happy with: see here on line 66 .  Another issue I found involved the formatting of the output.  The formatting library used with the u128 datatype was not applicable for the BigUint datatype.  I had an idea how I wanted the program to function and what i wanted the output to look like so I scrapped the application and decided to the write a new program using the BigUint datatype and develope my own function to format the output into what I consider a more appealing form.  The algorithm in the new program is based on the display below with a sample output beneath it.

Algorithm for the Fibonacci sequence

Algorithm for the Fibonacci sequence


The 180th Fibonacci number

Algorithm for the Fibonacci sequence


The way the program is now constructed satisfies my previous concerns:

   (1) Allowing the user a wider range of numbers to input.
          The largest number I tried successfully was 1.6 million.
   (2) Format the Fibonacci Sequence and the user input.

The 'formated' function has been implemented on both the u128 and BigUint datatypes.  That allows each of the mentioned dataypes to now have the capability to call the formated function on its own value. The source code can be viewed in the src/main folder for those unfamiliar with the Rust language.

Thanks for reading and do reach out and let me know if you have any questions or concerns.  All suggestions, constructive, even non-constructive, will be welcoming 😃 .
For those interested in the Fibonacci sequence, here is a full list of the first 10, 100, and 300 Fibonacci numbers.

You might also like...
Number names is a Rust library to provide formatted string names for cardinal and ordinal numbers.

Number Names Number names is a Rust library to provide formatted string names for cardinal and ordinal numbers. At this time, only American English is

File Tree Fuzzer allows you to create a pseudo-random directory hierarchy filled with some number of files.

FTZZ File Tree Fuzzer allows you to create a pseudo-random directory hierarchy filled with some number of files. Installation $ cargo +nightly install

Outputs a random number of 🌈's
Outputs a random number of 🌈's

Rainbows Outputs a random number of 🌈 's. Rust implementation of rainbows. 📦 Installation Install Rust and Cargo Run cargo install rainbows $ rainbo

A Rust attribute macro to limit a function's number of runs over a specified period of time

throttle_my_fn: A Rust attribute macro to throttle the execution of functions throttle_my_fn is a Rust attribute macro to limit a function's number of

A small random number generator hacked on top of Rust's standard library. An exercise in pointlessness.

attorand from 'atto', meaning smaller than small, and 'rand', short for random. A small random number generator hacked on top of Rust's standard libra

A Rust library for random number generation.

A Rust library for random number generation.

Some collections to store a fixed number of elements of a specific type.

This repo provide some useful data-structures, such as: Array, HashMap, HashSet, BTreeMap, BTreeSet, etc... That lives on the stack. It's a good choic

Convert number like 42 to forty-two

num2words Convert number like 42 to forty-two Usage This crate can be either used as a library or a binary. Library Example usage: use num2words::Num2

SWC Transform to prefix logs. Useful for adding file and line number to logs

SWC Transform to prefix logs. Useful for adding file and line number to logs

1️⃣ el lisp number uno - one lisp to rule them all 🏆

luno el lisp number uno luno is the one lisp to rule them all. Still experimental, do not use it in production yet. goals embeddable small size simple

Python module implemented in Rust for counting the number of one bits in a numpy array.

bit-counter Package for counting the number of one bits in a numpy array of uint8 values. Implemented as a Python module using Rust, providing high pe

A plugin to enable random number generation for the Bevy game engine.

bevy_turborand A plugin to enable random number generation for the Bevy game engine, built upon turborand. Implements ideas from Bevy's Deterministic

A command-line utility which aligns a block of text within the terminal (or a specified number of columns), written in Rust.
A command-line utility which aligns a block of text within the terminal (or a specified number of columns), written in Rust.

align: a command line utility for aligning text. ⭐ Overview Aligns text within the terminal (or a specified number of columns). The text is treated as

Count number of ifs in your rust project!
Count number of ifs in your rust project!

A long awaited solution for a widely encountered problem! The will count the number of ifs in your rust project! (it can also collect some other numer

🦀 Stupid simple presentation of the number of words, characters and lines on your clipboard.

clipcount: Counting words from the clipboard content Why does this exist? Do you find yourself often needing to count the number of words in a piece o

Social media style compact number formatting for rust.

prettty-num Format integers into a compact social media style format, similar to using Intl.NumberFormat("en", { notation: "compact" }); as a number f

A Rust library for generating cryptocurrency wallets
A Rust library for generating cryptocurrency wallets

Table of Contents 1. Overview 2. Build Guide 2.1 Install Rust 2.2a Build from Homebrew 2.2b Build from Crates.io 2.2c Build from Source Code 3. Usage

A project for automatically generating and maintaining Debian repositories from a TOML spec.

Debian Repository Builder A simple utility for constructing and maintaining Debian repositories. Configuration of a repo is based on the directory hie

A command-line tool and library for generating regular expressions from user-provided test cases
A command-line tool and library for generating regular expressions from user-provided test cases

Table of Contents What does this tool do? Do I still need to learn to write regexes then? Current features How to install? 4.1 The command-line tool 4

Owner
Charles "Chas" O'Riley
Technology Enthusiast
Charles
proc macros for generating mut and non-mut methods without duplicating code

mwt Hey! You! Read this before using! mwt was thrown together pretty quickly for personal use, because I couldn't find an existing crate that does thi

null 1 Dec 24, 2021
A library for parsing and generating ESP-IDF partition tables

esp-idf-part A library for parsing and generating ESP-IDF partition tables. Supports parsing from and generating to both CSV and binary formats. This

esp-rs 5 Nov 16, 2022
Web service generating images of Japanese (Riichi) Mahjong hands.

chombo-gen ChomboGen is a web service that allows to generate images of Japanese (Riichi) Mahjong hands. The hands are provided in a text format and a

Mateusz Maćkowski 5 May 2, 2023
📘 Utilities for the Fibonacci Number and Sequence

Fibora Port of fibonacci-deno for Rust. Utilities for the Fibonacci Number and Sequence. Usage This package exposes two Functions, fibonacci and fibon

Eliaz Bobadilla 5 Apr 6, 2022
Print out some fibonacci numbers.

give-me-some-fibonacci A Rust library for some fibonacci. TL;DR: its just a joke. Usage To get started using give_me_some_fibonacci, just add this to

Leonardo Vieira 2 Mar 22, 2022
Fibonacci, but different

n-days Fibonacci, but different? Problem You're given a workout in the 12 Days of Christmas style: 1. Burpee Bar Muscle-Up 2. Thrusters 3. Power Clean

Phillip Copley 0 Dec 24, 2021
A number of collections, such as linked-lists, binary-trees, or B-Trees are most easily implemented with aliasing pointers.

StaticRc is a safe reference-counted pointer, similar to Rc or Arc, though performing its reference-counting at compile-time rather than run-time, and

null 372 Dec 19, 2022
Calculate primes since the first number until the 10001th

find_prime Explaining this code: find_prime is a function that takes the nth index to be calculated. Note that this nth does not follow the array conv

Ciro Dourado 3 Apr 29, 2021
Fast and easy random number generation.

alea A zero-dependency crate for fast number generation, with a focus on ease of use (no more passing &mut rng everywhere!). The implementation is bas

Jeff Shen 26 Dec 13, 2022
Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.

Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.

Davy Duperron 224 Dec 21, 2022