math - PHP round number to 6 decimal places -
i need return result of (1 / n!) * (1! + 2! + 3! + ... + n!), n>=1. codewars challenge! code below returns 1.146652 n = 8, correct result 1.1466510000000001 or 1.146651.
how can truncate number correctly?
function factorial($val){ $factor = 1; for($i=1;$i<=$val;$i++){ $factor *= $i; } return $factor; } function going($n) { $val = 1/factorial($n); $somatorio = 0; for($i=1;$i<=$n;$i++){ $somatorio += factorial($i); } return round($val * $somatorio,6); }
Comments
Post a Comment