This post has been edited by TechSyndrome: 20 May 2012 - 01:17 PM
28 Replies - 2014 Views - Last Post: 21 May 2012 - 06:37 AM
#16
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:17 PM
#17
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:17 PM
P6 = (P6 - 3 * P6-1) + 1
Now we 6-3 and 6-1 are easily calculated, so we get:
P6 = (P3 * P5) + 1
Now we only need to know the values of P3 and P5. If we already calculated those, we can just insert them and we're done. If we haven't calculated them yet, we need to apply the rule again, this time replacing n with 5 (and then after that with 3), instead of 6.
#18
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:28 PM
sepp2k, on 20 May 2012 - 01:17 PM, said:
P6 = (P6 - 3 * P6-1) + 1
Now we 6-3 and 6-1 are easily calculated, so we get:
P6 = (P3 * P5) + 1
Now we only need to know the values of P3 and P5. If we already calculated those, we can just insert them and we're done. If we haven't calculated them yet, we need to apply the rule again, this time replacing n with 5 (and then after that with 3), instead of 6.
I'm writing this out with a pen right now to make sure I understand it fully.
#19
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:34 PM
TechSyndrome, on 20 May 2012 - 01:28 PM, said:
sepp2k, on 20 May 2012 - 01:17 PM, said:
P6 = (P6 - 3 * P6-1) + 1
Now we 6-3 and 6-1 are easily calculated, so we get:
P6 = (P3 * P5) + 1
Now we only need to know the values of P3 and P5. If we already calculated those, we can just insert them and we're done. If we haven't calculated them yet, we need to apply the rule again, this time replacing n with 5 (and then after that with 3), instead of 6.
I'm writing this out with a pen right now to make sure I understand it fully.
I'm sorry if I'm going around in circles, but how does P3 in "P6 = (P3 * P5) + 1" represent the value 2? We know that P0, P1, P2 is equal to 1, but P3 hasn't actually been given a value yet?
This post has been edited by TechSyndrome: 20 May 2012 - 01:43 PM
#20
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:39 PM
#21
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:42 PM
#22
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:44 PM
#23
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:45 PM
TechSyndrome, on 20 May 2012 - 10:34 PM, said:
We calculate P3 the same way we do P6, i.e. by taking the rule "Pn = (Pn - 3 * Pn-1) + 1" and replacing n with 3, so we get:
P3 = (P3 - 3 * P3-1) + 1
, which is:
P3 = (P0 * P2) + 1
Since we already know P0 and P2 this becomes:
P3 = (1 * 1) + 1
, which is 2.
#24
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:46 PM
#25
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:51 PM
So basically I worked backwards as #23 sepp2k explained. I was looking for P6, so I replaced all occurrences of n in the fourth rule with 6. The key was to do the calculation AFTERWARDS, but to replace all occurrences of n in the fourth rule FIRST.
#26
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:55 PM
TechSyndrome, on 20 May 2012 - 10:44 PM, said:
Oh, I am. To make this more clear, let's start with P6 and go all the way down to the bottom:
P6 = (P6 - 3 * P6-1) + 1
Now 6-3 and 6-1 are easily calculated, so we get:
P6 = (P3 * P5) + 1
Now we only need to know the values of P3 and P5. So let's start with P5:
P5 = (P5 - 3 * P5-1) + 1
= (P2 * P4) + 1
We already have P2 because that's one of the base cases. We still need P4, so let's calculate that:
P4 = (P4 - 3 * P4-1) + 1
= (P1 * P3) + 1
So now P3:
P3 = (P0 * P2) + 1
We know P0 and P2, so we get:
P3 = 2
So now we can go up again to P4:
P4 = (P1 * P3) + 1
= (1*2)+1 = 3
And now P5:
P5 = (P2 * P4) + 1
= (1*3)+1 = 4
And now P6:
P6 = (P3 * P5) + 1
= (2*4)+1 = 9
What we did here basically is to start at the top, then go all the way down to the bottom and then go up again. As I said earlier, this has no advantage over simply starting at the bottom and then going up in this case.
However in cases where we don't need to calculate all the preceding elements to calculate the next one, this approach can save needless calculations.
In fact there are even sequences where an element can depend on other elements that come after it. In those cases the bottom-up approach doesn't work and you actually need to use this approach.
#27
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 01:57 PM
#28
Re: How do you define a sequence of numbers recursively?
Posted 20 May 2012 - 02:02 PM
sepp2k, on 20 May 2012 - 01:55 PM, said:
TechSyndrome, on 20 May 2012 - 10:44 PM, said:
Oh, I am. To make this more clear, let's start with P6 and go all the way down to the bottom:
P6 = (P6 - 3 * P6-1) + 1
Now 6-3 and 6-1 are easily calculated, so we get:
P6 = (P3 * P5) + 1
Now we only need to know the values of P3 and P5. So let's start with P5:
P5 = (P5 - 3 * P5-1) + 1
= (P2 * P4) + 1
We already have P2 because that's one of the base cases. We still need P4, so let's calculate that:
P4 = (P4 - 3 * P4-1) + 1
= (P1 * P3) + 1
So now P3:
P3 = (P0 * P2) + 1
We know P0 and P2, so we get:
P3 = 2
So now we can go up again to P4:
P4 = (P1 * P3) + 1
= (1*2)+1 = 3
And now P5:
P5 = (P2 * P4) + 1
= (1*3)+1 = 4
And now P6:
P6 = (P3 * P5) + 1
= (2*4)+1 = 9
What we did here basically is to start at the top, then go all the way down to the bottom and then go up again. As I said earlier, this has no advantage over simply starting at the bottom and then going up in this case.
However in cases where we don't need to calculate all the preceding elements to calculate the next one, this approach can save needless calculations.
In fact there are even sequences where an element can depend on other elements that come after it. In those cases the bottom-up approach doesn't work and you actually need to use this approach.
I just used your bottom up approach in a different question and it worked!!! THANK YOU SO MUCH! I'm gonna read what you've just written right now...I'm so afraid I'm gonna forget this stuff. Amazing that just 1 hour ago I was depressed at not understanding any of this!
macosxnerd101, on 20 May 2012 - 01:57 PM, said:
The second part of this question is related to Java. This is all part of Data Structures and Algorithms in Java. Does that count?
#29
Re: How do you define a sequence of numbers recursively?
Posted 21 May 2012 - 06:37 AM
|
|

New Topic/Question
Reply




MultiQuote





|