# Recursion in ASP.NET MVC Razor engine

By Jean-Claude Colette Mar 14, 2016

Description:

We explain how to use recursion in ASP.NET MVC Razor engine with examples. Recursion is a feature of the C# language on the .NET platform available in Razor

## Introduction

Razor can create recursive functions, that is to say, functions calling themselves at least once.

We give several examples of recursive functions by trying to exploit the possibilities of Razor.

## Examples

### Recurrent sequence

The calculation of the terms of a recursive sequence can be
done very simply with a recursive function. Among the suites defined by
recurrent relations of the form: u (n) = f (u (n-1)) for n>=
1 include the factorial function. But I do not take this example, since
too few terms can be calculated without the use of multi-precision integers.
Consider instead the sequence `u` defined by:

u(0)=1 and for all n>=1, u(n)=(1+1/n/n)u(n-1)

Let's write a recursive function that calculates the `n`th
term of the sequence.

```
@functions{
public double u(int n)
{
if (n == 0)
{
return 1;
}
return (1.0 + 1.0 / n / n) * u(n - 1);
}
}
@Html.Raw(u(50))
```

The result is: 3.50116188372277

Now let's write a recursive function that returns the first n terms of this series. Why not call the previous function as many times as necessary? To recover the intermediate calculations made during the recursive calls corresponding to the first terms of the sequence. But this requires us to place these results in a list of real numbers.

```
@functions{
List<double> res = new List<double>();
public double u(int n)
{
if (n == 0)
{
res.Add(1);
return 1;
}
double x = (1.0 + 1.0 / n / n) * u(n - 1);
res.Add(x);
return x;
}
public string Display()
{
string s = "<ul>";
for (int i=0; i < res.Count; i++)
{
s += "<li> u(" + i + ")=" + Convert.ToString(res[i]) + "</li>";
}
s += "</ul>";
return s;
}
}
@u(20)
@Html.Raw(Display())
```

- u(0)=1
- u(1)=2
- u(2)=2.5
- u(3)=2.77777777777778
- u(4)=2.95138888888889
- u(5)=3.06944444444444
- u(6)=3.15470679012346
- u(7)=3.21908856135047
- u(8)=3.26938682012157
- u(9)=3.30974962036998
- u(10)=3.34284711657368
- u(11)=3.37047395224784
- u(12)=3.39388002136068
- u(13)=3.41396215166459
- u(14)=3.43138032590777
- u(15)=3.44663090513403
- u(16)=3.46009430710721
- u(17)=3.47206695176848
- u(18)=3.48278320779246
- u(19)=3.49243080670601
- u(20)=3.50116188372277

### Tower of Hanoi

It's a classic game of puzzle. The game consists of three towers (pegs) A, B and C and a number n of rings of different sizes. Early in the game, the rings are stacked from the largest to the smallest around the first tower. The goal is to move all the rings from the tower A to tower B with the same provision.

But the rings should be moved one after the other from tower to tower in the following rule:

It is forbidden to place a ring above a smaller ring.

Solving the problem with recursion is based on the following principle:

If we know move n-1 rings from the tower A to tower B then to move n rings of the tower A to tower B, just move the n-1 rings from the top to the tower C, move more large ring remaining on the tower A to tower B, then move the n-1 rings of the tower C to B. Finally, to stop this descent following the integer n, it suffices to treat the case of a single ring on the tower A and move it to the tower B. Let the program:

```
@functions{
int[][] t = new int[3][];
int[] above = new int[8];
int pwidth = 3 + 1;
int pheight = 8;
string s = "";
public void Init()
{
t[0] = new int[] { 3, 2, 1, 0, 0, 0, 0, 0 };
t[1] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
t[2] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
above[0] = 3;
above[1] = 0;
above[2] = 0;
s = "";
}
public void AddTable()
{
s += "<table class="table">\r\n";
for (int j = pheight - 1; j >= 0; j--)
{
s += "<tr>";
for (int i = 0; i < 3; i++)
{
int n = t[i][j]+1;
for (int k = 0; k < pwidth-n; k++)
{
s += "<td style=\"background-color:Lavender\"> </td>";
}
for (int k = 0; k < 2*n-1; k++)
{
if (n == 1)
{
s += "<td style=\"background-color:brown\"> </td>";
}
else
{
s += "<td style=\"background-color:blue\"> </td>";
}
}
for (int k = 0; k < pwidth - n; k++)
{
s += "<td style=\"background-color:Lavender\"> </td>";
}
s += "<td style=\"background-color:Lavender\"> </td>";
}
s += "</tr>";
}
s += "</table><br />";
}
public void Hanoi(int num, char fromt, char tot, char auxt)
{
int i = 0;
int a = 0;
if (num == 1)
{
s = s + "Move disk 1 from tower " + fromt + " to tower " + tot + "<br />";
i = fromt - 'A';
above[i]--;
a = t[i][above[i]];
t[i][above[i]] = 0;
i = tot - 'A';
t[i][above[i]] = a;
above[i]++;
AddTable();
return;
}
Hanoi(num - 1, fromt, auxt, tot);
s+="Move disk " + num + " from tower " + fromt + " to tower " + tot + "<br />";
i = fromt - 'A';
above[i]--;
a = t[i][above[i]];
t[i][above[i]] = 0;
i = tot - 'A';
t[i][above[i]] = a;
above[i]++;
AddTable();
Hanoi(num - 1, auxt, tot, fromt);
}
public string GetString()
{
return s;
}
}
@{
Init();
AddTable();
Hanoi(3, 'A', 'B', 'C');
}
@Html.Raw(GetString())
```

Move disk 1 from tower A to tower B

Move disk 2 from tower A to tower C

Move disk 1 from tower B to tower C

Move disk 3 from tower A to tower B

Move disk 1 from tower C to tower A

Move disk 2 from tower C to tower B

Move disk 1 from tower A to tower B