 Solutions for First Factorial
View the challenge

Official solution

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 Rated User Solution

``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
``loading...``
Top User Solutions