Module: Editor

Version: 4.2.1 +

User: Developer

Difficulty: Difficult

Introduction

This exercise teaches you how to create recursive functions.

Prerequisites

- The Futurama Editor must be installed.
- Completing the tutorial Creating formulas with Futurama first, is recommended.

## Description

A recursive function is a function that calls itself during its execution.
In this exercise we will create two recursive functions, the factorial function and the Fibonacci number.

## Assignment

Factorial

The factorial of the integer n, is the product of all positive integers less than or equal to n.
The factorial is denoted by n!

For example:

5! = 5 x 4 x 3 x 2 x 1 = 120

This function can be defined as:
When
n>0
Use the formula
n! =n x (n-1)!
If n=0
then use the fixed value
0! =1

Assignment 1: Create this formula in Futurama (Type Long).

Fibonacci number

The Fibonacci function will enable us to create the Fibonacci sequence of numbers.
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

This creates the sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

This function can be defined as:
f(n) = f(n-1) + f(n-2)
with fixed values
f(0) = 0
f(1) = 1

Assignment 2: Create this formula in Futurama (Type Long).

## Tips

• To create a recursive function in Futurama you have to create an argument, in which you refer to the function itself.
• Make sure your recursive function always has a limiting condition. Try to use the If function to indicate wheter to use a formula or a fixed value.
• For both formulas you only need one argument (for example n). When referring to your function to create the recursivity, fill the argument(s) with the formulas n-1 and n-2.

## Solutions

- Check your functions with the tables below

 n n! f(n) 0 1 0 1 1 1 2 2 1 3 6 2 4 24 3 5 120 5 6 720 8 7 5040 13 8 40.320 21 9 362.880 34 10 3.628.800 55

You can download our solution for this assignment by clicking the image at the right. (Futurama 4.2.1.2)
Please note that you can very well have a different solution that is just as good as ours.

## Variations

If you want some more practise on this subject, you could try the extra exercises below.
We won't provide a solution for these variations, just consider them as a way to further experiment with Futurama.

• (Medium:) Calculate the sum of the numbers 1 to 100, using a recursive function.

Updated: 2012-12-05