0

I am trying to implement the BBP algorithm in php. My code is returning a decimal which i thought was odd as it should be in hex. I was told to convert to decimal from hex by multiplying by 16 also but now its all just wrong. Here is a sample:

$n1=$n2=$n3=$n4=$n5=$n6=$n7=$n8 =0;
$S1=$S2=$S3=$S4=$S5=$S6=$S7=$S8 = 0; //initializing
$k = 0;
$m1=  8*$k + 1;
$m2 = 8*$k + 4;
$m3 = 8*$k + 5;
$m4 = 8*$k = 6;

$b =16;
$e=$n-$k;


while($k<$n){ //Sum 1 of 8

    $S1 +=Modular($b, $m1, $e)/$m1; //see Moduler_Expansion.php

   $k++;

}

$k = $n +1; //redefine for second sum, and every other
while($k<$limit){ //Sum 2 of 8

    $S2 += (pow($b,$n-$k))/($m1);

   $k++; //now repeat similar process for each sum.
}

and I repeat the process for each term of BBP then:

$S = 4*($S1 + $S2) - 2*($S3+$S4) -($S5+$S6) - ($S7+$S8);

` Following the wiki page I then strip the integer and multiply by 16, but for $k =0 I get; 3.4977777777778 and for $k = 1: 7.9644444444448.

I dont think these are right, it could just be i do not know how to interpret th ouput properly. Can anyone offer any advice?

  • You may want to try [`dechex()`](http://php.net/manual/en/function.dechex.php) to make the conversion to hex. – Jay Blanchard May 14 '15 at 16:48
  • But doesnt the BBP just print out hex to start with? –  May 14 '15 at 17:01
  • Because there is no BBP algorithm in PHP you would have to write the code to make it do this. – Jay Blanchard May 14 '15 at 17:02
  • ...yes, that's what i am trying to do. that's my attempt up there. –  May 14 '15 at 17:09
  • You can't assign a formula to a variable like you're trying to do with `$m1` through `$m4`, they are evaluated in-place while `$k` is zero. Try using functions instead. – Sammitch May 14 '15 at 18:01

0 Answers0