Jump to content

Fibonacci sequence: Difference between revisions

m
m (→‎Arbitrary Precision: accidently omitted line in last edit)
m (→‎BigInteger, speedier method: streamlined code)
Line 10,397:
This method doesn't need to iterate the entire list, and is much faster. The 2000000 (two millionth) Fibonacci number can be found in a fraction of a second.<br/>Algorithm from [http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html here, see section 3, ''Finding Fibonacci Numbers Fully''.]
<lang vbnet>Imports System
Imports System.Collections.Generic
Imports System.Numerics
 
Module Module1
 
' A sparse array of values calculated along the way
' Square a BigInteger
Dim sl As Function sqrSortedList(ByValOf n AsInteger, BigInteger) As= New SortedList(Of Integer, BigInteger)()
Return n * n
End Function
 
' Square a BigInteger
' A sparse array of values calculated along the way
Function sqr(ByVal Dim sln As SortedList(Of Integer, BigInteger) = New SortedList(Of Integer,As BigInteger)()
Return n * n
End Function
 
' ThisHelper routine callsfor itselfFsl(). asIt needed,adds butan doesn't needentry to evaluatethe everysorted numberlist upwhen to n.necessary
Sub IfNec(n as integer)
' Algorithm from here: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html#section3
If IfNot sl.IndexOfKeyContainsKey(n2n) < 0 Then sl.Add(n2n, Fsl(n2n))
Function Fsl(ByVal n As Integer) As BigInteger
End IfSub
If n < 2 Then Return n
 
Dim n2 as Integer = n >> 1
' This routine is semi-recursive, but doesn't need to evaluate every number up to n.
If (n And 1) <> 1 Then
' Algorithm from here: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html#section3
If sl.IndexOfKey(n2) < 0 Then sl.Add(n2, Fsl(n2))
Function Fsl(ByVal n As Integer) As BigInteger
If sl.IndexOfKey(n2 - 1) < 0 Then sl.Add(n2 - 1, Fsl(n2 - 1))
If n < 2 Then Return n
Return ((2 * sl.ElementAt(sl.IndexOfKey(n2 - 1)).Value) + sl.ElementAt(sl.IndexOfKey(n2)).Value) *
Dim n2 As Integer = n >> 1, pm As Integer = n2 + ((n And 1) << 1) - 1 sl.ElementAt(sl.IndexOfKey: IfNec(n2) : IfNec(pm).Value
Return If(n2 > pm, (2 * sl(pm) + sl(n2)) * sl(n2), sqr(sl(n2)) + sqr(sl(pm)))
Else
End Function
If sl.IndexOfKey(n2) < 0 Then sl.Add(n2, Fsl(n2))
If sl.IndexOfKey(n2 + 1) < 0 Then sl.Add(n2 + 1, Fsl(n2 + 1))
Return sqr(sl.ElementAt(sl.IndexOfKey(n2)).Value) + sqr(sl.ElementAt(sl.IndexOfKey(n2 + 1)).Value)
End If
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 Nextcur
End Function
Return cur
End Function
 
Sub Main()
Dim n2num asAs Integer = n >> 12_000_000
Dim st As DateTime = DateTime.Now
Dim v As BigInteger = Fsl(2_000_000num)
Console.WriteLine("{0:n3} ms to calculate it,",the (DateTime.Now{1:n0}th -Fibonacci st).TotalMilliseconds)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)
' the following is executed conditionally due to the large size
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</lang>
{{out}}
<pre>188177.536831 ms to calculate itthe 2,000,000th Fibonacci number,
224.008649 seconds to convert to a string.
number of digits is 417975</pre>
partial: 53129491750764154305166065450382516...91799493108960825129188777803453125
</pre>
 
=={{header|Wart}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.