Petr Faltus development

PHP calculating Pi

Petr Faltus development

Example source code in PHP how to calculate Ludolphs number Pi. Three different infinite series.

tPi.php

GitHub repository: php-calculating-pi

git clone https://github.com/petrfaltus/php-calculating-pi.git

Directories to tPi.php in the repository: class

<?php

abstract class tPi
{
 const DESCRIPTION = "?";

 protected $precision_digits;

 protected $pi = "?";
 protected $calculation_cycles = 0;
 protected $calculation_time = 0;

 //----------------------------------------------------------------------------
 public function get_pi()
 {
  return $this->pi;
 }
 //----------------------------------------------------------------------------
 public function print_report()
 {
  echo self::DESCRIPTION.PHP_EOL;
  echo PHP_EOL;

  echo $this->pi.PHP_EOL;
  echo PHP_EOL;

  echo "Desetinnych mist: ".$this->precision_digits.PHP_EOL;
  echo "Vypocetnich cyklu: ".$this->calculation_cycles.PHP_EOL;
  echo "Delka vypoctu: ".$this->calculation_time."s".PHP_EOL;
  echo PHP_EOL;

  return;
 }
 //----------------------------------------------------------------------------

 //----------------------------------------------------------------------------
 function __construct($precision_digits)
 {
  $this->precision_digits = $precision_digits;

  return;
 }
 //----------------------------------------------------------------------------
}

?>

tPi1.php

Directories to tPi1.php in the repository: class

<?php

class tPi1 extends tPi
{
 const DESCRIPTION = "Program pro vypocet Pi = 4*( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... )";

 //----------------------------------------------------------------------------
 public function calculate()
 {
  $scale = $this->precision_digits + 2;
  bcscale($scale);

  $polarity = 1;
  $this->calculation_cycles = 0;

  $result = 0;

  $start_time = gettimeofday(true); // calculation time measurent

  do
    {
     $denominator = 2 * $this->calculation_cycles + 1; // 1, 3, 5, 7, 9, ...
     $fraction = bcdiv(1, $denominator);
     $contribution = bcmul($polarity, $fraction);

     $result = bcadd($result, $contribution);

     $polarity *= -1;
     $this->calculation_cycles++;
    }
  while (bccomp($contribution, 0));

  $this->pi = bcmul(4, $result, $this->precision_digits);

  $finish_time = gettimeofday(true); // calculation time measurent
  $this->calculation_time = $finish_time - $start_time;

  return;
 }
 //----------------------------------------------------------------------------
}

?>

tPi2.php

Directories to tPi2.php in the repository: class

<?php

class tPi2 extends tPi
{
 const DESCRIPTION = "Program pro vypocet Pi = 4*( (1/2+1/3) - (1/2^3+1/3^3)/3 + (1/2^5+1/3^5)/5 - (1/2^7+1/3^7)/7 + ... )";

 //----------------------------------------------------------------------------
 public function calculate()
 {
  $scale = $this->precision_digits + 2;
  bcscale($scale);

  $fraction_one = bcdiv(1, 2); // 1/2
  $fraction_two = bcdiv(1, 3); // 1/3
  $polarity = 1;
  $this->calculation_cycles = 0;

  $result = 0;

  $start_time = gettimeofday(true); // calculation time measurent

  do
    {
     $numerator = bcadd($fraction_one, $fraction_two); // 1/2^N+1/3^N
     $denominator = 2 * $this->calculation_cycles + 1; // 1, 3, 5, 7, 9, ...
     $fraction = bcdiv($numerator, $denominator);
     $contribution = bcmul($polarity, $fraction);

     $result = bcadd($result, $contribution);

     $fraction_one = bcdiv($fraction_one, 4); // *= 1/2
     $fraction_two = bcdiv($fraction_two, 9); // *= 1/3
     $polarity *= -1;
     $this->calculation_cycles++;
    }
  while (bccomp($contribution, 0));

  $this->pi = bcmul(4, $result, $this->precision_digits);

  $finish_time = gettimeofday(true); // calculation time measurent
  $this->calculation_time = $finish_time - $start_time;

  return;
 }
 //----------------------------------------------------------------------------
}

?>

tPi3.php

Directories to tPi3.php in the repository: class

<?php

class tPi3 extends tPi
{
 const DESCRIPTION = "Program pro vypocet Pi = 4*( (4/5-1/239) - (4/5^3-1/239^3)/3 + (4/5^5-1/239^5)/5 - (4/5^7-1/239^7)/7 + ... )";

 //----------------------------------------------------------------------------
 public function calculate()
 {
  $scale = $this->precision_digits + 2;
  bcscale($scale);

  $fraction_one = bcdiv(4, 5); // 4/5
  $fraction_two = bcdiv(1, 239); // 1/239
  $polarity = 1;
  $this->calculation_cycles = 0;

  $result = 0;

  $start_time = gettimeofday(true); // calculation time measurent

  do
    {
     $numerator = bcsub($fraction_one, $fraction_two); // 4/5^N+1/239^N
     $denominator = 2 * $this->calculation_cycles + 1; // 1, 3, 5, 7, 9, ...
     $fraction = bcdiv($numerator, $denominator);
     $contribution = bcmul($polarity, $fraction);

     $result = bcadd($result, $contribution);

     $fraction_one = bcdiv($fraction_one, 25); // *= 1/5
     $fraction_two = bcdiv($fraction_two, 57121); // *= 1/239
     $polarity *= -1;
     $this->calculation_cycles++;
    }
  while (bccomp($contribution, 0));

  $this->pi = bcmul(4, $result, $this->precision_digits);

  $finish_time = gettimeofday(true); // calculation time measurent
  $this->calculation_time = $finish_time - $start_time;

  return;
 }
 //----------------------------------------------------------------------------
}

?>

do.php

#!/usr/bin/php
<?php

const BCMATH_EXTENSION = "bcmath";
const PATH_TO_CLASSES = "class";

# test the required extension
if (!extension_loaded(BCMATH_EXTENSION))
  {
   echo "ERROR, the ".BCMATH_EXTENSION." extension is not present.".PHP_EOL;
   echo "- uncomment or add the line 'extension=".BCMATH_EXTENSION."' under the '[PHP]' section in your php.ini file, please".PHP_EOL;

   exit;
  }

# register my classes auto loading
spl_autoload_register(function ($class_name)
{
 $class_source_file_path = PATH_TO_CLASSES."/".$class_name.".php";
 if (is_file($class_source_file_path))
   {
    require_once $class_source_file_path;
   }
});

echo "PHP version: ".phpversion().PHP_EOL;
echo PHP_EOL;

/*
$precision_digits = 6;

# the algorithm 1
$pi1 = new tPi1($precision_digits);
$pi1->calculate();
$pi1->print_report();
*/

$precision_digits = 201;

# the algorithm 2
$pi2 = new tPi2($precision_digits);
$pi2->calculate();
$pi2->print_report();

# the algorithm 3
$pi3 = new tPi3($precision_digits);
$pi3->calculate();
$pi3->print_report();

?>

  Previous example All examples  

  

🤝 Your IP address is 3.238.98.39


💻💽💾 Useful examples and tools for home and professional programming.


© Petr Faltus - 2021