Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Extension manager in VS2010Linq to nHibernate why you should always check the sql it produces. »
    comments

    In .Net 4.0 we now have a BigInteger to play with.

    This is how I used to do it as can be seen LTD Puzzle 5: Calculating the Fibonacci Sequence.

    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim x As Double
    5.         Console.WriteLine("Give the number to create")
    6.         Dim input As String
    7.         input = Console.ReadLine()
    8.         If Double.TryParse(input, x) AndAlso x > 2 Then
    9.             Dim y As Double
    10.             Dim oldz As Double
    11.             Dim z As Double = 0
    12.             Dim newz As Double = 1
    13.             Console.WriteLine(z & ",")
    14.             Console.WriteLine(newz & ",")
    15.             For y = 1 To x
    16.                 Try
    17.                     oldz = newz
    18.                     newz += z
    19.                     Console.WriteLine(y & ". " & newz & ",")
    20.                     z = oldz
    21.                 Catch ex As Exception
    22.                     Console.WriteLine("end reached")
    23.                     Exit For
    24.                 End Try
    25.             Next
    26.         End If
    27.         Console.ReadLine()
    28.     End Sub
    29.  
    30. End Module

    Which runs out of bytes at around 1475 when using the double.

    There is a way to do this with string if you don't have .Net 4.0.

    1. Imports System.Numerics
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Dim x As Double
    7.         Console.WriteLine("Give the number to create")
    8.         Dim input As String
    9.         input = Console.ReadLine()
    10.         If Double.TryParse(input, x) AndAlso x > 2 Then
    11.             Dim y As BigInteger
    12.             Dim oldz As BigInteger
    13.             Dim z As BigInteger = 0
    14.             Dim newz As BigInteger = 1
    15.             Console.WriteLine(z.ToString & ",")
    16.             Console.WriteLine(newz.ToString & ",")
    17.             For y = 1 To CType(x, BigInteger)
    18.                 Try
    19.                     oldz = newz
    20.                     newz += z
    21.                     Console.WriteLine(y.ToString & ". " & newz.ToString & ",")
    22.                     z = oldz
    23.                 Catch ex As Exception
    24.                     Console.WriteLine("end reached")
    25.                     Exit For
    26.                 End Try
    27.             Next
    28.         End If
    29.         Console.ReadLine()
    30.     End Sub
    31.  
    32. End Module

    To make the above work I had to reference System.Numerics and do an Imports. And I had to add a few ToStrings to.

    I tried it with 10000 which gives this as a result.

    5443837311356528133873426099375038013538918455469596702624771584120858286
    56223490170830515479389605411738226759780263173843595847511162414391747026429591
    69925586334117906063048089793531476108466259072759367899150677960088306597966641
    96582493772180038144115884104248099798469648737533718002816376331778192794110136
    92627509795098007135967180238147106699126442147752544785876745689638080029622651
    33111359929762726679441400101575800043510777465935805362502461707918059226414679
    00569075232189586814236784959388075642348375438634263963597073375626009896246266
    87461120417398194048750624437098686543156268471861956201461266422327118150403670
    18825205314845875817193533529827837800351902529239517836689467661917953884712441
    02846393544948461445077876252952096188759727288922076853739647586954315917243453
    71936112637439263373130058961672480517379863063681150030883967495871026195246313
    52447499505204198305187168321623283859794627245919771454628218399695789223798912
    19943177546970521613108109655995063829726125384824200789710905475402843814961193
    04650618661701229832889643527337507927860694447618535251444210779280459799045612
    98129423809156055033032338919609162236698759922782923191896688017718575555520994
    65332012844650237115371514174929091310489720345557750719664542523286202201950609
    14835852238827110167084330511699421157751512555102516559318881640483441295570388
    25477521111577395780115868397072602565614824956460538700280331311861485399805397
    03155572752969339958607985038158144627643385882852953580342485084542644647168153
    10015331804795674363968156533261525095711274804119281960221488491482843891241785
    20174507305538928717857923509417743383331506898239354421988805429332440371194867
    21554357654856549913451927109891980266518456492782782721295764924023550759555820
    56475693653948733176590002063731265706435097094826497100387335174777134033190281
    05575667931789470024118803094604034362953471997461392274791549730356412633074230
    82405199999610154978466734045832685296038830112076562924599813625165234709396304
    97340464451063653041636308236692422577614682884617918432247934344060799178833606
    76846711185597501

    I won't even try to read that. And it's pretty darn quick too. I guess you could easily go higher. I guess the sky is the limit.

    Here is the version using strings as made by George Mastros.

    1. Sub Main()
    2.         Dim Number As Long
    3.         Console.WriteLine("Give the number to create")
    4.         Dim input As String
    5.         input = Console.ReadLine()
    6.  
    7.         If Not IsNumeric(input & ".0e0") Then
    8.             Console.WriteLine("The number to calculate must be a valid integer greater than 2.")
    9.             Exit Sub
    10.         End If
    11.  
    12.         Number = CLng(input)
    13.  
    14.         FibonacciSequence(Number)
    15.         Console.ReadLine()
    16.         End Sub
    17.  
    18.     Private Sub FibonacciSequence(ByVal NumberToCalculate As Long)
    19.  
    20.         Dim arTemp() As String
    21.  
    22.         Dim i As Long
    23.  
    24.         ReDim arTemp(CInt(NumberToCalculate - 1))
    25.  
    26.         arTemp(0) = "0"
    27.         arTemp(1) = "1"
    28.  
    29.         For i = 2 To NumberToCalculate - 1
    30.             arTemp(CInt(i)) = AddString(arTemp(CInt(i - 2)), arTemp(CInt(i - 1)))
    31.         Next i
    32.  
    33.         Console.WriteLine(Join(arTemp, ","))
    34.  
    35.     End Sub
    36.  
    37.     Private Function AddString(ByVal String1 As String, ByVal String2 As String) As String
    38.  
    39.         Dim i As Long
    40.         Dim Output() As String
    41.         Dim CarryTheOne As Long
    42.         Dim Digit1 As Long
    43.         Dim Digit2 As Long
    44.  
    45.         If Len(String1) < Len(String2) Then
    46.             String1 = Replace(Right(Space(Len(String2)) & String1, Len(String2)), " ", "0")
    47.         Else
    48.             String2 = Replace(Right(Space(Len(String1)) & String2, Len(String1)), " ", "0")
    49.         End If
    50.  
    51.         ReDim Output(Len(String1))
    52.         CarryTheOne = 0
    53.         For i = Len(String1) To 1 Step -1
    54.             Digit1 = CLng(Mid(String1, CInt(i), 1))
    55.             Digit2 = CLng(Mid(String2, CInt(i), 1))
    56.  
    57.             Output(CInt(i)) = CStr((Digit1 + Digit2 + CarryTheOne) Mod 10)
    58.             If Digit1 + Digit2 + CarryTheOne > 9 Then
    59.                 CarryTheOne = 1
    60.             Else
    61.                 CarryTheOne = 0
    62.             End If
    63.         Next
    64.  
    65.         If CarryTheOne = 1 Then
    66.             Output(0) = "1"
    67.         End If
    68.         AddString = Join(Output, "")
    69.  
    70.     End Function

    About the Author

    User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
    Social SitingsTwitterLinkedInHomePageLTD RSS Feed
    Instapaper

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)