Most voted "recursion" questions
In computer science, recursion is a method of solving problems in which the solution is given by an algorithm that refers to itself. A classic example is the factorial calculation of a number.
Learn more…240 questions
Sort by count of
-
54
votes5
answers7178
viewsWhat is the advantage of using recursive functions?
Recently discovered the famous (or not so famous) Recursive Functions and I found the concept very interesting. However, during my reading I had some doubts regarding the use of such. What is the…
php loop software-engineering recursion encoding-styleasked 8 years, 10 months ago João Paulo Vieira da Silva 1,933 -
43
votes3
answers8167
viewsWhen to use recursion and when to use loops?
A problem can be solved and get the same result used a loop or through recursive calls to a function. Whereas the programming language being used has both features, how to know when one is better…
function loop software-engineering encoding-style recursionasked 10 years, 5 months ago Maniero 444,682 -
22
votes2
answers609
viewsCould current Javascript engines optimize "tail" recursive calls?
In functional programming, it is very common to use functions recursive. Certain languages, such as Scheme, do not even have control structures for loops, depending on recursion to iterate over…
-
19
votes2
answers6496
viewsWhat is the difference between recursion and tail recursion?
In functional programming languages, it is said that it is possible to avoid stack bursts using "tail recursion". But what is the difference between normal recursion and tail recursion?
-
18
votes0
answers488
viewsRecursive call error in nested procedures
I have some procedures Mysql to do the following: The main code will always call the trial CALL sp_syncTabela. To sp_syncTabela will check if there is another specific process for the tableName…
-
15
votes4
answers1141
viewsHow to simulate "tail recursion" in C#?
No. Net, I know it is possible to make cause calls because the compiler of F#, when optimizing code, transforms a function with tail recursion into a function with a loop, thus avoiding stack…
-
13
votes2
answers668
viewsWhy is a Fibonacci faster in Java than in C?
Not exactly like this, but I noticed that when the function needs more time to compute Fibonacci, Java actually does better than C. Here are the implementations: C: #include<stdio.h> unsigned…
-
13
votes3
answers689
viewsConvert nested loops to recursive function to compute combinations
I did a job for compute all combinations of an array. The problem with my approach is that it only "generates" three-digit combinations (array length past 3), since I have 3 nested loops. If I…
-
11
votes4
answers584
viewsHow to verify the efficiency of these 2 functions in C++?
How to determine the best choice between these two functions for implementation? 1: int X(int x){ if(x<=0) return 0; return(x + X(x-1)); } 2: int Y(int x){ int soma=0; for(int i=0;i<=x;++i)…
-
10
votes4
answers1546
viewsHow can I optimize a recursive method for finding ancestors?
I have a class Pessoa who owned relationships for his father and his mother (these at any time may be null). On a particular piece of my code I need to find out if one person is an ancestor of the…
-
10
votes1
answer269
viewsDifficulty in Syntax
I would like you to explain this function, I do not understand this syntax: double sum_arithmetic(double x0, double r, int n) { return n == 0 ? 0 : x0 + sum_arithmetic(x0 + r, r, n-1); }…
-
9
votes2
answers13617
viewsPython - 'int' Object is not iterable
Hi, I’m creating a character converter for your ascii code through a recursive Python function. However I have a problem "'int' Object is not iterable". This is my code: def cod(n): for i in n: i =…
-
8
votes4
answers5555
viewsDetermine the nth Fibonacci term with recursion
I don’t understand anything about recursive functions, even debugging, is very confusing for me. Someone can explain me in an easy way? I tried to analyze the following code: #!/usr/bin/python def…
-
8
votes1
answer304
viewsRecursiveness in Python
Next guys, I have a matrix that is a naval battle board represented by the list: tab = [['.', '.', '#', '#', '#'], ['.', '.', '.', '.', '.'], ['#', '#', '#', '#', '#'], ['.', '.', '.', '.', '.'],…
-
7
votes1
answer4415
viewsRecursive function simulating a number elevated to a power
How to simulate the elevation of a number to a power using the recursion (and Python)?`
-
7
votes1
answer752
viewsHow to make a sum in Lambda with recursive logic using successor and predecessor?
I’m trying to assemble a Soma with recursive logic using just successor and predecessor in Lambda. But they’re not succeeding... In the case: suc = λx.x+1 ant = λx.x-1 I’ve done something like:…
-
7
votes2
answers238
viewsDoubt with recursiveness
I took a table test but the results will never come 0 because I subtract 1 of n but then adds the result with n. The result of this question was 36 and I don’t understand why. public class…
-
7
votes2
answers169
viewsRecursive code C++
I have the following iterative function: int getNumPastasColididas(int i){ int c = 0; for(int i = 0; i < size; i++) c += table[i].hasColided; return c; // c = 89832 } I tried to play the function…
-
7
votes2
answers174
viewsDoubt in recursive function
When I put: return n * fatqua(n-1) the program returns the expected result that is 24. But when I put: return n * fatqua(--1) the result of the program is 0. I’m not getting the logic of this…
-
7
votes3
answers1161
viewsHanoi Tower - How does this recursive solution work?
Could someone explain to me the logic of this recursive function? I’m not getting the idea from if down. The code solves the problem of the Tower of Hanoi: def toweOFhanoi(disc,ori,dest,aux): if…
-
6
votes1
answer237
viewsRecursive function for integer number input
I have this code but it doesn’t work very well, I mean, if it’s whole at first numOfValues is correct, but if it is not gets the type None, for what remains in memory is the first input (which is…
-
6
votes1
answer1150
viewsRecursive analysis of a vector
QUESTION: Write a function recursive that analyzes the elements of a vector and returns one of the following codes: 0 Disordered elements 1 Elements sorted in ascending order 2 Constant elements 3…
-
6
votes2
answers606
viewsWhat is a tail recursion?
In that question I asked about performance. One of the users replied that the compiler does several interesting optimizations, such as inlining, Loop unwinding, tail recursion and caches. How Tail…
-
6
votes2
answers2487
viewsWhat is a recursive function?
I’ve always heard that recursion is an intelligent idea that plays a central role in functional programming and computer science in general. From what I understand, briefly, there is recursion…
-
6
votes3
answers234
viewsGlobal variables recursion
I’m learning recursion and have doubts about using global variables, particularly I think a gambit inelegant, maybe I’m wrong. I made a code to add positive numbers and used a variable called sum. I…
-
6
votes2
answers583
viewsA recursive function can replace while and for?
If so, how could I, for example, create a recursive function equivalent to the code below? lista = list(range(1000)) for i in lista: print(i)
-
6
votes0
answers164
viewsHow to handle "large iterations" using recursion?
In the context of functional programming, it is said that any type of loop repeats (such as for or while) should be set aside in favor of recursion. But as the number of elements of the work begins…
-
5
votes3
answers2176
viewsCommand to replace characters recursively
I need a command that overwrites a specific pattern on each line of a file as many times as necessary until the pattern is no longer found. For example, in a CSV file, fields are separated by a…
-
5
votes3
answers8270
viewsConvert natural number to binary recursively
How can I turn this function into a recursive function? I managed to make it iterative in this way: #include <iostream> using namespace std; int main (){ int n,pot,bin; cout << endl…
-
5
votes1
answer80
viewsView recursive calls in eclipse
Is there an eclipse tool that shows recursive calls like in this image? Where I can see the details of preference graphically?…
-
5
votes5
answers11043
viewsRecursive Fibonacci printing
I’m having problems with the recursive function Fibonacci, in the exercise you ask to print inside the function or even using an auxiliary recursive function, but you can’t print in the main…
-
5
votes2
answers648
viewsHyperfactor in C by recursion
I’m trying to make a program with a recursive function to return a hyperfactor of a number in C, but the only value the function returns is 1. Where I’m missing? #include <math.h> #include…
-
5
votes2
answers1175
viewsHow to get the representation of a positive integer in binary, using recursion?
I searched several sites, and in none I could understand how to make a conversion code using recursion from a positive integer to binary. I know there’s a function itoa but the challenge of the…
-
5
votes3
answers182
views -
4
votes2
answers2140
viewsHow to "clean" static variables?
In C++ static variables are very important. Let’s assume that I want to make a factorial that uses recursion. unsigned long long int RecursionFatorial(unsigned int fator) { static long long int…
-
4
votes1
answer5879
viewsRecursive query
I need to mount a recursive query to solve a simple problem, but I’m having a little trouble. I have a table called TABELA1 with the following fields (ID, IDPAI, NOME) I will put here some examples…
-
4
votes2
answers421
viewsHow to write a recursive function?
I’ve been tasked with making this function recursive, but I have no idea how to do that. int existe (int x){ FILE *arq; int y; char buf[MAX]; arq = fopen ("cliente.txt","r"); fgets (buf,MAX,arq);…
-
4
votes1
answer2492
viewsRecursive functions in C++: examples
I am starting to learn C++ and I am currently focusing on recursive functions. I’ve seen some interesting examples, like the factorial calculation of a number, but I’d like to see other examples. I…
-
4
votes4
answers3096
viewsrecursive superfatorial problem
I have a doubt in the realization of this recursive function of mathematics. Calculation of the superfatorial: The superfactor of a number N is defined by the product of the first factorial N of N.…
-
4
votes2
answers102
viewsDoubts related to recursion
Good afternoon. I am developing recursion methods that work on a binary tree of research, but I am having great difficulties in recursion. For example, the method below should return the tree…
-
4
votes3
answers4922
viewspython recursive function to compute sum of the elements of a list
I’m having difficulty in an exercise where I have to show the sum of all the elements of a list recursively. The code I arrived has only the basis of the recursion, the recursion itself did not do,…
-
4
votes1
answer4003
viewsRecursive binary tree and leaf sum
Friends I’m having trouble solving this exercise, and I don’t know how else to do it. I even implemented the tree with recursiveness, but I couldn’t leave the NODE empty and some leaves with number,…
-
4
votes1
answer47
viewsWhat is the name of a non-retail recursion?
In tail recursion, the recursive call is the last thing the function does, and can be optimized. And when is it not tail? What is the name? "Common"? "Body"? There must be a specific name for it!…
-
4
votes2
answers1240
viewsRecursion to Python List Inversion
I’m trying to create a recursive function that reverses the order of a certain list. As a beginner, I believe I’m making mistakes during the construction of the function. def inverter(lista, size):…
-
4
votes1
answer1052
viewsBank Draft Algorithm in C using recursion
I am having doubts about a bank withdrawal algorithm in C. First of all it is better to pass the statement: Atms in banks are a great invention, but sometimes we need change and the machine delivers…
-
4
votes1
answer213
viewsStackoverflow error when compiling code in C#
I am doing an exercise in C# and I am not able to find the error in my code, because I have already checked the resolution of the problem and I can’t find the difference in the syntax of the code…
-
4
votes2
answers98
viewsHow do I know if a value is higher or lower than another value in a list using recursion?
Being a classified monster with your name, attack and defense. ("medusa", 2, 5) or (string, int, int). And getting a list of monsters, how do I create a function that tells me if the monster I want…
-
3
votes1
answer673
viewsWhy this function shows the list before returning but returns None
I’m writing a function that capitalizes all the string elements of a list recursively, but it returns "strangely" None, but before returning correctly shows the capitalized list: def…
-
3
votes1
answer2412
viewsSelect recursive in mysql data hierarchy
How to build a mysql function or recursive query to bring the last descending of a specific side of a binary tree? Satisfying a binary tree, each node can have only two branches, one left and one…
-
3
votes2
answers1044
viewsString corresponding to real number
I am trying to check if a String x corresponds to a real number (any one). For this I created the following method: public static boolean Real(String s, int i) { boolean resp = false; // if ( i ==…