Skip to content

Instantly share code, notes, and snippets.

@mrunkel
Last active October 11, 2020 19:15
Show Gist options
  • Save mrunkel/451f1094d8ba379834bd51ed1088dd28 to your computer and use it in GitHub Desktop.
Save mrunkel/451f1094d8ba379834bd51ed1088dd28 to your computer and use it in GitHub Desktop.
How to calculate pi in PHP.
<?php
$add = true;
$pi = 3;
for ($x = 2; $x <10000; $x += 2) {
$new = 4/($x * ($x + 1) * ($x + 2));
$pi = $add ?
$pi + $new :
$pi - $new;
$add = !$add;
}
echo $pi . "<br>";
echo 3.14159265359;
@mrunkel
Copy link
Author

mrunkel commented Oct 11, 2020

Outputs:

3.1415926535918
3.14159265359 

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