57 lines
2.2 KiB
Plaintext
57 lines
2.2 KiB
Plaintext
Imports System
|
|
Imports System.Collections.Generic
|
|
Imports System.Numerics
|
|
|
|
Module Module1
|
|
|
|
' A sparse array of values calculated along the way
|
|
Dim sl As SortedList(Of Integer, BigInteger) = New SortedList(Of Integer, BigInteger)()
|
|
|
|
' Square a BigInteger
|
|
Function sqr(ByVal n As BigInteger) As BigInteger
|
|
Return n * n
|
|
End Function
|
|
|
|
' Helper routine for Fsl(). It adds an entry to the sorted list when necessary
|
|
Sub IfNec(n as integer)
|
|
If Not sl.ContainsKey(n) Then sl.Add(n, Fsl(n))
|
|
End Sub
|
|
|
|
' This routine is semi-recursive, but doesn't need to evaluate every number up to n.
|
|
' Algorithm from here: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html#section3
|
|
Function Fsl(ByVal n As Integer) As BigInteger
|
|
If n < 2 Then Return n
|
|
Dim n2 As Integer = n >> 1, pm As Integer = n2 + ((n And 1) << 1) - 1 : IfNec(n2) : IfNec(pm)
|
|
Return If(n2 > pm, (2 * sl(pm) + sl(n2)) * sl(n2), sqr(sl(n2)) + sqr(sl(pm)))
|
|
End Function
|
|
|
|
' Conventional iteration method (not used here)
|
|
Function Fm(ByVal n As BigInteger) As BigInteger
|
|
If n < 2 Then Return n
|
|
Dim cur As BigInteger = 0, pre As BigInteger = 1
|
|
For i As Integer = 0 To n - 1
|
|
Dim sum As BigInteger = cur + pre
|
|
pre = cur : cur = sum
|
|
Next : Return cur
|
|
End Function
|
|
|
|
Sub Main()
|
|
Dim num As Integer = 2_000_000
|
|
Dim st As DateTime = DateTime.Now
|
|
Dim v As BigInteger = Fsl(num)
|
|
Console.WriteLine("{0:n3} ms to calculate the {1:n0}th Fibonacci number,",
|
|
(DateTime.Now - st).TotalMilliseconds, num)
|
|
st = DateTime.Now
|
|
Dim vs As String = v.ToString()
|
|
Console.WriteLine("{0:n3} seconds to convert to a string.", (DateTime.Now - st).TotalSeconds)
|
|
Console.WriteLine("number of digits is {0}", vs.Length)
|
|
If vs.Length < 10000 Then
|
|
st = DateTime.Now
|
|
Console.WriteLine(vs)
|
|
Console.WriteLine("{0:n3} ms to write it to the console.", (DateTime.Now - st).TotalMilliseconds)
|
|
Else
|
|
Console.WriteLine("partial: {0}...{1}", vs.Substring(1, 35), vs.Substring(vs.Length - 35))
|
|
End If
|
|
End Sub
|
|
End Module
|