Python Basics

Factorial using recursion

Detailed guide and Python implementation for the 'Factorial using recursion' problem.

問題提起

Write a function factorial(n) that computes the factorial of a non-negative integer n using recursion. The factorial of n (written as n!) is the product of all positive integers from 1 to n. By definition, 0! = 1. The function should return the factorial as an integer.

制約
  • 0 <= n <= 20

Example 1
Input
n = 5
Output
120
Explanation

5! = 5 * 4 * 3 * 2 * 1 = 120.

Example 2
Input
n = 0
Output
1
Explanation

0! is defined as 1.

Example 3
Input
n = 7
Output
5040
Explanation

7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040.

Need a Hint?
Consider using Recursion-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。