Solutions 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

2
atib1980
Challenge Score: **10**
Cpp

1
kalgima
Challenge Score: **10**
Swift

1
cjordanball
Challenge Score: **10**
JavaScript

2
hunsoo
Challenge Score: **10**
JavaScript

1
Qlogin
Challenge Score: **10**
Python

2
ssprogis
Challenge Score: **10**
Python

3
KlaraKathrina
Challenge Score: **10**
Python

1
emuchynski
Challenge Score: **10**
Ruby

4
Sabin
Challenge Score: **10**
Python

3
gutty333
Challenge Score: **10**
Cpp

2
junjunparkpark
Challenge Score: **10**
Ruby

5
Corinto
Challenge Score: **10**
Python

6
bneeriemer
Challenge Score: **10**
Python

7
lesian
Challenge Score: **10**
Python

8
Ryantimjohn
Challenge Score: **10**
Python

9
EmperorDuny
Challenge Score: **10**
Python

3
jrorsini
Challenge Score: **10**
JavaScript

4
leot75
Challenge Score: **10**
JavaScript

3
Ryanm1234
Challenge Score: **10**
Ruby

5
nulln
Challenge Score: **10**
JavaScript

10
gzamfire
Challenge Score: **10**
Python

6
leonhfr
Challenge Score: **10**
JavaScript

7
mattlarsh
Challenge Score: **10**
JavaScript

8
beeboop19
Challenge Score: **10**
JavaScript

4
lokeshmogra
Challenge Score: **10**
Cpp

5
cdms0
Challenge Score: **10**
Cpp

9
rnelson082
Challenge Score: **10**
JavaScript

10
jackgeek
Challenge Score: **10**
JavaScript

4
memyselfandhai
Challenge Score: **10**
Ruby

5
kmosier
Challenge Score: **10**
Ruby

6
lucanioi
Challenge Score: **10**
Ruby

7
Bohdan
Challenge Score: **10**
Ruby

6
sanguookchoi
Challenge Score: **10**
Cpp

8
SSJ6Porfy
Challenge Score: **10**
Ruby

9
stevenli
Challenge Score: **10**
Ruby

10
zestybmac
Challenge Score: **10**
Ruby

1
arjnklc
Challenge Score: **10**
Java

7
theBestFella
Challenge Score: **10**
Cpp

2
tonichen
Challenge Score: **10**
Java

8
terry
Challenge Score: **10**
Cpp

9
ssjtrent
Challenge Score: **10**
Cpp

10
johne
Challenge Score: **10**
Cpp

1
ionutforyou
Challenge Score: **10**
PHP

2
tostercx
Challenge Score: **10**
PHP

3
danaksim
Challenge Score: **10**
Java

4
jakmaj
Challenge Score: **10**
Java

5
mikejagen
Challenge Score: **10**
Java

6
AjeethKannan
Challenge Score: **10**
Java

7
ph4ntom
Challenge Score: **10**
Java

8
binfeng
Challenge Score: **10**
Java

2
Julianee
Challenge Score: **10**
Swift

9
trebek1
Challenge Score: **10**
Java

10
Niliyam
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

3
designergenes
Challenge Score: **9**
Swift

With our large collection of challenges, 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
- 800,000+ Code Solutions
- Algorithm Tutorials
- Interview Prep Courses