Pi: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: added quicker unverified algo, added link to tio.run)
m (→‎Quicker, unverified algo: exchanged some multiplies for additions, increasing performance slightly)
Line 5,474: Line 5,474:
There seems to be another algorithm in the original reference article (see the [http://www.rosettacode.org/wiki/Pi#Ada Ada] entry), which produces output a bit faster. However, the math behind the algorithm has not been completely proven. It's faster because it doesn't calculate whether each digit is accumulated properly before squirting it out. When using (slow) arbitrary precision libraries, this avoids a lot of computation time.
There seems to be another algorithm in the original reference article (see the [http://www.rosettacode.org/wiki/Pi#Ada Ada] entry), which produces output a bit faster. However, the math behind the algorithm has not been completely proven. It's faster because it doesn't calculate whether each digit is accumulated properly before squirting it out. When using (slow) arbitrary precision libraries, this avoids a lot of computation time.
<lang vbnet>Imports System, System.Numerics, System.Text
<lang vbnet>Imports System, System.Numerics, System.Text

Module Program
Module Module1

Sub RunPiF(ByVal msg As String)
Sub RunPiF(ByVal msg As String)
If msg.Length > 0 Then Console.WriteLine(msg)
If msg.Length > 0 Then Console.WriteLine(msg)
Dim first As Boolean = True, stp As BigInteger = 360,
Dim first As Boolean = True, stp As Integer = 360,
lim As BigInteger = stp, res As StringBuilder = New StringBuilder(),
lim As Integer = stp,
rc As Integer = -1, u, j, k As BigInteger, q As BigInteger = 1,
res As StringBuilder = New StringBuilder(),
r As BigInteger = 180, t As BigInteger = 60, i As BigInteger = 2,
rc As Integer = -1, y As Byte, et As TimeSpan,
y As Byte, et As TimeSpan, st As DateTime = DateTime.Now
st As DateTime = DateTime.Now,
q, r, t, g, j, h, k, a, b As BigInteger

q = 1 : r = 180 : t = 60 : g = 60 : j = 54
While True
While i < lim
h = 10 : k = 10 : a = 15 : b = 3
While rc < 1000
j = i << 1 : k = j + i : u = 3 * (k + 1) * (k + 2)
While true
y = CByte(((q * (9 * k - 12) + 5 * r) / (5 * t)))
res.Append(y)
a += 27 : y = CByte((q * a + 5 * r) / (5 * t))
r = (q * (k + j - 2) + r - y * t) * u * 10
res.Append(y) : b += 5 : j += 54 : g += j
t *= u : q = 10 * q * (j - 1) * i : i += 1
r = 10 * g * (q * b + r - y * t)
k += 40 : h += k : q *= h : t *= g
End While
End While
If first Then res.Insert(1, "."c) : first = False
If first Then res.Insert(1, "."c) : first = False
Line 5,502: Line 5,503:
Console.WriteLine(vbLf & "Produced {0} digits in {1:n4} seconds.", rc, et.TotalSeconds)
Console.WriteLine(vbLf & "Produced {0} digits in {1:n4} seconds.", rc, et.TotalSeconds)
End Sub
End Sub

Sub Main(args As String())
Sub Main(args As String())
RunPiF("Press a key to exit...")
RunPiF("Press a key to exit...")