Python Data Science Jobs & Interviews
20.4K subscribers
188 photos
4 videos
25 files
326 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
Q: What is a recursive function, and how can it be used to calculate factorial?

A recursive function is a function that calls itself to solve smaller instances of the same problem. It's a key concept in algorithms and helps break down complex tasks into simpler steps.

To calculate factorial, we use the formula:
n! = n × (n-1) × (n-2) × ... × 1
With recursion, we define:
- Base case: 0! = 1, 1! = 1
- Recursive case: n! = n × (n-1)!

Example (Python code for beginners):

def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

# Example usage
result = factorial(5)
print("Factorial of 5:", result) # Output: Factorial of 5: 120

How it works:
- factorial(5)5 * factorial(4)
- factorial(4)4 * factorial(3)
- ... until factorial(1) returns 1
- Then it multiplies back: 5*4*3*2*1 = 120

Try changing the number to see different results!

#Recursion #Factorial #Programming #BeginnerCode #Algorithms #TechTips

By: @DataScienceQ 🚀
Please open Telegram to view this post
VIEW IN TELEGRAM