...

/

Fibonacci Numbers—Recursive Approach

Fibonacci Numbers—Recursive Approach

Let's solve the Fibonacci numbers using the recursive approach with memoization.

Origins of recursion

One of the earliest examples of recursion arose in India more than 2000 years ago, in the study of poetic meter or prosody. Classical Sanskrit poetry distinguishes between two types of syllables (aksara): light (laghu) and heavy (guru). In one class of meters, variously called matravr.ttam\overset{-}{a}tr\overset{-}{a}v\underset{.}{r}tta or matrachandasm\overset{-}{a}tr\overset{-}{a}chandas, each line of poetry consists of a fixed number of “beats” (matram\overset{-}{a}tr\overset{-}{a}), where each light syllable lasts one beat, and each heavy syllable lasts two beats. The formal study of matravr.ttam\overset{-}{a}tr\overset{-}{a}-v\underset{.}{r}tta dates back to the Chandah.sˊastraChanda\underset{.}{h}ś\overset{-}{a}stra, written by the scholar Pin.galaPi\overset{.}{n}gala between 600 BCE and 200 BCE. Pin.galaPi\overset{.}{n}gala observed that there are exactly five 4-beat meters: ——, — • •, • — •, • •—, and • • • •. (Here, each “—” represents a long syllable, and each “•” represents a short syllable.)

Although Pin.galasPi\overset{.}{n}gala’s text hints at a systematic rule for counting meters with a given number of beats, it took about a millennium for that rule to be stated explicitly. In the 7th-century CE, another Indian scholar named Virahan.kaVirah\overset{-}{a}\underset{.}{n}ka wrote a commentary on Pin.galasPi\overset{.}{n}gala’s work, in which he observed that the number of meters with nn beats is the sum of the number of meters with (n2)(n − 2) beats and the number of meters with (n1)(n − 1) beats. In more modern notation, Virahan.kasVirah\overset{-}{a}\underset{.}{n}ka’s ...