Attempting to calculate pi using chudnovsky algorithm in php -
i've been attempting create pi approximations using different methods, , chudnovsky algorithm looked fun, have hit roadblock.
here's code:
$pi =0; ($k = 0; $k < 1; $k++) { $pi += (($k%2==1?-1.0:1.0) * gmp_fact(6 * $k) * (13591409.0 + (545140134.0 * $k))) / (gmp_fact(3.0 * $k) * pow(gmp_fact($k), 3.0) * pow(640320.0, 3.0 * $k + 3.0/2.0)); } $pi *= 12; echo 1/$pi;
running gives me 301.59289474461.
i new php , thought training, forgive me if amateur work.
i realise there similar post in c++, attempted , successful, still broke. i've used stack exchange solve many other problems, hope can help!
make sure have gmp extension installed, , use gmp_strval() when using gmp functions in php avoid getting the address of gmp_fact object multiplied other variables. php.net : http://php.net/manual/en/function.gmp-fact.php#refsect1-function.gmp-fact-returnvalues
$pi =0; ($k = 0; $k < 20; $k++) { $pi += (($k%2==1?-1.0:1.0) * gmp_strval(gmp_fact(6 * $k)) * (13591409.0 + (545140134.0 * $k))) / ( gmp_strval(gmp_fact(3.0 * $k)) * pow( gmp_strval(gmp_fact($k)), 3.0) * pow(640320.0, 3.0 * $k + 3.0/2.0)); } $pi *= 12; echo 1/$pi;
Comments
Post a Comment