12,754,391 members (35,931 online)
Tip/Trick
alternative version

#### Stats

9.6K views
2 bookmarked
Posted 3 Jul 2012

# How to use StringBuilder in recursive procedure in VB.NET.

, 3 Jul 2012 CPOL
 Rate this:

Here is a recursive procedure. This procedure will resolve the following algorithm using StringBuilder.

• Take any natural number n (excluding 0). If n is even, halve it (n / 2), otherwise multiply it by 3 and add 1 to obtain 3n + 1. The conjecture is that for all numbers this process converges to 1.
```Public Sub RecursiveCall(ByVal num As Integer, ByRef result As StringBuilder)

If num <= 0 Then
Exit Sub
End If

If result.Length = 0 Then
result.AppendFormat("{0} ", num)
End If

If num <> 1 Then
If num Mod 2 = 0 Then
num = num / 2
Else
num = 3 * num + 1
End If

RecursiveCall(num, result.AppendFormat("{0} ", num))
End If

Exit Sub

End Sub
```
call the recursive procedure.
```Dim result As StringBuilder = New StringBuilder()
Dim intInput As Integer = 10

RecursiveCall(intInput, result)
```
Alternatively, we can solve this issue
```Dim strSeries As String=String.Empty
While num <> 1
If num Mod 2 = 0 Then
num = num / 2
Else
num = 3 * num + 1
End If
strSeries =strSeries + num.ToString()
End While
```
But in this trick recursive procedure is used which the most powerful way to solve this issue and using Stringbuilder for efficient programming and improve performance.

## Share

No Biography provided

## You may also be interested in...

 Pro Pro

 First Prev Next
 [My vote of 2] Why StringBuilder? Why recursion? Matt T Heffron5-Jul-12 15:40 Matt T Heffron 5-Jul-12 15:40
 Re: [My vote of 2] Why StringBuilder? Why recursion? Abdul Quader Mamun13-Aug-12 1:05 Abdul Quader Mamun 13-Aug-12 1:05
 Re: [My vote of 2] Why StringBuilder? Why recursion? Matt T Heffron14-Aug-12 7:41 Matt T Heffron 14-Aug-12 7:41
 But to what Lengths will he go to fix it Abdul Quader Mamun3-Jul-12 18:34 Abdul Quader Mamun 3-Jul-12 18:34
 But to what Lengths will he go to fix it Abdul Quader Mamun3-Jul-12 18:33 Abdul Quader Mamun 3-Jul-12 18:33
 Last Visit: 31-Dec-99 19:00     Last Update: 22-Feb-17 15:14 Refresh 1