Jump to content

Pi: Difference between revisions

19 bytes removed ,  3 years ago
m
→‎Quicker, unverified algo: exchanged some multiplies for additions, increasing performance slightly
(→‎{{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:
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
 
Module ProgramModule1
 
Sub RunPiF(ByVal msg As String)
If msg.Length > 0 Then Console.WriteLine(msg)
Dim first As Boolean = True, stp As BigIntegerInteger = 360,
lim As BigIntegerInteger = stp, res As StringBuilder = New StringBuilder(),
rcres As IntegerStringBuilder = -1,New u, j, k As BigInteger, q As BigInteger = 1StringBuilder(),
rrc As BigIntegerInteger = 180-1, ty As BigInteger = 60Byte, iet As BigInteger = 2TimeSpan,
y As Byte, et As TimeSpan, st As DateTime = DateTime.Now,
q, r, t, g, j, h, k, a, b As BigInteger
 
jq = i << 1 : kr = j + i180 : ut = 360 *: (kg += 1)60 *: (kj += 2)54
While True
Whileh i= <10 lim: k = 10 : a = 15 : b = 3
While rc < 1000
j = i << 1 : k = j + i : u = 3 * (k + 1) * (k + 2)
While Truetrue
y = CByte(((q * (9 * k - 12) + 5 * r) / (5 * t)))
res.Append(a += 27 : y = CByte((q * a + 5 * r) / (5 * t))
r = res.Append(qy) *: (kb += j5 -: 2)j += r54 -: yg *+= t) * u * 10j
t *= u : qr = 10 * qg * (jq -* 1)b *+ ir :- iy +=* 1t)
yk += CByte(((q40 *: (9h *+= k -: 12) + 5q *= r)h /: (5t *= t)))g
End While
If first Then res.Insert(1, "."c) : first = False
Line 5,502 ⟶ 5,503:
Console.WriteLine(vbLf & "Produced {0} digits in {1:n4} seconds.", rc, et.TotalSeconds)
End Sub
 
Sub Main(args As String())
RunPiF("Press a key to exit...")
Cookies help us deliver our services. By using our services, you agree to our use of cookies.