30 Days of Code — Day 9
Welcome to day 9 of 30 Days of Code from Hacker Rank!
The Task
Complete the factorial function in the editor below. Be sure to use recursion.
factorial has the following parameter:
- int n: an integer
The Code
The Result
In order to showcase that it worked I did alter the code a little:
import mathdef factorial(n):
return math.factorial(n)
if __name__ == '__main__':
n = int(input())
for i in range(n):
print(factorial(i))>? 10
… 1
… 1
… 2
… 6
… 24
… 120
… 720
… 5040
… 40320
… 362880
Conclusion
I will admit the levels of difficulty in this challenge change rapidly. Compared to the last one this one was as easy as looking up the math module API and finding the factorial method. After that it was just sticking the method into a defined function with a return statement.
Here is a link to the day_8 answer/challenge I wrote about previously: 30 Days of Code — Day 8.