How to resolve the algorithm Pi step by step in the PureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pi step by step in the PureBasic programming language
Table of Contents
Problem Statement
Create a program to continually calculate and output the next decimal digit of
π
{\displaystyle \pi }
(pi). The program should continue forever (until it is aborted by the user) calculating and outputting each decimal digit in succession. The output should be a decimal sequence beginning 3.14159265 ...
Note: this task is about calculating pi. For information on built-in pi constants see Real constants and functions.
Related Task Arithmetic-geometric mean/Calculate Pi
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pi step by step in the PureBasic programming language
Source code in the purebasic programming language
#SCALE = 10000
#ARRINT= 2000
Procedure Pi(Digits)
Protected First=#True, Text$
Protected Carry, i, j, sum
Dim Arr(Digits)
For i=0 To Digits
Arr(i)=#ARRINT
Next
i=Digits
While i>0
sum=0
j=i
While j>0
sum*j+#SCALE*arr(j)
Arr(j)=sum%(j*2-1)
sum/(j*2-1)
j-1
Wend
Text$ = RSet(Str(Carry+sum/#SCALE),4,"0")
If First
Text$ = ReplaceString(Text$,"3","3.")
First = #False
EndIf
Print(Text$)
Carry=sum%#SCALE
i-14
Wend
EndProcedure
If OpenConsole()
SetConsoleCtrlHandler_(?Ctrl,#True)
Pi(24*1024*1024)
EndIf
End
Ctrl:
PrintN(#CRLF$+"Ctrl-C was pressed")
End
You may also check:How to resolve the algorithm Floyd's triangle step by step in the Raku programming language
You may also check:How to resolve the algorithm Repeat step by step in the Ursa programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the C programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Genie programming language