What is a Factorial? How to Calculate Factorials with Examples (2024)

/ #Math
What is a Factorial? How to Calculate Factorials with Examples (1)
Ilenia Magoni
What is a Factorial? How to Calculate Factorials with Examples (2)

A factorial is a mathematical operation that you write like this: n!. It represents the multiplication of all numbers between 1 and n.

So if you were to have 3!, for example, you'd compute 3 x 2 x 1 (which = 6). Let's see how it works with some more examples.

Definition of a Factorial

The factorial of a number is the multiplication of all the numbers between 1 and the number itself. It is written like this: n!. So the factorial of 2 is 2! (= 1 × 2).

To calculate a factorial you need to know two things:

  1. 0! = 1
  2. n! = (n - 1)! × n

The factorial of 0 has value of 1, and the factorial of a number n is equal to the multiplication between the number n and the factorial of n-1.

For example, 5! is equal to 4! × 5.

Here the first few factorial values to give you an idea of how this works:

FactorialMultiplicationResult
0!11
1!11
2!1 × 22
3!1 × 2 × 36
4!1 × 2 × 3 × 424
5!1 × 2 × 3 × 4 × 5120
6!1 × 2 × 3 × 4 × 5 × 6720
7!1 × 2 × 3 × 4 × 5 × 6 × 75040
8!1 × 2 × 3 × 4 × 5 × 6 × 7 × 840,320
9!1 × 2 × 3 × 4 × 5 × 6 × 7 × 8 × 9362,880

What is a Factorial Used For?

Practically speaking, a factorial is the number of different permutations you can have with n items: 3 items can be arranged in exactly 6 different ways (expressed as 3!).

For example, let's see all the arrangements you can have with the three items, A, B and C:

ABCACBBACBCACABCBA

And in fact, 3! = 6.

How to Calculate the Factorial of 0

Looking at the factorial from this point of view, what's the factorial of 0?

Well, how many different ways can you arrange 0 elements?

There is exactly 1 way to arrange zero elements. And that's making a sequence of zero elements.

Factorial Use Cases

You typically use a factorial when you have a problem related to the number of possible arrangements. Let's look at some example problems.

Factorial example problem 1: the letters in the word "camper"

How many different ways can you arrange the letters of the word camper?

The word camper has 6 letters, so the number of possible arrangements is given by the factorial of 6: 6! = 6 × 5 × 4 × 3 × 2 × 1 = 720. That would have been a pretty big number of arrangements to find by hand, wouldn't it?

Factorial example problem 2: drawing colored balls from a bag

Let's say there are three balls in a bag – one green, one blue, and one yellow.

If you draw the three balls in sequence, what chance is there that you'll get the yellow first, the green one second, and the blue one last?

Maybe now you are wondering what chances have to do with factorials – well, in a moment you will see.

There are 6 possible sequences in which the balls can be drawn: 3! = 6.

There is a chance of 1 over the total number of possibilities to get the yellow-green-blue sequence, so that is 1/(3!) or 1/6 or 16.7% chance to get the desired outcome.

How to Calculate a Factorial Programmatically with JavaScript

There are two ways to calculate factorials programmatically in JavaScript:

How to calculate a factorial in JS with recursion

Let's get back to the two things to know when calculating a factorial – that is 0! = 1 and n! = (n - 1)! × n. We can use the first one to create the base case of the recursive function, because in that case we know the result already.

function factorial(n) { if (n === 0) { return 1; }}

The second thing to know about how to calculate a factorial, n! = (n - 1)! × n, can be the recursive case.

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

How to calculate a factorial with a JavaScript while loop

We said before that 0! = 1. So, to calculate the factorial of a number with a loop, we can initialize a variable to 1, and multiply the numbers from n to 1 by the variable inside the loop.

In this way, if the input is higher than 1, the output will easily be 1.

function factorial(n) { let result = 1; for (n > 1) { result *= n; n--; } return result;}

Conclusion

The factorial is a pretty important operator to know if you are interested in statistics and probabilities.

In this article you have learned a how to calculate a factorial, a simple application, and you have seen how to calculate it using JavaScript.

Have fun with it!

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

What is a Factorial? How to Calculate Factorials with Examples (3)
Ilenia Magoni

Moderator and staff author for freeCodeCamp.

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

What is a Factorial? How to Calculate Factorials with Examples (2024)

FAQs

What is a Factorial? How to Calculate Factorials with Examples? ›

A factorial is a mathematical operation that you write like this: n! . It represents the multiplication of all numbers between 1 and n. So if you were to have 3! , for example, you'd compute 3 x 2 x 1 (which = 6).

How do you calculate a factorial? ›

In short, a factorial is a function that multiplies a number by every number below it till 1. For example, the factorial of 3 represents the multiplication of numbers 3, 2, 1, i.e. 3! = 3 × 2 × 1 and is equal to 6.

