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
-
2
votes0
answers83
viewsSqlite recursive query
I’m starting with sqlite and would like help with a recursive query. I have 3 tables (generic example): CREATE TABLE IF NOT EXISTS tab_componente ( id_comp TEXT NOT NULL PRIMARY KEY, desc_comp TEXT…
-
2
votes2
answers505
viewsCount in recursive function
I need to count how many times a comparison is true within a recursive function, but I don’t know how to do it, because any counting variable will be reset when starting the code. def…
-
2
votes0
answers24
viewsDoubt in Javascript merge debug
I’m studying data structures with Javascript and so far everything was ok. Only when I got to the sorting algorithms, more specifically the merge Sort, I ended up crashing, I can’t understand the…
-
2
votes1
answer134
viewsRecursion Doubt with String in Java
I’m trying to solve the following exercise: A string collection S is recursively defined by: 'a' and 'b' belong to S; if X belongs to S, then Xb also belongs to S; Write a procedure that implements…
-
2
votes1
answer70
viewsWhy is this C code not working (binary tree)?
Good morning, I’m trying to create a recursive function that traverses a binary tree and finds the largest integer contained in the nodes. My idea is to move to the function the pointer to the node…
-
2
votes1
answer61
viewsHow does this code of turning the number into binary work?
I’m trying to understand this function that calls itself, but it doesn’t enter into the head as if it can transform a number into binary without at least a repetition, as it works? static int…
-
2
votes1
answer1543
viewsCalculate pi with python and recursion
Calculate pi by formula pi=(s*32)**(1/3), being s=(1/1^3)-(1/3^3)+(1/5^3)... Amount of s determined by the user. It should be developed in python with use of recursivity. I’m not getting the…
-
2
votes1
answer47
viewsThe function returns None although the variable saves the correct result
I must do a function that returns the odd elements of a list using recursion. I saved the list with the odd ones in the variable I, and when return this variable the result is None. def…
-
2
votes2
answers611
viewsJava recursive function to calculate: e = 1 + 2/1 + 3/2 + 4/3 + 5/4 + ... + n/(n-1)
My code: /** * Funcao para calcular: e = 1 + 2/1 + 3/2 + 4/3 + 5/4 + ... + n/(n-1) * * @param termos - quantidade de termos do somatorio * * Teste: * para termos = 4 * soma = 1 + 2 + 1.5 + 1.33... *…
-
2
votes1
answer383
viewsProblem when removing all attributes of an object containing null values
Context I’m inside an application in Nodejs, where I have a very large object that has various attributes and also attributes objects that have children, and I have a situation where I can’t keep…
-
2
votes1
answer459
viewsVery slow recursive Fibonacci method, what is the cause?
I made a program with recursiveness, but when the user type 50, the processing to generate the Fibonacci sequence is now over extremely slow. Is it due to data input/output processing on the…
-
2
votes1
answer613
viewsCount how many occurrences of one number there are in another, recursively
Using Python, write a recursive function that determines how many times a number k 2-digit occurs in a natural number n. For example, the number 21 occurs twice in 2135219877. The problem with my…
-
1
votes1
answer47
viewsDisplay contents of a list recursively
We’re studying recursion in our programming course, and we have an exercise where we have to show in Idle the contents of a list using recursiveness. I did so: def print_list_r(ls=[], n=0): if…
-
1
votes1
answer216
viewsJava permeability
I’m having some difficulty solving a recursive problem, which I explain below. Given an array of n rows and columns, check for permeability, i.e., having the following matrix (where '*' - represents…
-
1
votes1
answer1440
viewsRecursive function to end a list
I want to do a recursive function to offset the memory of each block in the list, but when I print it after using the function, it loops and memory addresses are printed when asked to print (the…
-
1
votes2
answers118
viewsHow to improve this "Flattening" function from a list
At school we are studying recursion, but of course this is never obvious. We have to create a function that "Flatten" the list. I’ve seen something on the net, and I’ve solved mine in this way:…
-
1
votes2
answers515
viewsHow to create a recursive algorithm to find the depth of a list
I’m trying to create a recursive algorithm to find the depth of a list, which is the largest number of nested lists, But this is complicated, I can’t find it. I understand the path that the…
-
1
votes1
answer568
viewsIs there any software that analyzes the complexity of an algorithm?
is there any software that does the complexity analysis of an algorithm? if yes, I would like to know which
-
1
votes2
answers437
viewsSorting in recursive query
In another question I asked i had found a solution but ordering only works if the ID’s are growing: USE TESTE GO WITH Niveis AS ( -- Membro âncora SELECT Id, IdPai, convert(varchar(1000), Nome) as…
-
1
votes2
answers69
viewsRecursive problem when integrating files
I am doing a recursive integration of several files,when running with only one file it integrates normal, but when I try to integrate several files it gives this error. {"An error occurred while…
-
1
votes2
answers124
viewshow do I make a table go showing tables
wanted to do a function that makes kind of something like the thing below for i in pairs(v) do if type(v[i])=="table" then for j in pairs(v[i]) do if type(v[i][j])=="table" then print("...") else…
-
1
votes1
answer104
viewsStackoverflowerror in recursive method when traversing folders and subfolders
This recursive method: public static void percorre(File caminho, String espaço){ if(caminho.isDirectory()){ for (int cta = 1; cta <= nespaço; cta++){ buffer.append(espaço); }…
-
1
votes2
answers234
viewsWhy is the recursive function return not being used?
What happens when I call a function, recursively, without assigning its return to a variable? Why there is no need, in this case, to assign the function mergeSort explicitly the variable array?…
-
1
votes1
answer88
viewsSeries using recursiveness
I’m trying to make a show x^(n)/n! but although it seems simple I have a constraint that is: It has to be a recursive function. What I’ve got so far that I don’t even know if it’s right is this::…
-
1
votes1
answer102
viewsRecursion and sum
I am making a program in which you need a recursion according to the exercise that sums a vector of two matrices and sums the rows after the recursion and then add the columns to my code #include…
-
1
votes2
answers803
viewsRecursive search for files in directories
I have a function that searches file passed by parameter in directories using C language on Linux, I am able to enter the subdirectories and do the search, but when finishing the files in the…
-
1
votes1
answer41
viewsDoubt regarding the passage of parameters
In the code below, in line 5, the function foo1t calls the function foo1 passing two parameters to it, however, the function foo1 has only one parameter. Is it possible to do this or the code is…
-
1
votes1
answer72
viewsProblem of resourcefulness
How do we fix this? I am trying to make a program that receives a path and translates in directions to follow (up(0),right(90),left(270),low(180)). The predicate vizinha of the Dir one of the…
-
1
votes2
answers292
viewsProblem with recursive function in array
I have a function (which I found here in the forum) that does a search in the array and returns whether it has the value I seek or not, so far so good. Only that a need has arisen, I need to return…
-
1
votes1
answer530
viewsFunction returning None when finished
The function, when undoes recursion, returns None, and I don’t know why. def fat(n): if n == 0 or n == 1: return 1 else: return n * fat(n - 1) def superfat (n, x = 1): if n > 0: x*=fat(n) n-=1…
-
1
votes1
answer725
viewsHow this recursive function (factorial function) behaves in Python
I don’t understand how the code behaves after it leaves the recursive function on line 7, fat = n * fatorial(n-1), i.e., how the code assigns the value to the variable n of this line and how is done…
-
1
votes1
answer3860
viewsRecursion in C: Sum
I’m learning Recursiveness in C and need to make a recursive function that returns the sum of a number n whichever. The prototype of the function is float somatorio(int n) and the sum to be…
-
1
votes2
answers649
viewsRecursion in Python 3
I’m having trouble organizing a recursive summation formula in Python. I need to calculate: My code: def seq1(n): if n == 1: return n return seq1(n-1)+(-1**(n+1)/n) I keep getting the same error for…
-
1
votes1
answer820
viewsBinary Tree with In-Order and Pre-order Path
I’m having some doubts about the route taken by this binary tree: Could you classify it as binary? Since it has 3-leaf knots?…
-
1
votes3
answers624
viewsDoubt about recursiveness in C
I’m learning about recursion, using the C language, and I have to do the following exercise: Design a recursive function that takes an integer n and compute the sum of the digits of n. For example:…
-
1
votes1
answer3225
viewsCount nodes in binary trees
Good evening, I’m studying binary trees and I came across an exercise that asks me to count the knots. I did it by adapting an algorithm that calculates height and works... int conta_nos (arvore *r)…
-
1
votes2
answers672
viewsAllocation overflows memory in recursive function in C
I created a program that keeps allocating 4 in 4 bytes of memory successively through a recursive function: #include <stdio.h> #include <stdlib.h> #define BUF 2 void overflow(int…
-
1
votes1
answer57
viewsInline in an infinite recursive function
What happens to the program if I declare an infinite recursive function inline?
-
1
votes2
answers1557
viewsNumber of recursive function calls
Hello. How could you know how many times a function is called recursively by its code? For example, in the code below, how to know how many asterisks the function prints for a given value n? int…
-
1
votes1
answer130
viewsRecursive function of natural numbers is showing negative
I need a recursive function (exercise) that prints all natural numbers between A and B: def cont(a, b): if a < b: print(a) cont(a + 1, b) cont(-3, 12) The problem is that the way I did, the…
-
1
votes2
answers841
viewsProblem with recursiveness and pointers
I am struggling to resolve the following issue: Make a recursive function that allows you to sum the even elements of an integer vector, you must use pointers to traverse the vector My code is not…
-
1
votes1
answer54
viewsrecursive function error in c++
This recursive function should calculate the multiplication of two integers but it is always returning +1 in the result, someone can help me? int multiplic(int m1, int m2){ if(m2==0){ return 1; }…
-
1
votes2
answers672
viewsRecursive function
Can anyone help me with this recursive function? It’s for her to calculate the maximum common divisor between two numbers but I don’t know what’s wrong with my function. int mdc(int m, int n){ int…
-
1
votes1
answer86
viewsHow does this function work?
It’s part of an sorting algorithm, reading the code made it seem to me that the function never makes the call: sort(mid+1, high); gets into loop at first sort(low, mid); until you leave the if, and…
-
1
votes1
answer87
viewsProgram with Recursion in C
I need to make a program that is a game, where the user will input a number and I must determine if it is possible for the user to win the game through a printf saying "Yes" or "No", it is not…
-
1
votes1
answer42
viewsWhy does this method return me 2?
I have a code to analyze and I’m not sure understand why this method of my Binary Tree returns me 2. Code: class No: def __init__(self, dado): self.esq = None self.dir = None self.dado = dado class…
-
1
votes1
answer781
viewsSize of Binary Tree
I need to develop a method to calculate the size of a Binary Tree but I’m getting compiler error. Code: class No: def __init__(self, dado): self.esq = None self.dir = None self.dado = dado class…
-
1
votes2
answers100
viewsRecursive program that accepts a non-negative integer as input and displays its vertically stacked digits
I developed the recursive function vertical(), which accepts a non-negative integer as input and displays its vertically stacked digits. For example: >>> vertical(3124) 3 1 2 4 Solution:…
-
1
votes2
answers75
viewsCounting the number of executed operations over the same input for a recursive call
idea: Compare a recursive version of the exponentiation implementation with a non-recursive version. Recursive version: def power(base,expoente): global cont if base == 1: return 1 elif expoente ==…
-
1
votes1
answer113
views