0

Ok, so I need this number 3.55687428096E+14 to actually look like a proper whole number. I am not sure what to do in order to make it look like a whole number so that I can go about with my coding. Basically, without resolving this number 3.55687428096E+14, I cant proceed properly. Below is my code, please help!

<?php
$num = 17;
$fact = $num;

echo "The factorial of $num is $num X ";
while ($num > 1){
    --$num;
    if ($num != 1){
        echo "$num X ";
    }
    else{
        echo "$num <b>equals</b> ";
    }

    $fact = $fact * $num;
}
echo $fact ."<br/>";
?>

By the way, the number 3.55687428096E+14 is the result I am getting after running the program. It comes ok till factorial 16 but once it turns to 17, things become 3.55687428096E+14!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • `number_format(3.55687428096E+14,0,'','')` – Alive to die - Anant Mar 06 '16 at 16:38
  • ok, so what does this do and where do I insert this code Anant? – Sricharan Krishnan Mar 06 '16 at 16:40
  • Looks like a proper whole number to me – Ed Heal Mar 06 '16 at 16:41
  • [Convert exponential to a whole number in PHP](http://stackoverflow.com/questions/4964059/convert-exponential-to-a-whole-number-in-php) – Alive to die - Anant Mar 06 '16 at 16:42
  • If you need to work with values that exceed the 32-bit signed integer range, and with absolute precision; then use gmp or bcmath – Mark Baker Mar 06 '16 at 16:42
  • Lol. No! it just comes as 3.55687428096E+14. Ok so, factorial 16 comes as 20922789888000. This makes sense to me but the output of factorial 17 just keeps coming as 3.55687428096E+14.!! – Sricharan Krishnan Mar 06 '16 at 16:43
  • `echo number_format($fact,0,'','') ."
    ";`
    – Alive to die - Anant Mar 06 '16 at 16:44
  • Is it just that you're not familiar with scientific notation? – jonrsharpe Mar 06 '16 at 16:48
  • @SricharanKrishnan YOU HAVE ANSWERS. PLEASE MARK AND UP-VOTE THE ANSWER WHICH IS CORRECT AND DESCRIPTIVE TO YOU. – Alive to die - Anant Mar 06 '16 at 17:02
  • ok, me just a newbie at all of these things and I am learning. so I tried the number_format procedure but does not fully work, I am basically solving a problem at project euler, from here planning to get the result of "sum of the digits of the factorial of !100." its still says me is wrong with the answer 734. So , what is this bcmath do and how do I use it in my code. If some one can help with putting this in my code and a bit of explanation, I think I should be able to catch up.... – Sricharan Krishnan Mar 06 '16 at 17:03
  • [bcmath](http://php.net/manual/en/book.bc.php) Arbitrary precision mathematics – Mark Baker Mar 06 '16 at 18:06
  • @MarkBaker :hello Mark. Just wanted to let you know that I am grateful for your help. I somehow figured out the process with the BC Math function in php. My understanding thus far thanks to your input is that PHP helps perform math operations with regular operands with only upto a certain value but inorder to effectively target much bigger numbers (like starting with factorial of 17), the BC Math functions are really super helpful! My code was succesfully executed. – Sricharan Krishnan Mar 07 '16 at 14:24
  • It's not PHP specifically, it's any language that uses fixed size for integers (which is most programming languages, either 32-bit or 64-bit signed/unsigned integer types).... the drawback with PHP is that it automatically converts integers to float on overflow of that fixed size, with related loss of precision; whereas many other languages will simply generate an overflow error when that happens – Mark Baker Mar 07 '16 at 14:41

3 Answers3

0

You can user number_format() function if your number is goes very large.

number_format():

The number_format() function formats a number with grouped thousands.

Your code

<?php
$num = 17;
$fact = $num;

echo "The factorial of $num is $num X ";
while ($num > 1){
    --$num;
    if ($num != 1){
        echo "$num X ";
    }
    else{
        echo "$num <b>equals</b> ";
    }

    $fact = $fact * $num;
}
echo number_format($fact) ."<br/>";
?>
Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52
0

Change the last line with :

echo number_format($fact,0) ."<br/>";
Tantowi Mustofa
  • 677
  • 4
  • 8
  • ok... so the number_format is working but I guess not fully enough. I guess I need to take the bcmath thing that Mark Baker suggested. though not sure what that means.... – Sricharan Krishnan Mar 06 '16 at 17:01
  • it's working and correct. it will output 355,687,428,096,000. That's the number you're looking for – Tantowi Mustofa Mar 06 '16 at 17:32
0

@Mark Baker: - my code below which was executed successfully thanks to your info and support:

<?php 

echo "<h2>Find the sum of the digits in the number 100!</h2>"."<br/>";

$num = 100;
$fact = $num;
echo "The factorial of $num is $num X ";

while ($num > 1){
    --$num;
    if ($num != 1){
        echo " $num X ";
    }
    else{
        echo " $num";
    }

    $fact = bcmul($fact,$num); //right over here!
}

echo " which equals" ."<br/>". "<h3>$fact</h3>";

//proceeding now to calculate the sum of the digits of the factorial!
$_array = str_split($fact);
$sum = array_sum($_array);
echo "The sum of the digits of the factorial is " ."<br/>";
echo "<h3>$sum</h3>";

?>