Skip to content

Instantly share code, notes, and snippets.

@wuxixigit
Last active October 10, 2021 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuxixigit/9f51f31cd4f7fb36a77d2bacea8132e9 to your computer and use it in GitHub Desktop.
Save wuxixigit/9f51f31cd4f7fb36a77d2bacea8132e9 to your computer and use it in GitHub Desktop.
Delphi code for calculating Pi.
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
Copy link

wxinix commented Oct 10, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment