How to resolve the algorithm Pi step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pi step by step in the Swift 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 Swift programming language

Source code in the swift programming language

//
//  main.swift
//  pi digits
//
//  Created by max goren on 11/11/21.
//  Copyright © 2021 maxcodes. All rights reserved.
//

import Foundation

var r = [Int]()
var i = 0
var k = 2800
var b = 0
var c = 0
var d = 0

for _ in 0...2800 {
    r.append(2000);
}
while k > 0 {
    d = 0;
    i = k;
    while (true) {
        d = d + r[i] * 10000
        b = 2 * i - 1
        r[i] = d % b
        d = d / b
        i = i - 1
        if i == 0 {
            break;
        }
        d = d * i;
    }
    print(c +  d / 10000, "")
    c = d % 10000
    k = k - 14
}


  

You may also check:How to resolve the algorithm Generator/Exponential step by step in the Nim programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the AWK programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Quackery programming language
You may also check:How to resolve the algorithm Deconvolution/1D step by step in the Scala programming language
You may also check:How to resolve the algorithm Sphenic numbers step by step in the Go programming language