{{ message }}

Instantly share code, notes, and snippets.

# wuxixigit/Delphi.CalculatePi.pas

Last active Oct 10, 2021
Delphi code for calculating Pi.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 program Delphi.CalculatePi; uses Windows; {\$APPTYPE CONSOLE} const N = 50000; ArrayLen = 10 * N div 3; var J, K, Q, Nines, PreDigit: Integer; PiArray: array[0..ArrayLen] of LongInt; StartTime, EndTime, Frequency: Int64; function OneLoop(aIndex: Integer): Integer; var x: Integer; begin aIndex := aIndex * 10 div 3 + 16; if aIndex > ArrayLen then aIndex := ArrayLen; Result := 0; repeat x := 10 * PiArray[aIndex] + Result * aIndex; Result := x div (2 * aIndex - 1); PiArray[aIndex] := x - Result * (2 * aIndex - 1); Dec(aIndex); until aIndex<= 0 ; end; begin QueryPerformanceFrequency(Frequency); QueryPerformanceCounter(StartTime); for J := 1 to ArrayLen do PiArray[J] := 2; PreDigit := 0; Nines := 0; for J := 1 to N do begin Q := OneLoop(N - J); PiArray[1] := Q mod 10; Q := Q div 10; if Q = 9 then Nines := Nines + 1 else if Q = 10 then begin Write(PreDigit+1); for K := 1 to Nines do Write(0); {zeros} PreDigit := 0; Nines := 0 end else begin Write(PreDigit); PreDigit := Q; if Nines <> 0 then begin for K := 1 to Nines do Write(9); Nines := 0 end end end; Writeln(PreDigit); QueryPerformanceCounter(EndTime); Writeln(#13#10 + 'Delphi takes time: ', (EndTime - StartTime)/Frequency); ReadLn; end.

### wxinix commented Oct 10, 2021

When testing, need to comment out those output line, e..g, Write(PreDigit)...