How to resolve the algorithm Pi step by step in the Seed7 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pi step by step in the Seed7 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 Seed7 programming language
Source code in the seed7 programming language
$ include "seed7_05.s7i";
include "bigint.s7i";
const proc: main is func
local
var bigInteger: q is 1_;
var bigInteger: r is 0_;
var bigInteger: t is 1_;
var bigInteger: k is 1_;
var bigInteger: n is 3_;
var bigInteger: l is 3_;
var bigInteger: nn is 0_;
var bigInteger: nr is 0_;
var boolean: first is TRUE;
begin
while TRUE do
if 4_ * q + r - t < n * t then
write(n);
if first then
write(".");
first := FALSE;
end if;
nr := 10_ * (r - n * t);
n := 10_ * (3_ * q + r) div t - 10_ * n;
q *:= 10_;
r := nr;
flush(OUT);
else
nr := (2_ * q + r) * l;
nn := (q * (7_ * k + 2_) + r * l) div (t * l);
q *:= k;
t *:= l;
l +:= 2_;
incr(k);
n := nn;
r := nr;
end if;
end while;
end func;
You may also check:How to resolve the algorithm Empty program step by step in the Sidef programming language
You may also check:How to resolve the algorithm Permutations step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Count in octal step by step in the zig programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the J programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Scheme programming language