Search code examples
javascriptphppoisson

Parse poisson function in javascript to php


I have the next code in javascript:

var exponential = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746;


var numerator, denominator;

function fact(x) {
if(x==0) {
    return 1;
}
return x * fact(x-1);
}



function poisson(k, landa) {
    exponentialPower = Math.pow(exponential, -landa); // negative power k
    landaPowerK = Math.pow(landa, k); // Landa elevated k
    numerator = exponentialPower * landaPowerK;
    denominator = fact(k); // factorial of k.

    return (numerator / denominator);
}

I need parse to php but i don't know how...

Can somebody help me?


Solution

  • you should put below variables within poisson function also dont think you need to initialize $numerator and $dominator to 0;

    $exponential = 2;
    $numerator = 0; 
    $dominator = 0; 
    

     function fact($x) {
            if($x==0) {
                return 1;
            }
            return $x * fact($x-1);
            }
    
    
            function poisson($k, $landa)
    {
            $exponential = 2;
            $exponentialPower = pow($exponential, -$landa);
            $landaPowerK = pow($landa,$k);
            $numerator = $exponentialPower * $landaPowerK;
    
            $dominator = fact($k);
    
            echo ($numerator / $dominator);
    }
    
    poisson(1,2);