What is factorial with example? ›

The factorial sign is an exclamation point and it means to start with the number and multiply by each previous integer until reaching 1. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.

What is a factorial of 7? ›

7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040.

What is a factorial of 24? ›

When you come across a factorial problem in math, you'll see an exclamation symbol (!) to represent the factorial. In math, we define factorial as the product of a certain integer with all the positive integers below it. So, 3! is 3 * 2 * 1. So, 24! is 24 * 23 * 21 * all the way down to * 1.

What is the exact formula for factorial? ›

In mathematics, the factorial of a number is found by the multiplication of the number with every positive integer less than that. So, n!= n × (n-1) × (n-2) × (n-3) × ..... × 3 × 2 × 1.

What is 52 factorial factorial? ›

Here's what that looks like: 80658175170943878571660636856403766975289505440883277824000000000000. This number is beyond astronomically large. I say beyond astronomically large because most numbers that we already consider to be astronomically large are mere infinitesmal fractions of this number.

What is the factorial for 20? ›

Summary: The factorial of 20 is 2432902008176640000.

What is the answer to the factorial of 40? ›

The factorial of 40 is 815915283247897734345611269596115894272000000000.

How to solve factorials without a calculator? ›

To do factorials, start by determining which number you're computing the factorial for, which will be the number that's in front of the exclamation point. Then, write out all of the numbers that descend sequentially from that number until you get to 1. Finally, multiply all of the numbers together.

What is factorial of 100 *? ›

The answer of what is the factorial of 100

The approximate value of 100! is 9.3326215443944E+157. The number of trailing zeros in 100! is 24. The number of digits in 100 factorial is 158.

When to use factorial? ›

It is common to use Factorial functions to calculate combinations and permutations. Thanks to the Factorial you can also calculate probabilities.

How do you find Factorials quickly? ›

To do factorials, start by determining which number you're computing the factorial for, which will be the number that's in front of the exclamation point. Then, write out all of the numbers that descend sequentially from that number until you get to 1. Finally, multiply all of the numbers together.

How do you do a 5 factorial? ›

To find 5 factorial, or 5!, simply use the formula; that is, multiply all the integers together from 5 down to 1. When we use the formula to find 5!, we get 120. So, 5! = 120.

What is the factorial formula simplified? ›

The factorial operation, n!, is defined as n! = n(n – 1)(n – 2)(n – 3) · · · 4 · 3 · 2 · 1. In other words, you multiply the number n, being operated upon, by every positive integer smaller than n.

Top Articles
Best Video CMS - August 2024 Reviews & Comparison
The Best Digital Audio Workstations (DAWs) for 2024
Lux Nails Columbia Mo
Futuretechgirls Contact
0.0Gomovies
Ebony Ts Facials
What Does Sybau Mean
Craigslist Pets Longview Tx
When Does Dtlr Close
Brazos County Jail Times Newspaper
7 Best Character Builds In Nioh 2
Strange World Showtimes Near Cmx Downtown At The Gardens 16
Vonage Support Squad.screenconnect.com
Pokemon Fire Red Download Pc
Nearest Walmart Address
Chris Evert Twitter
Craigslist Tools Las Cruces Nm
Pathfinder 2E Throwing Weapons
Fragments Of Power Conan Exiles
Tractorhouse Farm Equipment
Advanced Eyecare Bowling Green Mo
Osrs Toby
Coleman Funeral Home Olive Branch Ms Obituaries
Craigslist For Sale By Owner Chillicothe Ohio
My Eschedule Greatpeople Me
John Wick 4 Showtimes Near Starlight Whittier Village Cinemas
San Diego Cars And Trucks Craigslist
Filmy4Wap Xyz.com 2022
Charles Bengry Commerce Ca
Bdo Passion Of Valtarra
Bakkesmod Preset
Publix – Supermarkt mit ökologischem Gewissen und exzellentem Service
Marie Anne Thiebaud 2019
Proto Ultima Exoplating
Central Valley growers, undocumented farmworkers condemn Trump's 'emergency'
Margie's Money Saver Hey Dudes
Joftens Notes Skyrim
Wiki Jfk Film
Parx Entries For Today
I Got Hoes Might Just Be You N
Theresa Alone Gofundme
Cibo Tx International Kitchen Schertz Menu
Craigslist Pelham Al
Gizmo Ripple Tank Answer Key
Gaylia puss*r Davis
Grizzly Expiration Date 2023
Saratoga Otb Results
Farmers And Merchants Bank Broadway Va
Wayfair Outlet Dayton Ohio
Four Observations from Germany’s barnstorming 5-0 victory over Hungary
Edible Arrangements Track
Tetris Google Sites
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 5945

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.