Sequence Generation – Recursive

Problem

Given the following sequence of integer numbers:

Write a recursive function that receives the index of a given number in the sequence above and returns the corresponding sequence number for example if index = 2 the function should return 2, if the index = 5 the function should return 8 and so on

Solution

This is the same problem that we solved before but now we are going to solve it recursively. The base case is going to be the same as that in the iterative solution which is index = 0 or index = 1 in that case we just return 1 otherwise we simply return the sum of the previous two elements. Please refer to the code below for more information

Code

Here is the code in Perl

Add a Comment

Your email address will not be published. Required fields are marked *