Recursive method confusion - java

Given the below
public class Recursion {
public static int magic(int a, int b) {
if (b == 0) {
return 0;
} else {
if (b % 2 == 0)
return magic(a + a, b / 2);
else
return magic(a + a, b / 2) + a;
}
}
public static void main(String[] args) {
Recursion r = new Recursion();
System.out.println(r.magic(3,11));
//System.out.println(r.magic(2,25));
}
}
When I use r.magic(3,11) I get an output of 33 and if I use r.magic(2.25) I get an output of 50, can someone explain the math behind it to me because I can't really seem to make sense of it.

Ok, here it is.
First of all you need to know the basics of recursion like how its returns the value and what is the exit condition.
Here is the stack trace for 2 and 25. In recursion we make a stack and this is the stack for 2 and 25. The first call value of a and b is in the bottom of the stack. i.e (2,25). You need to visualize it like this only.
a b called from return magic(a+a,b/2) or return magic(a+a,b/2)+a
64 0
32 1 2
16 3 2
8 6 1
4 12 1
2 25 2
Here 1 is for calling from magic(a+a,b/2) and 2 is for calling from return magic(a+a,b/2)+a.
So while returning from the function here is stack.If it is called from 1 then it will simply return the value else it will add a with the returned value.
a b
64 0 returns 0
32 1 2 returns 32
16 3 2 returns 32+16
8 6 1 returns 32+16
4 12 1 returns 32+16
2 25 2 returns 32+16+2
So the final return statement returns 50
And for the second call similarly here is the stack trace.
a b
48 0 returns 0
24 1 2 returns 24
12 2 1 returns 24
6 5 2 returns 24+6
3 11 2 returns 24+6+3
So the final return statement returns 33
So you can see now how the result is 50 and 33.
If you have any doubt you can comment.

I guess you want some explanations, not steps of the program. Here it is:
return magic(a + a, b / 2)
is actually
return magic(2 * a, b / 2)
It means you multiply one of the numbers by 2, and divide the other one by 2. So, these operations will not change the result in Math.
ab = (2a)*(b/2)
But, you work with integers in this method. That's OK with even numbers. But, if you divide an odd number by 2, you will lose the floating part which is "0.5".
If b is odd, let's say b = 2*n+1
a*(2n+1) = (2a)((2n+1)/2)
2an + a = (2a)(n+0.5)
2an + a = 2an + a
When you work with integers, it will be that which is wrong:
2an + a = 2an
Recursively this additional a becomes 2a, 4a, ...
And you can derive a*b with summation of these multiples of a.
When b is 0, this is the end, there is no need to sum any multiples of a.

Related

What does a recursive method in java return? [duplicate]

As the title explains I have a very fundamental programming question which I have just not been able to grok yet. Filtering out all of the (extremely clever) "In order to understand recursion, you must first understand recursion." replies from various online threads I still am not quite getting it.
Understanding that when faced with not knowing what we don't know, we can tend to ask the wrong questions or ask the right questions incorrectly I will share what I "think" my question is in hopes that someone with a similar outlook can share some bit of knowledge that will help turn on the recursive light bulb for me!
Here is the function (the syntax is written in Swift):
func sumInts(a: Int, b: Int) -> Int {
if (a > b) {
return 0
} else {
return a + sumInts(a: a + 1, b: b)
}
}
We'll use 2 and 5 as our arguments:
println(sumInts(a: 2, b: 5))
Obviously the answer is 14. But I'm not clear on how that value is achieved.
These are my 2 hangups:
The function is called recursively until a condition is met. That condition is a > b. When this condition is met, return 0. At first glance, I would expect the return value to be 0 which is obviously incorrect.
Printing out the value of 'a' on each iteration yields a value which I would expect: 2, 3, 4, 5 (at which point 5+1 > b which meets the first condition: a > b) but I still don't see how the value of 14 is achieved.
My first thought is that something similar to the following is happening magically:
var answer = a;
answer += a+1 until a > b;
return answer;
So ruling out magic, I'm just not getting something. I would love to understand what's happening more than just implicitly.
If someone could kindly explain what technically happens during this kind of function and why the result isn't 0 and how, eventually, a + sumInts(a: a + 1, b: b) = 14, I would be forever in your debt.
1.The function is called recursively until a condition is met. That condition is a > b. When this condition is met, return 0. At first glance, I would expect the return value to be 0 which is obviously incorrect.
Here is what the computer computing sumInts(2,5) would think if it were able to:
I want to compute sumInts(2, 5)
for this, I need to compute sumInts(3, 5)
and add 2 to the result.
I want to compute sumInts(3, 5)
for this, I need to compute sumInts(4, 5)
and add 3 to the result.
I want to compute sumInts(4, 5)
for this, I need to compute sumInts(5, 5)
and add 4 to the result.
I want to compute sumInts(5, 5)
for this, I need to compute sumInts(6, 5)
and add 5 to the result.
I want to compute sumInts(6, 5)
since 6 > 5, this is zero.
The computation yielded 0, therefore I shall return 5 = 5 + 0.
The computation yielded 5, therefore I shall return 9 = 4 + 5.
The computation yielded 9, therefore I shall return 12 = 3 + 9.
The computation yielded 12, therefore I shall return 14 = 2 + 12.
As you see, some call to the function sumInts actually returns 0 however this not the final value because the computer still has to add 5 to that 0, then 4 to the result, then 3, then 2, as described by the four last sentences of the thoughts of our computer. Note that in the recursion, the computer does not only have to compute the recursive call, it also has to remember what to do with the value returned by the recursive call. There is a special area of computer's memory called the stack where this kind of information is saved, this space is limited and functions that are too recursive can exhaust the stack: this is the stack overflow giving its name to our most loved website.
Your statement seems to make the implicit assumption that the computer forgets what it were at when doing a recursive call, but it does not, this is why your conclusion does not match your observation.
2.Printing out the value of 'a' on each iteration yields a value which I would expect: 2, 3, 4, 5 (at which point 5+1 > b which meets the first condition: a > b) but I still don't see how the value of 14 is achieved.
This is because the return value is not an a itself but the sum of the value of a and the value returned by the recursive call.
I think the confusion is stemming from thinking of it as "the same function" being called many times. If you think of it as "many copies of the same function being called", then it may be clearer:
Only one copy of the function ever returns 0, and it's not the first one (it's the last one). So the result of calling the first one is not 0.
For the second bit of confusion, I think it will be easier to spell out the recursion in English. Read this line:
return a + sumInts(a + 1, b: b)
as "return the value of 'a' plus (the return value of another copy of the function, which is the copy's value of 'a' plus (the return value of another copy of the function, which is the second copy's value of 'a' plus (...", with each copy of the function spawning a new copy of itself with a increased by 1, until the a > b condition is met.
By the time you reach the the a > b condition being true, you have a (potentially arbitrarily) long stack of copies of the function all in the middle of being run, all waiting on the result of the next copy to find out what they should add to 'a'.
(edit: also, something to be aware of is that the stack of copies of the function I mention is a real thing that takes up real memory, and will crash your program if it gets too large. The compiler can optimize it out in some cases, but exhausting stack space is a significant and unfortunate limitation of recursive functions in many languages)
To understand recursion you must think of the problem in a different way. Instead of a large logical sequence of steps that makes sense as a whole you instead take a large problem and break up into smaller problems and solve those, once you have an answer for the sub problems you combine the results of the sub problems to make the solution to the bigger problem. Think of you and your friends needing to count the number of marbles in a huge bucket. You do each take a smaller bucket and go count those individually and when you are done you add the totals together.. Well now if each of you find some friend and split the buckets further, then you just need to wait for these other friends to figure out their totals, bring it back to each of you, you add it up. And so on. The special case is when you only get 1 marble to count then you just return it back and say 1. let the other people above you do the adding you are done.
You must remember every time the function calls itself recursively it creates a new context with a subset of the problem, once that part is resolved it gets returned so that the previous iteration can complete.
Let me show you the steps:
sumInts(a: 2, b: 5) will return: 2 + sumInts(a: 3, b: 5)
sumInts(a: 3, b: 5) will return: 3 + sumInts(a: 4, b: 5)
sumInts(a: 4, b: 5) will return: 4 + sumInts(a: 5, b: 5)
sumInts(a: 5, b: 5) will return: 5 + sumInts(a: 6, b: 5)
sumInts(a: 6, b: 5) will return: 0
once sumInts(a: 6, b: 5) has executed, the results can be computed so going back up the chain with the results you get:
sumInts(a: 6, b: 5) = 0
sumInts(a: 5, b: 5) = 5 + 0 = 5
sumInts(a: 4, b: 5) = 4 + 5 = 9
sumInts(a: 3, b: 5) = 3 + 9 = 12
sumInts(a: 2, b: 5) = 2 + 12 = 14.
Another way to represent the structure of the recursion:
sumInts(a: 2, b: 5) = 2 + sumInts(a: 3, b: 5)
sumInts(a: 2, b: 5) = 2 + 3 + sumInts(a: 4, b: 5)
sumInts(a: 2, b: 5) = 2 + 3 + 4 + sumInts(a: 5, b: 5)
sumInts(a: 2, b: 5) = 2 + 3 + 4 + 5 + sumInts(a: 6, b: 5)
sumInts(a: 2, b: 5) = 2 + 3 + 4 + 5 + 0
sumInts(a: 2, b: 5) = 14
Recursion is a tricky topic to understand and I don't think I can fully do it justice here. Instead, I'll try to focus on the particular piece of code you have here and try to describe both the intuition for why the solution works and the mechanics of how the code computes its result.
The code you've given here solves the following problem: you want to know the sum of all the integers from a to b, inclusive. For your example, you want the sum of the numbers from 2 to 5, inclusive, which is
2 + 3 + 4 + 5
When trying to solve a problem recursively, one of the first steps should be to figure out how to break the problem down into a smaller problem with the same structure. So suppose that you wanted to sum up the numbers from 2 to 5, inclusive. One way to simplify this is to notice that the above sum can be rewritten as
2 + (3 + 4 + 5)
Here, (3 + 4 + 5) happens to be the sum of all the integers between 3 and 5, inclusive. In other words, if you want to know the sum of all the integers between 2 and 5, start by computing the sum of all the integers between 3 and 5, then add 2.
So how do you compute the sum of all the integers between 3 and 5, inclusive? Well, that sum is
3 + 4 + 5
which can be thought of instead as
3 + (4 + 5)
Here, (4 + 5) is the sum of all the integers between 4 and 5, inclusive. So, if you wanted to compute the sum of all the numbers between 3 and 5, inclusive, you'd compute the sum of all the integers between 4 and 5, then add 3.
There's a pattern here! If you want to compute the sum of the integers between a and b, inclusive, you can do the following. First, compute the sum of the integers between a + 1 and b, inclusive. Next, add a to that total. You'll notice that "compute the sum of the integers between a + 1 and b, inclusive" happens to be pretty much the same sort of problem we're already trying to solve, but with slightly different parameters. Rather than computing from a to b, inclusive, we're computing from a + 1 to b, inclusive. That's the recursive step - to solve the bigger problem ("sum from a to b, inclusive"), we reduce the problem to a smaller version of itself ("sum from a + 1 to b, inclusive.").
If you take a look at the code you have above, you'll notice that there's this step in it:
return a + sumInts(a + 1, b: b)
This code is simply a translation of the above logic - if you want to sum from a to b, inclusive, start by summing a + 1 to b, inclusive (that's the recursive call to sumInts), then add a.
Of course, by itself this approach won't actually work. For example, how would you compute the sum of all the integers between 5 and 5 inclusive? Well, using our current logic, you'd compute the sum of all the integers between 6 and 5, inclusive, then add 5. So how do you compute the sum of all the integers between 6 and 5, inclusive? Well, using our current logic, you'd compute the sum of all the integers between 7 and 5, inclusive, then add 6. You'll notice a problem here - this just keeps on going and going!
In recursive problem solving, there needs to be some way to stop simplifying the problem and instead just go solve it directly. Typically, you'd find a simple case where the answer can be determined immediately, then structure your solution to solve simple cases directly when they arise. This is typically called a base case or a recursive basis.
So what's the base case in this particular problem? When you're summing up integers from a to b, inclusive, if a happens to be bigger than b, then the answer is 0 - there aren't any numbers in the range! Therefore, we'll structure our solution as follows:
If a > b, then the answer is 0.
Otherwise (a ≤ b), get the answer as follows:
Compute the sum of the integers between a + 1 and b.
Add a to get the answer.
Now, compare this pseudocode to your actual code:
func sumInts(a: Int, b: Int) -> Int {
if (a > b) {
return 0
} else {
return a + sumInts(a + 1, b: b)
}
}
Notice that there's almost exactly a one-to-one map between the solution outlined in pseudocode and this actual code. The first step is the base case - in the event that you ask for the sum of an empty range of numbers, you get 0. Otherwise, compute the sum between a + 1 and b, then go add a.
So far, I've given just a high-level idea behind the code. But you had two other, very good questions. First, why doesn't this always return 0, given that the function says to return 0 if a > b? Second, where does the 14 actually come from? Let's look at these in turn.
Let's try a very, very simple case. What happens if you call sumInts(6, 5)? In this case, tracing through the code, you see that the function just returns 0. That's the right thing to do, to - there aren't any numbers in the range. Now, try something harder. What happens when you call sumInts(5, 5)? Well, here's what happens:
You call sumInts(5, 5). We fall into the else branch, which return the value of `a + sumInts(6, 5).
In order for sumInts(5, 5) to determine what sumInts(6, 5) is, we need to pause what we're doing and make a call to sumInts(6, 5).
sumInts(6, 5) gets called. It enters the if branch and returns 0. However, this instance of sumInts was called by sumInts(5, 5), so the return value is communicated back to sumInts(5, 5), not to the top-level caller.
sumInts(5, 5) now can compute 5 + sumInts(6, 5) to get back 5. It then returns it to the top-level caller.
Notice how the value 5 was formed here. We started off with one active call to sumInts. That fired off another recursive call, and the value returned by that call communicated the information back to sumInts(5, 5). The call to sumInts(5, 5) then in turn did some computation and returned a value back to the caller.
If you try this with sumInts(4, 5), here's what will happen:
sumInts(4, 5) tries to return 4 + sumInts(5, 5). To do that, it calls sumInts(5, 5).
sumInts(5, 5) tries to return 5 + sumInts(6, 5). To do that, it calls sumInts(6, 5).
sumInts(6, 5) returns 0 back to sumInts(5, 5).</li>
<li>sumInts(5, 5)now has a value forsumInts(6, 5), namely 0. It then returns5 + 0 = 5`.
sumInts(4, 5) now has a value for sumInts(5, 5), namely 5. It then returns 4 + 5 = 9.
In other words, the value that's returned is formed by summing up values one at a time, each time taking one value returned by a particular recursive call to sumInts and adding on the current value of a. When the recursion bottoms out, the deepest call returns 0. However, that value doesn't immediately exit the recursive call chain; instead, it just hands the value back to the recursive call one layer above it. In that way, each recursive call just adds in one more number and returns it higher up in the chain, culminating with the overall summation. As an exercise, try tracing this out for sumInts(2, 5), which is what you wanted to begin with.
Hope this helps!
You've got some good answers here so far, but I'll add one more that takes a different tack.
First off, I have written many articles on simple recursive algorithms that you might find interesting; see
http://ericlippert.com/tag/recursion/
http://blogs.msdn.com/b/ericlippert/archive/tags/recursion/
Those are in newest-on-top order, so start from the bottom.
Second, so far all of the answers have described recursive semantics by considering function activation. That each, each call makes a new activation, and the recursive call executes in the context of this activation. That is a good way to think of it, but there is another, equivalent way: smart text seach-and-replace.
Let me rewrite your function into a slightly more compact form; don't think of this as being in any particular language.
s = (a, b) => a > b ? 0 : a + s(a + 1, b)
I hope that makes sense. If you're not familiar with the conditional operator, it is of the form condition ? consequence : alternative and its meaning will become clear.
Now we wish to evaluate s(2,5) We do so by doing a textual replacing of the call with the function body, then replace a with 2 and b with 5:
s(2, 5)
---> 2 > 5 ? 0 : 2 + s(2 + 1, 5)
Now evaluate the conditional. We textually replace 2 > 5 with false.
---> false ? 0 : 2 + s(2 + 1, 5)
Now textually replace all false conditionals with the alternative and all true conditionals with the consequence. We have only false conditionals, so we textually replace that expression with the alternative:
---> 2 + s(2 + 1, 5)
Now, to save me having to type all those + signs, textually replace constant arithmetic with its value. (This is a bit of a cheat, but I don't want to have to keep track of all the parentheses!)
---> 2 + s(3, 5)
Now search-and-replace, this time with the body for the call, 3 for a and 5 for b. We'll put the replacement for the call in parentheses:
---> 2 + (3 > 5 ? 0 : 3 + s(3 + 1, 5))
And now we just keep on doing those same textual substitution steps:
---> 2 + (false ? 0 : 3 + s(3 + 1, 5))
---> 2 + (3 + s(3 + 1, 5))
---> 2 + (3 + s(4, 5))
---> 2 + (3 + (4 > 5 ? 0 : 4 + s(4 + 1, 5)))
---> 2 + (3 + (false ? 0 : 4 + s(4 + 1, 5)))
---> 2 + (3 + (4 + s(4 + 1, 5)))
---> 2 + (3 + (4 + s(5, 5)))
---> 2 + (3 + (4 + (5 > 5 ? 0 : 5 + s(5 + 1, 5))))
---> 2 + (3 + (4 + (false ? 0 : 5 + s(5 + 1, 5))))
---> 2 + (3 + (4 + (5 + s(5 + 1, 5))))
---> 2 + (3 + (4 + (5 + s(6, 5))))
---> 2 + (3 + (4 + (5 + (6 > 5 ? 0 : s(6 + 1, 5)))))
---> 2 + (3 + (4 + (5 + (true ? 0 : s(6 + 1, 5)))))
---> 2 + (3 + (4 + (5 + 0)))
---> 2 + (3 + (4 + 5))
---> 2 + (3 + 9)
---> 2 + 12
---> 14
All we did here was just straightforward textual substitution. Really I shouldn't have substituted "3" for "2+1" and so on until I had to, but pedagogically it would have gotten hard to read.
Function activation is nothing more than replacing the function call with the body of the call, and replacing the formal parameters with their corresponding arguments. You have to be careful about introducing parentheses intelligently, but aside from that, it's just text replacement.
Of course, most languages do not actually implement activation as text replacement, but logically that's what it is.
So what then is an unbounded recursion? A recursion where the textual substitution doesn't stop! Notice how eventually we got to a step where there was no more s to replace, and we could then just apply the rules for arithmetic.
The way that I usually figure out how a recursive function works is by looking at the base case and working backwards. Here's that technique applied to this function.
First the base case:
sumInts(6, 5) = 0
Then the call just above that in the call stack:
sumInts(5, 5) == 5 + sumInts(6, 5)
sumInts(5, 5) == 5 + 0
sumInts(5, 5) == 5
Then the call just above that in the call stack:
sumInts(4, 5) == 4 + sumInts(5, 5)
sumInts(4, 5) == 4 + 5
sumInts(4, 5) == 9
And so on:
sumInts(3, 5) == 3 + sumInts(4, 5)
sumInts(3, 5) == 3 + 9
sumInts(3, 5) == 12
And so on:
sumInts(2, 5) == 2 + sumInts(3, 5)
sumInts(4, 5) == 2 + 12
sumInts(4, 5) == 14
Notice that we've arrived at our original call to the function sumInts(2, 5) == 14
The order in which these calls are executed:
sumInts(2, 5)
sumInts(3, 5)
sumInts(4, 5)
sumInts(5, 5)
sumInts(6, 5)
The order in which these calls return:
sumInts(6, 5)
sumInts(5, 5)
sumInts(4, 5)
sumInts(3, 5)
sumInts(2, 5)
Note that we came to a conclusion about how the function operates by tracing the calls in the order that they return.
Recursion. In Computer Science recursion is covered in depth under the topic of Finite Automata.
In its simplest form it is a self reference. For example, saying that "my car is a car" is a recursive statement. The problem is that the statement is an infinite recursion in that it will never end. The definition in the statement of a "car" is that it is a "car" so it may be substituted. However, there is no end because in the case of substitution, it still becomes "my car is a car".
This could be different if the statement were "my car is a bentley. my car is blue." In which case the substitution in the second situation for car could be "bentley" resulting in "my bentley is blue". These types of substitutions are mathematically explained in Computer Science through Context-Free Grammars.
The actual substitution is a production rule. Given that the statement is represented by S and that car is a variable which can be a "bentley" this statement can be recursively reconstructed.
S -> "my"S | " "S | CS | "is"S | "blue"S | ε
C -> "bentley"
This can be constructed in multiple ways, as each | means there is a choice. S can be replaced by any one of those choices, and S always starts empty. The ε means to terminate the production. Just as S can be replaced, so can other variables (there is only one and it is C which would represent "bentley").
So starting with S being empty, and replacing it with the first choice "my"S S becomes
"my"S
S can still be substituted as it represents a variable. We could choose "my" again, or ε to end it, but lets continue making our original statement. We choose the space which means S is replaced with " "S
"my "S
Next lets choose C
"my "CS
And C only has one choice for replacement
"my bentley"S
And the space again for S
"my bentley "S
And so on "my bentley is"S, "my bentley is "S, "my bentley is blue"S, "my bentley is blue" (replacing S for ε ends the production) and we have recursively built our statement "my bentley is blue".
Think of recursion as these productions and replacements. Each step in the process replaces its predecessor in order to produce the end result. In the exact example of the recursive sum from 2 to 5, you end up with the production
S -> 2 + A
A -> 3 + B
B -> 4 + C
C -> 5 + D
D -> 0
This becomes
2 + A
2 + 3 + B
2 + 3 + 4 + C
2 + 3 + 4 + 5 + D
2 + 3 + 4 + 5 + 0
14
Think recursion as a multiple clones doing same thing...
You ask to clone[1]: "sum numbers between 2 and 5"
+ clone[1] it knows that: result is 2 + "sum numbers between 3 and 5". so it asks to clone[2] to return: "sum numbers between 3 and 5"
| + clone[2] it knows that: result is 3 + "sum numbers between 4 and 5". so it asks to clone[3] to return: "sum numbers between 4 and 5"
| | + clone[3] it knows that: result is 4 + "sum numbers between 5 and 5". so it asks to clone[4] to return: "sum numbers between 5 and 5"
| | | + clone[4] it knows that: result is 5 + "sum numbers between 6 and 5". so it asks to clone[5] to return: "sum numbers between 6 and 5"
| | | | clone[5] it knows that: it can't sum, because 6 is larger than 5. so he returns 0 as result.
| | | + clone[4] it gets the result from clone[5] (=0) and sums: 5 + 0, returning 5
| | + clone[3] it gets the result from clone[4] (=5) and sums: 4 + 5, returning 9
| + clone[2] it gets the result from clone[3] (=9) and sums: 3 + 9, returning 12
+ clone[1] it gets the result from clone[2] (=12) and sums: 2 + 12, returning 14
and voilá!!
I'll give it a go.
Executing the equation a + sumInts(a+1, b), I will show how the final answer is 14.
//the sumInts function definition
func sumInts(a: Int, b: Int) -> Int {
if (a > b) {
return 0
} else {
return a + sumInts(a + 1, b)
}
}
Given: a = 2 and b = 5
1) 2 + sumInts(2+1, 5)
2) sumInts(3, 5) = 12
i) 3 + sumInts(3+1, 5)
ii) 4 + sumInts(4+1, 5)
iii) 5 + sumInts(5+1, 5)
iv) return 0
v) return 5 + 0
vi) return 4 + 5
vii) return 3 + 9
3) 2 + 12 = 14.
Let us know if you have any further questions.
Here's another example of recursive functions in the following example.
A man has just graduated college.
t is the amount of time in years.
The total actual number of years worked before retiring, can be calculated as follows:
public class DoIReallyWantToKnow
{
public int howLongDoIHaveToWork(int currentAge)
{
const int DESIRED_RETIREMENT_AGE = 65;
double collectedMoney = 0.00; //remember, you just graduated college
double neededMoneyToRetire = 1000000.00
t = 0;
return work(t+1);
}
public int work(int time)
{
collectedMoney = getCollectedMoney();
if(currentAge >= DESIRED_RETIREMENT_AGE
&& collectedMoney == neededMoneyToRetire
{
return time;
}
return work(time + 1);
}
}
And that should be just enough to depress anyone, lol. ;-P
A little bit off-topic, I know, but... try looking up recursion in Google... You'll see by example what it means :-)
Earlier versions of Google returned the following text (cited from memory):
Recursion
See Recursion
On September 10th 2014, the joke about recursion has been updated:
Recursion
Did you mean: Recursion
For another reply, see this answer.
One really good tip I came across in learning and really understanding recursion is to spend some time learning a language that doesn't have any form of loop construct other than via recursion. That way you'll get a great feel for how to USE recursion via practice.
I followed http://www.htdp.org/ which, as well as being a Scheme tutorial, is also a great introduction on how to design programs in terms of the architecture and design.
But basically, you need to invest some time. Without a 'firm' grasp of recursion certain algorithms, such as backtracking, will always seem 'hard' or even 'magic' to you. So, persevere. :-D
I hope this helps and Good Luck!
I think the best way to understand recursive functions is realizing that they are made to process recursive data structures. But in your original function sumInts(a: Int, b: Int) that calculates recursively the sum of numbers from a to b, it seems not to be a recursive data structure... Let's try a slightly modified version sumInts(a: Int, n: Int) where n is how many numbers you'll add.
Now, sumInts is recursive over n, a natural number. Still not a recursive data, right? Well, a natural number could be considered a recursive data structre using Peano axioms:
enum Natural = {
case Zero
case Successor(Natural)
}
So, 0 = Zero, 1 = Succesor(Zero), 2 = Succesor(Succesor(Zero)), and so on.
Once you have a a recursive data structure, you have the template for the function. For each non recursive case, you can calculate the value directly. For the recursive cases you assume that the recursive function is already working and use it to calculate the case, but deconstructing the argument. In the case of Natural, it means that instead of Succesor(n) we'll use n, or equivalently, instead of n we'll use n - 1.
// sums n numbers beginning from a
func sumInts(a: Int, n: Int) -> Int {
if (n == 0) {
// non recursive case
} else {
// recursive case. We use sumInts(..., n - 1)
}
}
Now the recursive function is simpler to program. First, the base case, n=0. What should we return if we want to add no numbers? The answer is, of course 0.
What about the recursive case? If we want to add n numbers beginning with a and we already have a working sumInts function that works for n-1? Well, we need to add a and then invoke sumInts with a + 1, so we end with:
// sums n numbers beginning from a
func sumInts(a: Int, n: Int) -> Int {
if (n == 0) {
return 0
} else {
return a + sumInts(a + 1, n - 1)
}
}
The nice thing is that now you shouldn't need to think in the low level of recursion. You just need to verify that:
For the base cases of the recursive data, it calculates the answer without using recursion.
For the recursive cases of the recursive data, it calculates the answer using recursion over the destructured data.
You might be interested in Nisan and Schocken's implementation of functions. The linked pdf is part of a free online course. It describes the second part of a virtual machine implementation in which the student should write a virtual-machine-language-to-machine-language compiler. The function implementation they propose is capable of recursion because it is stack-based.
To introduce you to the function implementation: Consider the following virtual machine code:
If Swift compiled to this virtual machine language, then the following block of Swift code:
mult(a: 2, b: 3) - 4
would compile down to
push constant 2 // Line 1
push constant 3 // Line 2
call mult // Line 3
push constant 4 // Line 4
sub // Line 5
The virtual machine language is designed around a global stack. push constant n pushes an integer onto this global stack.
After executing lines 1 and 2, the stack looks like:
256: 2 // Argument 0
257: 3 // Argument 1
256 and 257 are memory addresses.
call mult pushes the return line number (3) onto the stack and allocates space for the function's local variables.
256: 2 // argument 0
257: 3 // argument 1
258: 3 // return line number
259: 0 // local 0
...and it goes-to the label function mult. The code inside mult is executed. As a result of executing that code we compute the product of 2 and 3, which is stored in the function's 0th local variable.
256: 2 // argument 0
257: 3 // argument 1
258: 3 // return line number
259: 6 // local 0
Just before returning from mult, you will notice the line:
push local 0 // push result
We will push the product onto the stack.
256: 2 // argument 0
257: 3 // argument 1
258: 3 // return line number
259: 6 // local 0
260: 6 // product
When we return, the following happens:
Pop the last value on the stack to the memory address of the 0th argument (256 in this case). This happens to be the most convenient place to put it.
Discard everything on the stack up to the address of the 0th argument.
Go-to the return line number (3 in this case) and then advance.
After returning we are ready to execute line 4, and our stack looks like this:
256: 6 // product that we just returned
Now we push 4 onto the stack.
256: 6
257: 4
sub is a primitive function of the virtual machine language. It takes two arguments and returns its result in the usual address: that of the 0th argument.
Now we have
256: 2 // 6 - 4 = 2
Now that you know how a function call works, it is relatively simple to understand how recursion works. No magic, just a stack.
I have implemented your sumInts function in this virtual machine language:
function sumInts 0 // `0` means it has no local variables.
label IF
push argument 0
push argument 1
lte
if-goto ELSE_CASE
push constant 0
return
label ELSE_CASE
push constant 2
push argument 0
push constant 1
add
push argument 1
call sumInts // Line 15
add // Line 16
return // Line 17
// End of function
Now I will call it:
push constant 2
push constant 5
call sumInts // Line 21
The code executes and we get all the way to the stopping point where lte returns false. This is what the stack looks like at this point:
// First invocation
256: 2 // argument 0
257: 5 // argument 1
258: 21 // return line number
259: 2 // augend
// Second
260: 3 // argument 0
261: 5 // argument 1
262: 15 // return line number
263: 3 // augend
// Third
264: 4 // argument 0
265: 5 // argument 1
266: 15 // return line number
267: 4 // augend
// Fourth
268: 5 // argument 0
269: 5 // argument 1
270: 15 // return line number
271: 5 // augend
// Fifth
272: 6 // argument 0
273: 5 // argument 1
274: 15 // return line number
275: 0 // return value
Now let's "unwind" our recursion. return 0 and goto line 15 and advance.
271: 5
272: 0
Line 16: add
271: 5
Line 17: return 5 and goto line 15 and advance.
267: 4
268: 5
Line 16: add
267: 9
Line 17: return 9 and goto line 15 and advance.
263: 3
264: 9
Line 16: add
263: 12
Line 17: return 12 and goto line 15 and advance.
259: 2
260: 12
Line 16: add
259: 14
Line 17: return 14 and goto line 21 and advance.
256: 14
There you have it. Recursion: Glorified goto.
There are already a lot of good answers. Still I am giving a try.
When called, a function get a memory-space allotted, which is stacked upon the memory-space of the caller function. In this memory-space, the function keeps the parameters passed to it, the variables and their values. This memory-space vanishes along with the ending return call of the function. As the idea of stack goes, the memory-space of the caller function now becomes active.
For recursive calls, the same function gets multiple memory-space stacked one upon another. That's all. The simple idea of how stack works in memory of a computer should get you through the idea of how recursion happens in implementation.
Recursion started making sense to me when I stopped reading what others say about it or seeing it as something I can avoid and just wrote code. I found a problem with a solution and tried to duplicate the solution without looking. I only looked at the solution when I got helplessly stuck. Then I went back at trying to duplicate it. I did this again on multiple problems until I developed my own understanding and sense of how to identify a recursive problem and solve it. When I got to this level, I started making up problems and solving them. That helped me more. Sometimes, things can only be learned by trying it out on your own and struggling; until you “get it”.
Many of the answers above are very good. A useful technique for solving recursion though, is to spell out first what we want to do and code as a human would solve it . In the above case, we want to sum up a sequence of consecutive integers (using the numbers from above):
2, 3, 4, 5 //adding these numbers would sum to 14
Now, note that these lines are confusing (not wrong, but confusing).
if (a > b) {
return 0
}
Why the test a>b?, and whyreturn 0
Let's change the code to reflect more closely what a human does
func sumInts(a: Int, b: Int) -> Int {
if (a == b) {
return b // When 'a equals b' I'm at the most Right integer, return it
}
else {
return a + sumInts(a: a + 1, b: b)
}
}
Can we do it even more human like? Yes! Usually we sum up from left to right (2+3+...). But the above recursion is summing from right to left (...+4+5). Change the code to reflect it (The - can be a little intimidating, but not much)
func sumInts(a: Int, b: Int) -> Int {
if (a == b) {
return b // When I'm at the most Left integer, return it
}
else {
return sumInts(a: a, b: b - 1) + b
}
}
Some may find this function more confusing since we are starting from the 'far' end, but practicing can make it feel natural (and it is another good 'thinking' technique: Trying 'both' sides when solving a recursion). And again, the function reflects what a human (most?) does: Takes the sum of all left integers and adds the 'next' right integer.
I was having hard time to understanding recursion then i found this blog and i already seen this question so i thought i must have to share . You must read this blog i found this extremely helpful it explain with stack and even it explain how two recursion works with stack step by step. I recommend you first understand how stack works which it explain very well here : journey-to-the-stack
then now you will understand how recursion works now take a look of this post : Understand recursion step by step
Its a program :
def hello(x):
if x==1:
return "op"
else:
u=1
e=12
s=hello(x-1)
e+=1
print(s)
print(x)
u+=1
return e
hello(3)
Let me tell you with an example of Fibonacci series, Fibonacci is
t(n) = t(n - 1) + n;
if n = 0 then 1
so let see how recursion works, I just replace n in t(n) with n-1 and so on. it looks:
t(n-1) = t(n - 2) + n+1;
t(n-1) = t(n - 3) + n+1 + n;
t(n-1) = t(n - 4) + n+1 + n+2 + n;
.
.
.
t(n) = t(n-k)+ ... + (n-k-3) + (n-k-2)+ (n-k-1)+ n ;
we know if t(0)=(n-k) equals to 1 then n-k=0 so n=k we replace k with n:
t(n) = t(n-n)+ ... + (n-n+3) + (n-n+2)+ (n-n+1)+ n ;
if we omit n-n then:
t(n)= t(0)+ ... + 3+2+1+(n-1)+n;
so 3+2+1+(n-1)+n is natural number. it calculates as Σ3+2+1+(n-1)+n = n(n+1)/2 => n²+n/2
the result for fib is : O(1 + n²) = O(n²)
This the best way to understand recursive relation

How do i find a value in an array in the most efficient way in that case?

NOTE: this algorithm should be done in the most efficient way that's a bad idea to have O(n^2)
This is a two dimensional array, now notice it's hard to see that in first glance but there is a hidden pattern on that array.
this array is actually sorted take a look at that image below you may see the pattern now.
the goal here is to make a function that searches for a value in an array that follows the exact pattern like the array i've showed you in the image and you need to return true if the value was found and also print the column and row where the value sits in the array.
if value wasn't found return false
here is an example
here is the function signature
public static boolean search(int [][]mat,int num){
//Code Here
}
let's say we are looking for the value 22 in that array(the same array in the image)
int [][]arr= {{1,3,7,9},{6,4,15,11},{36,50,21,22},{60,55,30,26}}
search(arr,22); //Returns true and also will be printed "row = 2 col = 3"
search(arr,86); //Returns false
The indices i and j into your matrix can be computed from a single linear index ranging from 0 through 15. This will allow you to do a normal binary search just translating each index from 1D to 2D.
private static int i(int index) {
return iBit(index / 4) * 2 + iBit(index % 4);
}
private static int iBit(int part) {
return (part / 2) ^ (part % 2);
}
private static int j(int index) {
int temp = index / 2;
return temp / 4 * 2 + temp % 2;
}
It’s a bit tricky. So let’s check that the methods work:
for (int index = 0; index < 16; index++) {
System.out.format("%2d -> %d %d%n", index, i(index), j(index));
}
Output:
0 -> 0 0
1 -> 1 0
2 -> 1 1
3 -> 0 1
4 -> 2 0
5 -> 3 0
6 -> 3 1
7 -> 2 1
8 -> 2 2
9 -> 3 2
10 -> 3 3
11 -> 2 3
12 -> 0 2
13 -> 1 2
14 -> 1 3
15 -> 0 3
The numbers agree with the pattern in the question.
NOTE: this algorithm should be done in the most efficient way that's a bad idea to have O(n^2)
It’s really nonsense. Big-O is defined based on the behaviour as the input size grows towards infinity. 16 does not grow towards infinty. Nor does 4. When the matrix size is constant, even a simple linear search will be O(1).

How can I find the stop and start index for a Java vector?

I have a vector that looks like this:
y =
Columns 1 through 19:
1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2
Columns 20 through 38:
2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4
Columns 39 through 57:
4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6
Columns 58 through 67:
6 6 6 6 6 6 6 6 6 6
The vector y is always start at 1 and be counted up. You see that there are lots of same numbers. It's the classes for the samples.
Here we have 1 1 1 1 1 1 1 1 1 1 1 1 = 12 samples for class number 1.
We have 2 2 2 2 2 2 2 2 2 2 2 = 11 samples for class number 2.
My problem here is that I want to find start and stop for every class. For example: Class 1 begins always at index 0 and ends, in this case, at index 11.
Class 2 begins directly after class 1 ends.
Question:
I'm using EJML (Effient Java Matrix Library) and I'm planning to use this function:
C = A.extractMatrix(1,4,2,8)
Which is equal to this MATLAB code:
C = A(2:4,3:8)
But I need to find the start and stop indexes from this y vector. In what index does e.g class 3 stops and starts? Do you have any smart ideas how to do that?
Sure, I could use a for-loop, to do this, but for-loops in Java is quite slow because I'm going to have a very very large y vector.
Suggestions?
Edit:
Here is an suggestion. Is that good, or could it be done better?
private void startStopIndex(SimpleMatrix y, int c, Integer[] startStop) {
int column = y.numCols();
startStop[0] = startStop[1] + 1; // Begin at the next class
for(int i = startStop[0]; i < column; i++) {
if(y.get(i) != c) {
break;
}else {
startStop[1] = i;
}
}
}
Assuming that we are calling the method from:
Integer[] startStop = new Integer[2];
for(int i = 0; i < c; i++) {
startStopIndex(y, c, startStop);
}
If you want to do it faster then binary search is your friend. Threw this together really quick and it does things in O(log n) time, where as a linear search does it in O(n). It's pretty basic and assumes your data looks pretty much like you describe it. Feed it weird data and it will break.:
int[] breakPoints(int[] arr, int low, int high){
int[] rtrn = new int[high];
for(int i=low;i<high;i++){
rtrn[i]=binarySearch(arr, i, 0, arr.length-1);
}
return rtrn;
}
int binarySearch(int[] arr, int k, int start, int end){
int mid = (start+end)/2;
if(mid==arr.length){
return -1;
}
if(arr[mid]==k && arr[mid+1]==k+1){
return mid+1; //or just mid if you want before breakpoint
}
if(arr[mid]<=k){
return binarySearch(arr, k, mid+1, end);
}
return binarySearch(arr, k, start, mid-1);
}
You'd call it like this:
int[] data = {1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,5,5,6,6,6,6};
int[] bp = breakPoints(data,1,6);
//return 0, 3, 8, 13, 16, 18
I think there is a name for this, but I can't remember what it might be, but you start looking for the next boundary with an accelerating search, and use a binary search after that.
You know the numbers are in ascending order, and there are potentially a lot of the same number, so you start by checking the next element. But instead of keep going 1 step at a time, you accelerate and step 2, 4, 8, 16, ... until you find a higher number.
Once you've found a higher number, you've gone too far, but the last step had the initial number, so you know the boundary is somewhere between the last two steps, and you then apply a binary search for the boundary.
Once you've fund the boundary, you start over stepping 1, 2, 4, ... for the next boundary.
If you expect most numbers to have about the same number of occurrences, you could keep a running average count, and make the first step with that average, to get a running start.
I'll leave it to you to actually code this.
The below is in MATLAB. the for loop will go through each unique value stored in x1 and then find the first and last occurrence of that value.
x = [ 1 1 1 2 2 3 3 3 3 3 4 4 4 4 5 5 5 ]
x1 = unique(x)'
for k1 = 1:length(x1)
x1(k1,2:3) = [find(x == x1(k1,1),1,"first"), find(x == x1(k1,1),1,"last")];
end
the above code yields x1 to be a 3 column matrix
1 1 3
2 4 5
3 6 10
4 11 14
5 15 17

Integer powers program Java help needed

So i was getting this program in my book and tested it, it worked fine. I need help though understanding how it excactly works. I know the power of two means for example 2x2x2x2x2x2x2x2x2 = 512. Well, here is the program:
// Compute integer powers of 2.
class Power {
public static void main(String args[]) {
int e;
int result;
for(int i=0; i < 10; i++) {
result = 1;
e = i;
while(e > 0) {
result *= 2;
e--;
}
System.out.println("2 to the " + i + " power is " + result);
}
}
}
So i've learned that result *=2 means: result = result * 2. I don't get how this works. Example, i is now 4. Result = 1 again, e=i, so e is now 4. 4 is higher than 0, so the while loop runs. Then it say's result is result * 2, but that's 1=1 * 2 = 2. But the result should be 16. How does result changes here to 16? Because it is 1 all the time. I don't get it. Also, why the e-- part? I've tried the program with e++, but then it prints result as 1 and after this only 0. Also tried it without e-- or e++ at all, but then it freezes in the dos-prompt. Please note that i am a beginner and this is my first while loop. Any help i would appreciate.
while(e > 0) {
result * = 2;
e--;
}
this loop executes untill the e becomes zero.
so in first loop
result = 1 * 2;
in second loop
result = result * 2; means result = 2 * 2
likewise.
You are using nested loops. That is, a while loop inside a for loop. This means that for every iteration of for loop, while loop will execute until it's condition becomes false.
Now coming to your questions, for first iteration of for, i = 0. Which means that e = 0, which in-turn means that while loop will not execute. So, result is 1 here, which is logical since any n^0 = 1.
For second iteration of for, i = 1. Which means that e = 1. Here, while loop will run for 1 iteration. It will make result = 2 (result *= 2). So, result is 2 because 2^1 = 2.
Below is the dry-run table.
for i while e result
loop loop
1 0 1 0 1
2 1 1 1 2
3 2 1 2 2
1 2 1 4
4 3 1 3 2
2 2 2 4
1 3 1 8
and so on. This table seems ugly, but it will give you a hint.
How does result changes here to 16? Because it is 1 all the time.
Result is only initialized with 1, but it's value is multiplied each time while loop executes.
I've tried the program with e++, but then it prints result as 1 and
after this only 0.
If you put e++ instead of e--, this loop will run until e overflows. But as soon value of e reaches 33, result will become 0 due to multiplication. And after that, it will remain 0.
Also tried it without e-- or e++ at all, but then it freezes in the
dos-prompt.
It means that your condition of while loop will never become false, and hence, it will be an infinite loop.

check number present in a sequences

I am writing a program which I found on a coding competition website, I have sort of figured out how to solve the problem but, I am stuck on a math part of it, I am completely diluting the problem and showing what I need.
first I need to check if a number is part of a sequence, my sequence is 2*a+1 where a is the previous element in the sequence or 2^n-1 to get nth item in the sequence. so it is 1,3,7,15,31,63...
I don't really want to create the whole sequence and check if a number is present, but I am not sure what a quicker method to do this would be.
Second if I am given a number lets say 25, I want to figure out the next highest number in my sequence to this number. So for 25 it would be 31 and for 47 it would be 63, for 8 it would be 13.
How can i do these things without creating the whole sequence.
I have seen similar questions here with different sequences but I am still not sure how to solve this
Start by finding the explicit formula for any term in your sequence. I'm too lazy to write out a proof, so just add 1 to each term in your sequence:
1 + 1 = 2
3 + 1 = 4
7 + 1 = 8
15 + 1 = 16
31 + 1 = 32
63 + 1 = 64
...
You can clearly see that a_n = 2^n - 1.
To check if a particular number is in your sequence, assume that it is:
x = 2^n - 1
x + 1 = 2^n
From Wikipedia:
The binary representation of integers makes it possible to apply a
very fast test to determine whether a given positive integer x is a
power of two:
positive x is a power of two ⇔ (x & (x − 1)) equals to zero.
So to check, just do:
bool in_sequence(int n) {
return ((n + 1) & n) == 0;
}
As #Blender already pointed out your sequence is essentially 2^n - 1, you can use this trick if you use integer format to store it:
boolean inSequence(int value) {
for (int i = 0x7FFF; i != 0; i >>>= 1) {
if (value == i) {
return true;
}
}
return false;
}
Note that for every elements in your sequence, its binary representation will be lots of 0s and then lots of 1s.
For example, 7 in binary is 0000000000000000000000000000111 and 63 in binary is 0000000000000000000000000111111.
This solution starts from 01111111111111111111111111111111 and use an unsigned bitshift, then compare if it is equal to your value.
Nice and simple.
How to find the next higher number :
For example, we get 19 ( 10011 ) , should return 31 (11111)
int findNext(int n){
if(n == 0) return 1;
int ret = 2; // start from 10
while( (n>>1) > 0){ // end with 100000
ret<<1;
}
return ret-1;
}

Categories