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
Qlogin
Challenge Score: **10**
Python

2
Sabin
Challenge Score: **10**
Python

1
emuchynski
Challenge Score: **10**
Ruby

3
KlaraKathrina
Challenge Score: **10**
Python

2
junjunparkpark
Challenge Score: **10**
Ruby

1
cjordanball
Challenge Score: **10**
JavaScript

4
Corinto
Challenge Score: **10**
Python

5
EmperorDuny
Challenge Score: **10**
Python

6
gzamfire
Challenge Score: **10**
Python

7
mmalkavian
Challenge Score: **10**
Python

2
GarenLiang
Challenge Score: **10**
JavaScript

2
lokeshmogra
Challenge Score: **10**
Cpp

3
beeboop19
Challenge Score: **10**
JavaScript

3
cdms0
Challenge Score: **10**
Cpp

4
jackgeek
Challenge Score: **10**
JavaScript

5
escapiststupor
Challenge Score: **10**
JavaScript

8
hurgadion
Challenge Score: **10**
Python

4
gutty333
Challenge Score: **10**
Cpp

9
scates
Challenge Score: **10**
Python

10
ricky0309
Challenge Score: **10**
Python

6
chrigolex
Challenge Score: **10**
JavaScript

3
Bohdan
Challenge Score: **10**
Ruby

5
sanguookchoi
Challenge Score: **10**
Cpp

7
mattlarsh
Challenge Score: **10**
JavaScript

4
zestybmac
Challenge Score: **10**
Ruby

8
deuzen
Challenge Score: **10**
JavaScript

1
arjnklc
Challenge Score: **10**
Java

9
SuprDewd
Challenge Score: **10**
JavaScript

10
gladysum
Challenge Score: **10**
JavaScript

5
mattfara
Challenge Score: **10**
Ruby

6
theBestFella
Challenge Score: **10**
Cpp

6
toasterbob
Challenge Score: **10**
Ruby

7
SSJ6Porfy
Challenge Score: **10**
Ruby

8
duncanbb
Challenge Score: **10**
Ruby

9
kelaiyarao1
Challenge Score: **10**
Ruby

10
mienaineko
Challenge Score: **10**
Ruby

7
johne
Challenge Score: **10**
Cpp

1
ionutforyou
Challenge Score: **10**
PHP

8
Justification
Challenge Score: **10**
Cpp

2
tostercx
Challenge Score: **10**
PHP

9
Zeikar
Challenge Score: **10**
Cpp

2
danaksim
Challenge Score: **10**
Java

3
jakmaj
Challenge Score: **10**
Java

4
mikejagen
Challenge Score: **10**
Java

5
ph4ntom
Challenge Score: **10**
Java

6
binfeng
Challenge Score: **10**
Java

10
anta0
Challenge Score: **10**
Cpp

7
trebek1
Challenge Score: **10**
Java

8
Niliyam
Challenge Score: **10**
Java

9
k8conant
Challenge Score: **10**
Java

10
nicklivens
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.

developer.” ― Josh Aharonoff

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