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
junjunparkpark
Challenge Score: **10**
Ruby

1
cjordanball
Challenge Score: **10**
JavaScript

3
Corinto
Challenge Score: **10**
Python

4
mmalkavian
Challenge Score: **10**
Python

5
gzamfire
Challenge Score: **10**
Python

2
EmperorDuny
Challenge Score: **10**
JavaScript

2
lokeshmogra
Challenge Score: **10**
Cpp

3
cdms0
Challenge Score: **10**
Cpp

3
jackgeek
Challenge Score: **10**
JavaScript

6
hurgadion
Challenge Score: **10**
Python

7
scates
Challenge Score: **10**
Python

8
ricky0309
Challenge Score: **10**
Python

4
chrigolex
Challenge Score: **10**
JavaScript

9
philtroin
Challenge Score: **10**
Python

4
sanguookchoi
Challenge Score: **10**
Cpp

5
beeboop19
Challenge Score: **10**
JavaScript

6
mattlarsh
Challenge Score: **10**
JavaScript

7
deuzen
Challenge Score: **10**
JavaScript

8
SuprDewd
Challenge Score: **10**
JavaScript

5
gutty333
Challenge Score: **10**
Cpp

1
arjnklc
Challenge Score: **10**
Java

9
safetyscissors
Challenge Score: **10**
JavaScript

10
franko
Challenge Score: **10**
JavaScript

6
theBestFella
Challenge Score: **10**
Cpp

2
toasterbob
Challenge Score: **10**
Ruby

10
WayToTheTop
Challenge Score: **10**
Python

3
duncanbb
Challenge Score: **10**
Ruby

4
Bohdan
Challenge Score: **10**
Ruby

5
emuchynski
Challenge Score: **10**
Ruby

6
mienaineko
Challenge Score: **10**
Ruby

7
johne
Challenge Score: **10**
Cpp

7
randyjap
Challenge Score: **10**
Ruby

8
johncoleman83
Challenge Score: **10**
Ruby

1
ionutforyou
Challenge Score: **10**
PHP

9
kelaiyarao1
Challenge Score: **10**
Ruby

8
Justification
Challenge Score: **10**
Cpp

10
ironfingers
Challenge Score: **10**
Ruby

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
binfeng
Challenge Score: **10**
Java

10
anta0
Challenge Score: **10**
Cpp

6
ph4ntom
Challenge Score: **10**
Java

7
k8conant
Challenge Score: **10**
Java

8
nicklivens
Challenge Score: **10**
Java

9
KSmithson
Challenge Score: **10**
Java

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