BrainFuck part 11 - Iterative and recursive ways

DPAmar
8,395 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Welcome!

Welcome to this new playground about the BF language. Make sure you already read

playgrounds if you didn't already !

The goal of this playground is to compare different ways to implement recursion based on a very common example: factorial

It may be hard to understand how we can define a recursion in a language that does not even allow functions. But functions are just one way to implement recurion.

Actually the main difference between recursion and iteration is : in order to solve a given problem

  • the iterative way solves the problem, starting from a simple version and getting closer to the solution by incrementing the initial result
  • the recursive way solves the problem, defining the solution as a function of solutions to the same problem for smaller instances

It can be proven that both ways are equivalent, and that an algorithm that uses one of these can be re-implemented using the other one.

Factorial function

The factorial function ! is recursively defined by

  • factorial(0) = 0! = 1
  • factorial(n) = n! = n x factorial(n-1)

Let's see different ways to implement this function using BF

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content