Back
Close

BrainFuck part 11 - Iterative and recursive ways

DPAmar
8,015 views

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

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io