Discovering pi with freepascal

Discovering pi with freepascal

Postby Guest » Tue Dec 13, 2022 12:02 pm

I was happy to find BigDecimal math library for freepascal compiler: https://benibela.de/sources_en.html#bigdecimalmath It is is easy to use. Just put bigdecimalmath.pas to same directory where your program is. Freepascal compiler can be downloaded here: https://www.freepascal.org/

I counted thousand decimals of pi with this program:
Code: Select all
program ThousandPi;
    uses bigdecimalmath, sysutils,DateUtils;
    const accuracy = 1000;
            accuracyfactor = 2;
    type strindeksi = 1..accuracy;
    var pi, pihelp1, pihelp2,five,number239,four,one,ind,two,divider: BigDecimal;
            str: array [1..accuracy] of char;
            time1,time2:Double;
    function potency(a,n: BigDecimal):BigDecimal; // a^n
    var output,i: BigDecimal;
    begin
            output:=1;
            i:=1;
            while i<=n do begin
                    output:=output*a;
                    i:=i+1;
            end;
            potency:=output;
    end;   
    begin
            time1:=Now;
            pi:= 0;
            ind:= 1;
            pihelp1:=0;
            pihelp2:=0;
            five:=5;
            number239:=239;
            four:=4;
            one:=1;
            two:=2;
            while ind <= (accuracy*2) do
            begin
                    divider:=ind*(potency(five,ind));
                    pihelp1:= pihelp1+(divide(four, divider,accuracyfactor*accuracy));
                    divider:=(ind*(potency(number239,ind)));
                    pihelp2:= pihelp2-(divide(one, divider,accuracyfactor*accuracy));
                    ind:=ind+two;
                    divider:=(ind*(potency(five,ind)));
                    pihelp1:=pihelp1-(divide(four, divider,accuracyfactor*accuracy));
                    divider:=(ind*(potency(number239,ind)));
                    pihelp2:=pihelp2+(divide(one, divider,accuracyfactor*accuracy));
                    pi:=four*(pihelp1+pihelp2);
                    ind:=ind+two;
            end;
            time2:=Now;
            Writeln((time2-time1)*86400,' second.');
            str:=BigDecimalToStr(pi);
            writeln(str);
    end.

It use quite effective John Mach's serie:
Image
but my program is not so effective :( It counts thousand decimals in minute, but it took too long to count ten thousand. I made a graph of time requirements, and the curve raises very fast:
Image
If I try to count N decimals I count numbers in accuracy of 2*N and take 2*N number from John Mach's serie. Maybe it is too much?

Because my program was too slow I downloaded 1 billion decimals: [url]https://pi2e.ch/blog/2017/03/10/pi-digi … /#download[/url] and made experiments. First I wanted to know the longest chain of the same numbers. Among the first billion, there is only one chain of nine consecutive identical numbers.

Decimals from 386 980 425 to 386 980 433 are 6 so there are nine of them 666666666. I done my discovery with this program:
Code: Select all
program DiscoveryPi;
    uses Sysutils;
    var file1 : File of Char;
            digit: char;
            trythis : char;
            longest :       word;
            amount,indexi   : LongInt;
    begin
      Assign (file1,'PI.txt'); //There is billion decimals in this file
      Reset (file1);
      longest:=0;
      indexi:=1;
      While not Eof(file1) do
            begin
                    read(file1,digit);
                    indexi:=indexi+1;
                    trythis:=digit;
                    amount:=0;
                    while (trythis=digit) and (not Eof(file1)) do
                            begin
                                    amount:=amount+1;
                                    read(file1,digit);
                                    indexi:=indexi+1;
                            end;
                    if amount>=longest then
                            begin
                                    longest:=amount;
                                    writeln(indexi,'-',indexi+amount-1,' ',trythis,'   ',amount);
                            end;           
            end;
    end.

Then I wanted to know what is the longest sequence of consecutive numbers. I found that decimals between 956753757-956753765 are 012345678.

And thirdly I wanted know how long pi there is inside the pi. I found that there is twelve places where are eight numbers 31415926 among first billion digits of pi.

And last I discoveried the stats of different numbers:

0 99993942
1 99997334
2 100002410
3 99986912
4 100011958
5 99998885
6 100010387
7 99996061
8 100001839
9 100000273

Number 4 is the 'winner' and 3 is the 'loser'.

Useless discoveries, but fun to program! :)

I'm even now downloading 100 billion decimals...So let's see what I will found :)
Guest
 

Return to Programming and Algorithms



Who is online

Users browsing this forum: No registered users and 5 guests

cron