Hi. This page is about creating a function which would extract Fibonacci numbers in R.

(This function can also be coded in other programming languages such as Python and C++. )

**A Function Which Extracts A Fibonacci Number In R**

My main objective for this function is to have the function output the n-th Fibonacci number in a Fibonacci sequence.

Remember that the Fibonacci sequence starts with 1 and 1 as follows:

(An alternate version of the Fibonacci sequence has the sequence starting at 0 and 1.)

If , the fifth Fibonacci number in the above sequence would be 5.

Before introducing the Fibonacci function, I have included a brief review of functions in R, if-else statements in R and for-loops in R.

__Functions In R__

Recall that a function in R follows this format.

1 2 3 |
function_name <- function(arg1, arg2, ...) { <put code here> } |

To use the function, the function_name along with the argument(s) needs to be declared. An example of a function call would be something like:

1 |
function_name(arg1 = *, arg2 = *, ...) |

__If-Else Statements In R__

In this Fibonacci function, I use an if-else statement for dealing with cases. I want the user of the function to put in whole numbers that are at least 1.

In R, the if-else statement follows a format which looks something like this:

1 2 3 4 5 6 7 8 9 10 |
if (case_1) { < code here for case_1 > } else if (case_2) { < code here for case_2> } ... { } else if (case_[n -1]){ < code here for case_[n - 1]> } else #case_n < code here for case_n> |

__For Loops In R__

For loops is a compact way to repeatedly run lines of code while changing variable values. The for-loop format in R would look something like:

1 2 3 |
for (index_var in min:max){ <run code here> } |

A simple example of a for loop in printing numbers from 1 to 10.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
> for (i in 1:10){ + print(i) + } [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 |

__The Fibonacci Function In R__

The code for the Fibonacci function can be found below. The user puts in a single argument which should be a positive whole number (integer) that is at least 1 or greater. In the function, the if-else statement deals with cases for numbers less than 1 and for non-whole numbers (by making sure the fractional part of the number is 0).

Since the Fibonacci sequence relies on the previous two numbers in the sequence (with 1 and 1 as the first two numbers), a for loop is used here. The n-th Fibonacci number in the sequence is represented by fibonacci_vect[n] which is used in the paste0() string concatenation function.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# Fibonacci Sequence in R # 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, and so on. # A Function Which ouptuts the n-th Fibonacci number (n >= 1 & n is natural number): fibonacci_funct <- function(n) { if (n < 1){ print("Please choose a whole number that is 1 or greater") } else if (n == 1) { return(print("The first Fibonacci number is 1.")) } else if (n == 2) { return(print("The second Fibonacci number is 2.")) } else if ((n >= 3) && (n %% 1 == 0)){ fibonacci_vect = c() #Initalize empty vector # Initalize first two numbers in Fibonacci sequence: fibonacci_vect[1] <- 1 fibonacci_vect[2] <- 1 for (i in 3:n){ fibonacci_vect[i] = fibonacci_vect[i - 2] + fibonacci_vect[i - 1] } # Create output message with paste0() concatenation: msg <- paste0("Position: ", n, ", Fibonacci Number: ", fibonacci_vect[n]) return(msg) # Output n-th Fibonacci number as a message } else # Non-whole number case. print("Invalid number. Please enter in a whole number that is 1 or greater.") } |

**Test Cases**

To make sure that the function works and is error free, some testing is required. I want to test cases where the numbers in the function call are negative, 1, 2, and numbers with a fractional part (non-whole numbers).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
> # Test Cases: > > fibonacci_funct(2) [1] "The second Fibonacci number is 2." > > fibonacci_funct(-1) [1] "Please choose a whole number that is 1 or greater" > > fibonacci_funct(1) [1] "The first Fibonacci number is 1." > > fibonacci_funct(0) [1] "Please choose a whole number that is 1 or greater" > > fibonacci_funct(2.4) [1] "Invalid number. Please enter in a whole number that is 1 or greater." > > fibonacci_funct(3) [1] "Position: 3 , Fibonacci Number: 2" > > fibonacci_funct(6) [1] "Position: 6 , Fibonacci Number: 8" > > fibonacci_funct(10) [1] "Position: 10 , Fibonacci Number: 55" |

**References**

https://stackoverflow.com/questions/3476782/check-if-the-number-is-integer

https://www.programiz.com/r-programming/if-else-statement