2

For example, if I produce the factorial of 100 and store it in this way using JavaScript:

function factorial (num) {
    if (num == 0) {
        return 1;
    }
    else     {
        return num * factorial(num - 1);
    }
}

var result = factorial(100);

console.log(result) will then produce 9.33262154439441e+157.

Is there a way to have it display in full?

As a string would be fine, but result.toString() literally produces "9.33262154439441e+157".

If there's some way to imitate charAt() for numbers, iterating over each digit and concatenating them as a string would become an option.

All in all, is there any way to achieve this?

user2870301
  • 31
  • 1
  • 3
  • 1
    Use a string for `num`, and do the multiplication one character at a time (from right to left). Don't forget about carrying. – Trojan Oct 27 '13 at 22:21
  • 2
    That's almost as many "good" digits as you can get out of a double-precision floating point number when expressed in decimal. Most of the 158 digits are not really there. – Pointy Oct 27 '13 at 22:21
  • If what you want is processing really large numbers indeed, check [this library](https://github.com/jtobey/javascript-bignum). I wonder, though, ain't you looking for something else to do with that 100! value... – raina77ow Oct 27 '13 at 22:24
  • @raina77ow - Thanks for the help; in actual fact, I'm trying to find the sum of the digits in very large numbers. The library looks interesting, I'll try it out. Thank you. – user2870301 Oct 27 '13 at 22:31
  • 1
    But that's another question, don't you think? And it's already answered [here](http://stackoverflow.com/questions/1469529/sum-of-digits-of-a-factorial). Obviously, you don't have to compute `100!` to find the sum of its digits, that would be rather trivial. – raina77ow Oct 27 '13 at 22:34
  • @raina77ow - Yes, most likely. I just wanted to ask this question, because I thought it would be something of use to know if I ran into a similar problem again. – user2870301 Oct 27 '13 at 22:39

0 Answers0