I am new to recursion and I found the following Java problem: Write a function that gets an integer n, and prints the numbers 1!,2!,3!,…,n!. Here is what I did and I would like to know if this is …
Tag: recursion
Knight tour problem – order of moves affect performance
I was trying to implement a solution for knight tour problem. I faced an interesting issue. I would like to know the reason for the behavior. This is the code – it works fine. Now we could run this, starting with knight position as (2, 2) in the matrix When i tried to run the starting position as (0, 0)
How to print odd numbers using recursive java with the limits = n
I’m a newbie of programming languages. I have the following code if i make the limits is 7, the output should be : the output : 1, 3, 5, 7 Answer You can use below code: if you dont have any library other than jdk.
How multiple recursion works? [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago. This post was edited and submitted for review 4 months ago and failed to reopen the post: Original close reason(s) were not resolved Improve this question I need
how to traverse a Boolean recursion array
The exercise: Build a recursion(with no loops) that every cell that you go inside is the number of steps that you can go, it could be right/left until you get to the last cell. if you can’t get to the last cell return false, else return true. you must start from index 0. My problem: I build the program, it’s
How to make this recursive function faster in Python or Java?
I have this recursive function: F(n)=4F(n-1)+F(n-2), for all n>=2, where F(0)=0 and F(1)=1. This is my code in python def f(n): res = 0; if n == 0: return 0 elif n == 1: …
Reverse Linked-List Recursive
I’ve traced through my code to reverse a linked-list using recursion, and I cannot find anything wrong with it, but I know it does not work. Can anyone please explain why? Answer Below is a working version of your code, added with some helping structures:
Java: Find out if a number is prime recursively
I’m writing a function that returns true if a number is prime, and false otherwise Here is my current code: It works for a lot of test cases except numbers like “1000000007” where I get an Out of memory error. How could I tweak this code to be more efficient space-wise? Answer The first problem I see is that your
More recursive method to print Pascal’s triangle
I’ll try to use a recursive method to print pascals triangle to the standard output. I started by making an iterative method to get an idea of how I wanted the method to work. See the code below. Javadoc and the signature of binom Then i began to work on the recursive method. I can not use any class variable
Fibonacci Memoized/Dynamic Programming in Java
So this is some code to calculate the Fibonacci sequence with memoization. What confuses me is when we check if memo[i]==0. I understand that Java arrays are initialized to zero and thus if memo[i] == 0 this may mean that the computation for memo[i] has not yet occured. However, one of the return values for this fibonacci function is 0.