Use of recursion for repetitive algorithms

--Originally published at Py(t)hon

This time we are going to learn about recursion, what is recursion? Recursion is a method where the solution to a problem is based on solving smaller instances of the same problem or in other words  is a way of programming or coding a problem, in which a function calls itself one or more times in its body.

An example is the function of the factorial:

fact-1

Click here for more examples.

Here is a video for better understanding:

#Pug#TC101#Tec#ISC#Recursion#NoMore


Recursion

--Originally published at Python

Recursion is when you call a function inside of itself. It can be used in several ways, for example, you could create a function that will give you a final result after repeating itself several times, or it could give you several answers, depending on how many times it went through itself. The most common and probably the easiest example to understand on this subject is when writing a program that gives you the factorial of a number.

Code:

num = int(input(“Type a number: “))

def Factorial(num):
if num<=1:
return 1

else:
return num*Factorial(num-1)

print(Factorial(num))

2recursion

If the number is 1 or 0, the function won’t call itself, it will just return 1. If the number is bigger, then the recursion begins. Every time it goes through the else, it multiplies n by the answer to factorial n-1. This answer will depend on the number you get in the new factorial, and it will keep on decreasing until you get to 1, which is when all the previous numbers will start multiplying. These answer will stack up until the program gets to the first time the function was called, and it will return the final answer. It might be confusing at times, but ultimately, it follows this idea.


OOPS! I DID IT AGAIN

--Originally published at Coding The Future

via GIPHY

Hello my fellow programmers! I'm back again with a new and interesting topic for today. And I know, I said I would stop with the song references, but I just can't!

Today, I'm quoting Britney and her masterpiece Oops! I did it again to get us started on the topic of recursion. Recursion is the repetition of an algorithm for as many times as necessary to generate a conclusion.

What is recursion?

Recursion is funny because it's kind of related to infinity. Is not the same as loop, because the function "calls itself more than one time within its own body"1, becoming something like a function inside a function, inside a function, inside a function, and so on. It can also be interpreted as solving a big problem in smaller and smaller portions, and solving the first and smallest, followed by the second, until reaching the final big problem. However, a recursive function must end at some point to successfully obtain an answer.

Let's dive deeper into the topic. To do that, we will explore a popular example.

Examples of recursion

The Fibonacci numbers are the numbers of the following sequence of integer values:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

The Fibonacci numbers are defined by:
Fn = Fn-1 + Fn-2 with F0 = 0 and F1 = 1.

If we wanted to write a function that returns a determined index of the of Fibonacci series we would use something like this:

def fibo(n): if n == 0: return 0 elif n == 1: return 1 else: return fibo(n-1) + fibo(n-2)

As you may notice, the else statement, which exits within fibo calls itself again, until the the if or the elif are reached. If Continue reading "OOPS! I DID IT AGAIN"