-
Notifications
You must be signed in to change notification settings - Fork 6
/
Pi_to_Nth.php
37 lines (35 loc) · 942 Bytes
/
Pi_to_Nth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
if (isset($_POST["Submit"])){
$precision = $_POST["precision"];
};
function bcpi($precision){
$num = 0;$k = 0;
bcscale($precision+3);
$limit = ($precision+3)/14;
while($k < $limit){
$num = bcadd($num,bcdiv(bcmul(bcadd('13591409',bcmul('545140134', $k)),bcmul(bcpow(-1, $k), bcfact(6*$k))),bcmul(bcmul(bcpow('640320',3*$k+1),bcsqrt('640320')), bcmul(bcfact(3*$k), bcpow(bcfact($k),3)))));
++$k;
}
return bcdiv(1,(bcmul(12,($num))),$precision);
}
function bcfact($n){
return ($n == 0 || $n== 1) ? 1 : bcmul($n,bcfact($n-1));
}
?>
<html>
<head>
<title>Calculate PI to Nth Digits</title>
</head>
<body>
<h1>Calculate PI</h1>
<form method="POST" action=" ?">
<p>Number of Digits: <input type="text" name="precision" /><input type="submit" value="Submit" name="Submit"/></p>
</form>
<?php
if(isset($_POST["precision"])){
$pi = bcpi($precision);
echo $pi;
}
?>
</body>
</html>