Calculate 70! in JavaScript

We may never know how many atoms are in the entire observable universe... but we can imagine the answer as a very big number. Some scientists claim however that this number is less than 70!

In the above statement 70! means factorial of 70, which is equal to the product of all numbers from 1 to 70.

70! = 1 * 2 * 3 * ... * 70

This looks impossible but you can actually verify this statement pretty easily with the help of Google and a little bit of JavaScript.

Step 1

Just google "atoms in the observable universe" and see the answer.

Step 2

Use the codeguppy.com editor and copy and paste the following code snippet:

let p = 1n;

for(let i = 1n; i <= 70n; i++)
{
    p *= i;
}

println(p);


Note: In this example we are working with Big Numbers in JavaScript (note the "n" suffix after the constants in the code above).

Step 3

Compare the results from the previous steps! You'll see that the statement seems to be accurate!

Of course, we don't know for sure if scientists are right about the number of atoms in the observable universe. But we can clearly see how big 70! is.

70! is such a big number that you cannot calculate accurately using regular numbers. That's why we use used Big Numbers in the above program. If you remove the "n" sufix in the above program you'll still be able to calculate the factorial, although with less precision.

Big numbers are a pretty recent construct in the JavaScript language. If you want to calculate 70! in a classical way, without big numbers, but also with precision, just take a look at this code: https://codeguppy.com/code.html?m4AfgJmCABGNEvKlUNtM

Read more blog articles Browse JavaScript projects

About codeguppy

CodeGuppy is a FREE coding platform for schools and intependent learners. If you don't have yet an account with codeguppy.com, you can start by visiting the registration page and sign-up for a free account. Registered users can access tons of fun projects!


Follow @codeguppy on Twitter for coding tips and news about codeguppy platform. For more information, please feel free to contact us.