Step-by-step solution for First Factorial

View the challenge

This challenge requires you to return the factorial of a given number. A **factorial** of some number N is N multiplied by (N-1) multiplied by (N-2) and so forth until you reach the number 1. For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24. The algorithm for this will be quite simple, we'll loop from 1 to N multiplying each number by the previous one until we reach our number. Note: the notation for a factorial is ! so the factorial of 4 is written 4!
In the code below, we create a new variable called **factorial** which we will use to store our temporary values as we loop. In our loop, we start at 1 and increment until we reach our variable num.

```
function FirstFactorial(num) {
var factorial = 1;
for (var i = 1; i <= num; i++) {
// multiply each number between 1 and num
// factorial = 1 * 1 = 1
// factorial = 1 * 2 = 2
// factorial = 2 * 3 = 6
// factorial = 6 * 4 = 24
// ...
factorial = factorial * i;
}
return factorial;
}
FirstFactorial(4);
```

```
def FirstFactorial(num):
factorial = 1
for i in range(1, num+1):
# multiply each number between 1 and num
# factorial = 1 * 1 = 1
# factorial = 1 * 2 = 2
# factorial = 2 * 3 = 6
# factorial = 6 * 4 = 24
# ...
factorial = factorial * i
return factorial
print FirstFactorial(4)
```

```
def FirstFactorial(num)
factorial = 1
(1..num).each do |i|
# multiply each number between 1 and num
# factorial = 1 * 1 = 1
# factorial = 1 * 2 = 2
# factorial = 2 * 3 = 6
# factorial = 6 * 4 = 24
# ...
factorial = factorial * i
end
return factorial
end
puts FirstFactorial(STDIN.gets)
```

It is also possible to create a recursive function to calculate the factorial. A **recursive** function calls itself some number of times until it reaches a condition that terminates the function calls. A factorial function can be represented by the following recursive terms:
1! = 1
2! = 1! * 2
3! = 2! * 3
4! = 3! * 4
5! = 4! * 5
...
We can see that each factorial relies on the previous factorial, and then it multiplies that number by the current number. We'll convert these recursive terms into the following function:

```
function FirstFactorial(num) {
// our factorial function
function factorial(n) {
// terminate the recursion once we hit zero
if (n===0) { return 1; }
// otherwise keep calling the function recursively
else { return factorial(n-1) * n; }
}
return factorial(num);
}
FirstFactorial(4);
```

```
def FirstFactorial(num):
# our factorial function
def factorial(n):
# terminate the recursion once we hit zero
if n == 0:
return 1
# otherwise keep calling the function recursively
else:
return factorial(n-1) * n
return factorial(num)
print FirstFactorial(4)
```

```
def FirstFactorial(num)
# terminate the recursion once we hit zero
if num == 0
return 1
end
# otherwise keep calling the function recursively
return num * FirstFactorial(num - 1)
end
puts FirstFactorial(STDIN.gets)
```

While the above recursive implementation is correct, it may run slowly if you try to calculate the factorial of a very large number. One solution to this is memoization. **Memoization** is a technique used in programming where you store values that take a long time to calculate in a list, which you can then retrieve values from easily later on. You can read more about recursion and memoization here.

Top User Solutions

1
hyuil
Challenge Score: **10**
Cpp

1
cjordanball
Challenge Score: **10**
JavaScript

1
slavasprogis
Challenge Score: **10**
Python

2
Qlogin
Challenge Score: **10**
Python

1
emuchynski
Challenge Score: **10**
Ruby

3
KlaraKathrina
Challenge Score: **10**
Python

4
Sabin
Challenge Score: **10**
Python

2
junjunparkpark
Challenge Score: **10**
Ruby

5
Corinto
Challenge Score: **10**
Python

6
EmperorDuny
Challenge Score: **10**
Python

7
gzamfire
Challenge Score: **10**
Python

8
mmalkavian
Challenge Score: **10**
Python

2
mattlarsh
Challenge Score: **10**
JavaScript

3
leot75
Challenge Score: **10**
JavaScript

9
lesian
Challenge Score: **10**
Python

4
beeboop19
Challenge Score: **10**
JavaScript

2
lokeshmogra
Challenge Score: **10**
Cpp

3
gutty333
Challenge Score: **10**
Cpp

5
nulln
Challenge Score: **10**
JavaScript

4
cdms0
Challenge Score: **10**
Cpp

6
rnelson082
Challenge Score: **10**
JavaScript

7
jackgeek
Challenge Score: **10**
JavaScript

8
escapiststupor
Challenge Score: **10**
JavaScript

3
kmosier
Challenge Score: **10**
Ruby

10
hurgadion
Challenge Score: **10**
Python

4
lucanioi
Challenge Score: **10**
Ruby

5
Bohdan
Challenge Score: **10**
Ruby

9
chrigolex
Challenge Score: **10**
JavaScript

5
sanguookchoi
Challenge Score: **10**
Cpp

6
SSJ6Porfy
Challenge Score: **10**
Ruby

7
stevenli
Challenge Score: **10**
Ruby

10
JoanneTisch
Challenge Score: **10**
JavaScript

8
zestybmac
Challenge Score: **10**
Ruby

1
arjnklc
Challenge Score: **10**
Java

9
youngheekwon
Challenge Score: **10**
Ruby

10
mattfara
Challenge Score: **10**
Ruby

6
theBestFella
Challenge Score: **10**
Cpp

7
terry
Challenge Score: **10**
Cpp

8
johne
Challenge Score: **10**
Cpp

1
ionutforyou
Challenge Score: **10**
PHP

9
Justification
Challenge Score: **10**
Cpp

2
tostercx
Challenge Score: **10**
PHP

10
ssjtrent
Challenge Score: **10**
Cpp

2
danaksim
Challenge Score: **10**
Java

3
jakmaj
Challenge Score: **10**
Java

4
mikejagen
Challenge Score: **10**
Java

5
AjeethKannan
Challenge Score: **10**
Java

6
ph4ntom
Challenge Score: **10**
Java

7
binfeng
Challenge Score: **10**
Java

8
trebek1
Challenge Score: **10**
Java

9
Niliyam
Challenge Score: **10**
Java

10
k8conant
Challenge Score: **10**
Java

3
longcheng
Challenge Score: **10**
PHP

4
torbjornperttu
Challenge Score: **10**
PHP

5
albionselimaj
Challenge Score: **10**
PHP

6
teplicko
Challenge Score: **10**
PHP

7
Althalus
Challenge Score: **10**
PHP

8
smcdowell
Challenge Score: **10**
PHP

9
jetonr
Challenge Score: **10**
PHP

10
copacubanana
Challenge Score: **10**
PHP

With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

- 200+ Coding Challenges
- Mock Interview Questions
- 500,000+ Code Solutions
- Algorithm Tutorials
- Interview Prep Courses