30 Days of Code — Day 9

Tyler Carter
1 min readMar 11, 2021
Photo by Hitesh Choudhary on Unsplash

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.

--

--

Tyler Carter
0 Followers

I am currently an Operations Program Manager for Microsoft. I have been teaching myself Python to use for Data Analytics and Machine Learning.