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 8 years ago.
Improve this question
I need to use recursion to calculate how many medals a player gets for example if I enter 3 the player got 8 medals [Ex1.(3+(3-1)+3)=(3+2+3)=8]/[Ex2.(5+(5-1)+5)=(5+4+5)=14] When I enter 1 on the main method to test it works but when I change a number greater than 1 it crashes and I get red letters as error this is the error I get. I have done about 5 recursion methods already but I'm stuck on this one.
java.lang.StackOverflowError
at RecursiveFunctions.countMedals(RecursiveFunctions.java:87)
public class RecursiveFunctions{
public static int countMedals(int n){
if(n==0){
return 1+(1-1)+1;
}
else{
return countMedals((n)+(n-1)+(n));
}
public static void main(String[] args){
System.out.println("Number of Medals: " + RecursiveFunctions.countMedals(3));
}
}
Try tracing through the function to see what happens:
countMedals(3) returns countMedals(3 + 2 + 3)
countMedals(8) returns countMedals(8 + 7 + 8)
This is going to continually grow, and never hit your base case of 0.
Your formula has no recursive logic. There is no iteration, it's just a simple math equation.
public static int countMedals(int n){ return 3*n - 1;}
You recursive method has no endpoint in the recursive calls.
You call:
countMedals((n)+(n-1)+(n))
If n is 2, this expression evaluates:
countMedals((2)+(2-1)+(2)) -> countMedals(5)
And this is:
countMedals((5)+(5-1)+(5)) -> countMedals(14)
And so on... You never stop. So you stack is full, and Java crashes.
From your code, it looks like you will never reach the case where n == 0. This is why you are entering infinite recursion.
Each time you call your function with n + (n - 1) + n, which is essentially 3n - 1. So what you have is f(n) = 3n - 1, and you're passing the result of that into the function itself. Now f(n) = 0 only when n = 1 / 3. Can you see any way where n can ever be 1 / 3?
Also, try plotting this function on a graph. What do you see happening to the y values as you keep increasing x (basically n in this case)?
I don't see how you can apply recursion like this. To compute f(n) you need to compute f(3n-1), for which you need to compute f(f(3n-1)), etc. It never terminates, so you get a stack overflow.
In fact I don't know why you are recursing at all, if f(3) and f(5) both = 3n-1. Evidently you haven't understood your problem correctly.
Related
I know that lowkey it does 1 + 2 + 3 + 4 = 10, but I want to know how exactly it does that
public class Main {
public static int sum(int n) {
if(n == 0) return 0;
return sum(n - 1) + n;
}
public static void main(String[] args) {
System.out.println(sum(4));
}//main
}//class
public static int sum(int n) {
if(n == 0) return 0;
return sum(n - 1) + n;
}
When you call sum(4), the compiler does the following steps:
sum(4) = sum(3) + 4, sum(3) then calls sum(int n) and go to next step
sum(3) = sum(2) + 3, sum(2) then calls sum(int n) and go to next step
sum(2) = sum(1) + 2, sum(1) then calls sum(int n) and go to next step
sum(1) = sum(0) + 1, sum(0) then calls sum(int n) and go to next step
sum(0) = 0, return the value and bring it to previous step.
Then with backtracking, the compiler brings the value of sum(0) to the formula sum(0) + 1, so the value of sum(1) is 1. And so on, finally we get sum(4) is 10.
The key to understanding how this recursion work is the ability to see what is happening at each recursive step. Consider a call sum(4):
return
sum(3) + 4
sum(2) + 3
sum(1) + 2
sum(0) + 1
return 0 in next recursive call
It should be clear how a sum of 10 is obtained for sum(4), and may generalize to any other input.
Okay so lets understand it :
you call the method from main method passing the argument as 4.
It goes to method , the very first thing it checks is called as base condition in recursion . Here base condition is if n == 0 return 0.
We skipped the base condition since n is not yet zero . we go to return sum(n-1)+n that is sum(4-1)+4 . So addition will not happen , because you made the recursive call again to sum method by decrementing the n value to n-1 , in this case it is 3.
You again entered the method with n =3, check the base condition which is not valid since 3 != 0 , so we go to return sum (n-1)+3 , which is sum(3-1)+3
Next recursive call where n = 2 , base condition is not valid 2!=0 , so we return sum(n-1)+2that is sum(2-1)+2.
Next call with n = 1 , base condition is not valid , we go to return sum(n-1)+1 that is sum(1-1)+1.
Next recursive call with n = 0 , so now base condition is met , means it is time to stop the recursion and keep going back to from where we came to get the desired result. So this time we returned 0.
Lets go back to step 6 , with 0 we got and compute the addition part of sum(1-1)+1 . You got sum(1-1) => sum(0) = . So sum(1-1)+1 will be equal to 0+1=1
One more step back with 1 as value to step 5 , where we have sum(2-1)+2 = sum(1)+2 , sum(1) you know , which is 1 , so we will return 1+2=3 from this recursive call.
One step back with value as 3 , to step 4 , sum(3-1)+3 = sum (2)+3 = 3+3 =6 .
Going one step back with 6 as value to step 3 , sum(4-1)+4 = sum(3)+4 = 6+4 = 10 . And that is where we started from . You got the result as 10.
Recursion itself is very easy to understand.
From a mathematical point of view, it is just a simple function call, such as your code:
public static int sum(int n) {
if(n == 0) return 0;
return sum(n - 1) + n;
}
/*
sum(0) = 0
sum(1) = 1
sum(n) = n + sum(n-1)
*/
In fact, the concept of recursion has been introduced in high school. It is the "mathematical construction method" that is often used to prove sequence problems. The characteristics are obvious: the structure is simple and the proof is crude. As long as you build the framework, you can prove it in conclusion. So what is a recursive "simple structure" framework?
Initial conditions: sum(0) = 0
Recursive expression: sum(n) = sum(n-1) + n
And in fact about the sum() function, every calculation starts from sum(0), and it is natural. Even if you are asked to calculate sum(1000), all you need is paper, pen, and time, so recursion itself is not difficult.
So why recursion give people an incomprehensible impression? That's because "recursive realization" is difficult to understand, especially using computer language to realize recursion. Because the realization is the reverse, not to let you push from the initial conditions, but to push back to the initial conditions, and the initial conditions become the exit conditions.
In order to be able to reverse the calculation, the computer must use the stack to store the data generated during the entire recursion process, so writing recursion will encounter stack overflow problems. In order to achieve recursion, the human brain has to simulate the entire recursive process. Unfortunately, the human brain has limited storage, and two-parameter three-layer recursion can basically make you overflow.
Therefore, the most direct way is to use paper to record the stacks in your head. It is very mechanically painful and takes patience, but problems can often be found in the process.
Or, go back to the definition of recursion itself.
First write the architecture and then fill it in. Define the exit conditions and define the expression.
Second implement the code strictly according to the architecture. Recursive code is generally simple enough, so it is not easy to make mistakes in implementation. Once there is a problem with the program result, the first should not be to check the code, but to check your own definition.
Meeting Infinite loop? The initial conditions are wrong or missing; wrong result? There is a problem with recursion. Find out the problem, and then change the code according to the new architecture. Don't implement it without clearly defining the problem.
Of course, it really doesn't work. There is only one last resort: paper and pen.
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 6 years ago.
Improve this question
I understand everything this method does up until m=1 and n=1. What happens after if() when m=1 and n=1 is problematic.
public class exc {
public static void main(String[] args) {
System.out.println(prod(1, 4));
}
public static int prod(int m, int n) {
if (m == n) {
return n;
} else {
int recurse = prod(m, n - 1);
int result = n * recurse; // how n gets its value here?
return result;
}
}
}
What is the value of recurse? I mean how prod yields only one
integer? (since it has two integers)
In if, when m=1 and n=1 return value is 1 so why the program doesn't terminate at there? And instead it terminates after "return result;" in else?
After m=1 and n=1, n is 2 (in recurse) ; how is n set to 2? Is it because 2 is still in memory and had to be dealt with?
I checked this page
I also used println after different lines to see the out put, but that didn't help either. I also used a debugger.
Your program return the factorial when m = 1.
Take this example: (m=1, n=4)
4 != 1 -> so you call recursively prod(1, 3)
3 != 1 -> so you call recursively prod(1, 2)
2 != 1 -> so you call recursively prod(1, 1)
1 == 1 -> you return 1 to the last call
In this call, recurse = 1 and n = 2 so you return 2 to the upper call
Here, recurse = 2, n = 3, so you return 6 to the upper call
Here, recurse = 6, n = 4, so you return 24 to the upper call
END OF FUNCTION, result is 24 which is 4!
Every time you are calling the function prod recursively, the current function prodA is paused (saving the values of its variables in memory) to execute the new function prodB until prodB returns something to prodA. Then A continues to execute itself (loading back its values from memory)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My teacher makes us do these error fixing assignments, and I just can't fix this one. That's why I came here. I want to ask for one favor and a few questions
Questions: What does the error message mean in detail(I just want to know for my own curiosity), Why does it occur specifically using my code, How can I fix it
Favor: Can u please fix my code, Sorry if this seems like a way to get an answer but I'm a bit lost.
Here's the code
public class Recursion
{
public static void indifferent( int x, int y )
{
System.out.print( x );
indifferent( x - 1, y + 2 );
System.out.print( y );
}
}
public class RecursionDriver//-the driver class
{
public static void main( String [] args )
{
Recursion.indifferent( 7, -1 );
}
}
If you examine the code it should be obvious that it can never terminate. Every call to indifferent makes another call to indifferent. Unconditionally.
The StackOverflowError happens because the nested calls have gotten too deep. Java method calls use a stack to hold local variables (and the return address, and other stuff). The stack has a fixed size. If you have too many nested calls, the stack overflows.
How to fix it? Rewrite the code to do what it is currently doing, but without using recursion.
But first you need to understand what it is doing. How do you do that? Well ... basically ... you read the code.
Well I'm not completely sure what indifferent means because this is what my teacher told us to put as the name for the class(she said its meaningless and told us to ignore it)
Yup. Ignore the name, and read what the code actually does. If you can't figure it out ... then "hand execute" it using a pencil and paper.
In recursion you try to divide your problem in sub problems and try to compute the result. Hence, in recursion you always need to have a base case. Base case in the final depth of your recursion. Once you reach the base case you start coming out of the recursion and get you final answer.
In your code:
public static void indifferent( int x, int y )
{
System.out.print( x );
indifferent( x - 1, y + 2 );
System.out.print( y );
}
You do not have a base case, do your code just keeps going deep in recursion.
public static void indifferent( int x, int y )
{
if(x == 1) //example of a base case
return;
System.out.print( x );
indifferent( x - 1, y + 2 );
System.out.print( y );
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My job is to write a recursive version to this method. From what I understand Recursion is starting with a base call (if something then return) followed by an else which unwinds back to the original base. Like starting with a deck, adding on to the deck then removing cards from the deck until you are back to the original deck.
With that in mind here it is.
public static long fact(int n)
{
long result = 1;
while(n > 0)
{
result = result * n;
n = n - 1;
}
return result;
}
//my recursive version:
public static void recFact(int n)
{
if(n==0)
{
return n; // ir 0 it really doesn't matter right?
}
else
{
return recFact(n-1);
}
}
This is just an example test problem for an exam I have coming up, just want to make sure I have a handle on recursion. Did I do this right? If not what am I missing? please no answers in questions, just tell me what I did wrong and maybe some advice on better ways to understand it.
Thanks.
No, this recursive solution is not correct.
For every positive n, you're just return rectFact(n-1), which will recourse until you reach 0, at which point it will return. In other words, your function will always return 0. You're missing the part where you multiply the current n with rectFact(n-1). Additionally, note that 0! is 1, not 0:
public static int recFact(int n)
{
if(n==0)
{
return 1;
}
else
{
return n * recFact(n-1);
}
}
And finally, since the if clause returns, the else is somewhat redundant. This doesn't affect the method's correctness, of course, but IMHO the code looks cleaner without it:
public static int recFact(int n)
{
if(n==0)
{
return 1;
}
return n * recFact(n-1);
}
Your recursive version does no multiplication, and it will return zero for any input. So no, you didn't do it right.
But, the recursive version DOES recurse, so you have that going for you! To understand what's going wrong, walk through a very simple case.
Client calls recFact(3)
This will return to client recFact(2)
Which will return to above recFact(1)
Which will return to above recFact(0)
Which will return to above 0.
There are two major things going wrong:
Your base case is wrong (zero is too low)
You're not doing any multiplication
Good attitude about not wanting the solution handed to you! Hopefully these pointers wil help you figure it out.
EDIT: Apparently I misunderstood your grammar and you did want the solution.
Any recursive function needs three things:
The terminating condition: This tells the function when to stop calling itself. This is very important to avoid infinite recursion and avoid stack overflow exceptions.
The actual processing: You need to run the actual processing within each function. In your non recursive case, this was result = result * n. This is missing from your recursive version!
A collector/agggregator variable: You need some way to store the partial result of the recursive calls below you. So you need some way to return the result of recFact so that you can include it in processing higher up in the call chain. Note that you say return recFact(n - 1) but in the definition recFact returns void. That should probably be an int.
Based from your example you are missing the return type of your recFact which is int
Also recFact will always return 0 because you are not multiplying n each time to the recursion call of the method.
There are two ways to write recursive routines. One is the "standard" way that we all are taught. This is one entry point that must first check to see if the recursive chain is at an end (the escape clause). If so, it returns the "end of chain" value and ends the recursion. If not at the end, it performs whatever calculation it needs to get a partial value according to the level and then calls itself passing a value the next increment closer to the end of the chain.
private final int InitialValue = 15;
System.out.println( "Fact(" + InitialValue + ") = " + recFact( InitialValue ) );
public int recFact( int val ){
if( val < 2 ){
return 1;
}
else{
return recFact( val - 1 ) * val; // recursive call
}
}
//Output: "Fact(15) = 2004310016"
In regular recursion, a partial answer is maintained at each level which is used to supplement the answer from the next level. In the code above, the partial answer is val. When first called, this value is 15. It takes this value and multiplies it by the answer from Fact(14) to supply the complete answer to Fact(15). Fact(14) got its answer by multiplying 14 by the answer it got from Fact(13) and so on.
There is another type of recursion called tail recursion. This differs in that partial answers are passed to the next level instead of maintained at each level. This sounds complicated but in actuality, make the recursion process much simpler. Another difference is that there are two routines, one is non recursive and sets up the recursive routine. This is to maintain the standard API to users who only want to see (and should only have to see)
answer = routine( parameter );
The non-recursive routines provides this. It is also a convenient place to put one-time code such as error checking. Notice in the standard routine above, if the user passed in -15 instead of 15, the routine could bomb out. That means that in production code, such a test must be made. But this test will be performed every time the routine is entered which means the test will be made needlessly for all but the very first time. Also, as this must return an integer value, it cannot handle an initial value greater than 19 as that will result in a value that will overflow the 32-bit integer container.
public static final int MaxFactorialSeq = 20;
private final int InitialValue = 15;
System.out.println( "Fact(" + InitialValue + ") = " + recFact( InitialValue ) );
public int recFact( int value ){
if( value < 0 || value > MaxFactorialSeq ){
throw new IllegalArgumentException(
"Factorial sequence value " + value + " is out of range." );
}
return recFact( value, 1 ); // initial invocation
}
private int recFact( int val, int acc ){
if( val < 2 ){
return acc;
}
else{
return recFact( val - 1, acc * val ); // recursive call
}
}
//Output: "Fact(15) = 2004310016"
Notice the public entry point contains range checking code. This is executed only once and the recursive routine does not have to make this check. It then calls the recursive version with an initial "seed" of 1.
The recursive routine, as before, checks to see if it is at the end of the chain. If so, it returns, not 1 as before, but the accumulator which at this point has the complete answer. The call chain then just rewinds back to the initial entry point in the non-recursive routine. There are no further calculations to be made as the answer is calculated on the way down rather than on the way up.
If you walk though it, the answer with standard recursion was reached by the sequence 15*14*13*...*2*1. With tail recursion, the answer was reached by the sequence 1*15*14*...*3*2. The final answer is, of course, the same. However, in my test with an initial value of 15, the standard recursion method took an average of 0.044 msecs and the tail recursion method took an average of 0.030 msecs. However, almost all that time difference is accounted for by the fact that I have the bounds checking in my standard recursion routine. Without it, the timing is much closer (0.036 to 0.030) but, of course, then you don't have error checking.
Not all recursive routines can use tail recursion. But then, not all recursive routines should be. It is a truism that any recursive function can be written using a loop. And generally should be. But a Factorial function like the ones above can never exceed 19 levels so they can be added to the lucky few.
The problem with recursion is that to understand recursion you must first understand recursion.
A recursive function is a function which calls itself, or calls a function which ultimately calls the first function again.
You have the recursion part right, since your function calls itself, and you have an "escape" clause so you don't get infinite recursion (a reason for the function not to call itself).
What you are lacking from your example though is the actual operation you are performing.
Also, instead of passing a counter, you need to pass your counter and the value you are multiplying, and then you need to return said multiplied value.
public static long recFact(int n, long val)
{
if(n==1)
{
return val;
}
else
{
return recFact(n-1, val) * n;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So I have this small method I am working on for class and this question is a tough one and a bit confusing. Basically what I have to to is write a method that will take a number that you input and it will find the next prime number that is 1 greater than a squared number. For example if I were to type in 10 the result would be 17, because 17 is a prime number and its 1 greater than a square, the square being 16. So I have to write a method with only one return statement in it to do this, and I have absolutely no idea where to start. What I have so far ispublic static int nextSquarePlus1Prime(int num){
} Literally. Some helped would be greatly appreciated.
I already have this method where I check if the number is prime or not:
public static boolean isPrime(int num){
int i;
boolean numberIsPrime=true;
if(num==1){
numberIsPrime=false;
}
for(i=2;i*i<=num;i++){
if(num%i==0){
numberIsPrime=false;
}
}
return numberIsPrime;
}
Is there a way to add to this method with another piece of code to work alongside with this one to check if the number is a square?
So this is what i came up with for my code and i put in 10 as my number. I'm getting 50 when I should be getting 17.
public static int nextSquarePlus1Prime(int num){
int i;
int save=0;
for(i=1;i<=num;i++){
if(isPrime(i)&& i*i+1>=num){
save=i*i+1;
}
}
return save;
}
The sqrt() method may help. Here is an idea of how to use it:
int squareRoot = (int) Math.sqrt((double) input);
Input being the number you want to round. Casting the result to an int will automatically round it. It is optional. I cast the input to a double, but you only need to do so if your input in an int.
Here is an easier way to check if a number is prime:
if (input % 2 == 0){
input += 1;
}
return input;
I reccomend you look at #useSticks answer again. Using the approach he described I created a method that does exactly what I think you want.
Here is a method that finds the square root of a positive number:
public static int sqrt(double number){
double g1;
if(number==0){
return 0;
}
double squareRoot = number/2;
do{
g1=squareRoot;
squareRoot = (g1 + (number/g1))/2;
}while((g1-squareRoot)!=0);
return (int) squareRoot;
}
You should provide some additional details for more specific pointers.
In Java, there is a method "is probable prime" that may be of use to you.
Its easier to square a number than it is to find the square root, so it may make sense to find the square root of the starting number, then round up. Take that number and square it, add 1 and check for prime. If not, add 1 to the variable and try again.
Good luck.