Click here to Skip to main content
Click here to Skip to main content

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

By , 3 Jul 2012
 

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abdul Quader Mamun
Founder
Bangladesh Bangladesh
Member
I have been developing software/web application since 2002 mainly on Microsoft technologies. I am a MCSD and MCTS. I have developed a wide range of Web, Desktop and Mobile applications.

I am vast experience with Telerik technologies.

I am also experience with other technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
General[My vote of 2] Why StringBuilder? Why recursion? [modified]memberMatt T Heffron5 Jul '12 - 14:40 
GeneralRe: [My vote of 2] Why StringBuilder? Why recursion?memberAbdul Quader Mamun13 Aug '12 - 0:05 
GeneralRe: [My vote of 2] Why StringBuilder? Why recursion?memberMatt T Heffron14 Aug '12 - 6:41 
GeneralBut to what Lengths will he go to fix itmemberAbdul Quader Mamun3 Jul '12 - 17:34 
QuestionBut to what Lengths will he go to fix itmemberAbdul Quader Mamun3 Jul '12 - 17:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 4 Jul 2012
Article Copyright 2012 by Abdul Quader Mamun
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid