# Answer to Question #6788 in AJAX | JavaScript | HTML | PHP for Lucy

Question #6788

Write a PHP program that calculate Pi.

Expert's answer

Write a PHP program that calculate Pi.

PHP script is designed to be able to follow Viete's formula for calculating pi and if this script is given enough time to run, it can display 1000 digits of pi.

<?php

set_time_limit(0);

echo '<br />';

&

$digits=1000;

$pival='1';

$str1='';

while ($pirow<2000)

{

$pirow+=1;

$pisubrow=0;

$tempval='0';

while ($pisubrow<$pirow)

& {

& $tempval=bcsqrt(bcadd('2',$tempval,$digits),$digits);

& $pisubrow+=1;

& sleep(1);

& }

$tempval=bcdiv('2',$tempval,$digits);

$pival=bcmul($tempval,$pival,$digits);

unset($tempval);

$tempval=bcmul('2',$pival,$digits);

$common = array();

$length = (strlen($str1) >= strlen($tempval)) ? strlen($str1) : strlen($tempval);

for($x=0;$x<$length;$x++){

& if($str1[$x] == $tempval[$x]){

$common[] = $str1[$x];

& }else{

break;

& }

}

if ($pirow>5)

& {

& echo 'pi='.substr(join('',$common),0,-2);

& }

unset($str1);

$str1=$tempval;

unset($tempval);

echo '<p>';

flush();

sleep(1);

}

$pival=bcmul($pival,'2',$digits);

unset($pirow);

?>

PHP script is designed to be able to follow Viete's formula for calculating pi and if this script is given enough time to run, it can display 1000 digits of pi.

<?php

set_time_limit(0);

echo '<br />';

&

$digits=1000;

$pival='1';

$str1='';

while ($pirow<2000)

{

$pirow+=1;

$pisubrow=0;

$tempval='0';

while ($pisubrow<$pirow)

& {

& $tempval=bcsqrt(bcadd('2',$tempval,$digits),$digits);

& $pisubrow+=1;

& sleep(1);

& }

$tempval=bcdiv('2',$tempval,$digits);

$pival=bcmul($tempval,$pival,$digits);

unset($tempval);

$tempval=bcmul('2',$pival,$digits);

$common = array();

$length = (strlen($str1) >= strlen($tempval)) ? strlen($str1) : strlen($tempval);

for($x=0;$x<$length;$x++){

& if($str1[$x] == $tempval[$x]){

$common[] = $str1[$x];

& }else{

break;

& }

}

if ($pirow>5)

& {

& echo 'pi='.substr(join('',$common),0,-2);

& }

unset($str1);

$str1=$tempval;

unset($tempval);

echo '<p>';

flush();

sleep(1);

}

$pival=bcmul($pival,'2',$digits);

unset($pirow);

?>

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment