Find All Divisors of a Number (For Loop v.s Recursion)

Опубликовано: 13 Октябрь 2024
на канале: Quoc Dat Phung
2,680
29

In this video, I will show you how to find all of the divisors of a number in a Python program using a for loop and then using recursion. If a is a divisor of b it means that when b divides a, the remainder is zero. An example is 6 and 3. Three is the divisor of 6 because the remainder is zero when six divides three. Here, our goal is to get all the divisors. Therefore, all the divisors of 6 are 1, 2, 3, and 6.

What is recursion? Recursion is a function that calls upon itself. Here, we write a function that calls upon itself and to find all of the divisors of a number. While it is easy to use a for loop or while loop to do this, it is important that you understand these recursion steps to have a strong foundation to learn more complex recursive algorithm in the future.

In the next video, I will show you how to check if an element exists inside a list using recursion. For example, the function inList([1,2,3], 1) returns true because 1 is indeed inside [1,2,3]. This is also known as list membership.
LIKE & SUBSCRIBE:
🔴 My Channel:    / quocdatphung  
🔴 My second channel:    / @purifynature8479