Campbell Ritchie wrote:What n being defined in terms of n - 1 means is:
Somwehere you have to have a recursive call to numbers(n - 1).
numbers(n-1)
Ulf Dittmer wrote:
numbers(n-1)
This implies that whatever is done for "n-1" is exactly the same as what's being done for "n", but that's not the case. For starters, the output for "n-1" is contained twice in the output for "n", not just once.
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Sam Mercs wrote:Having a look at your pattern, I see it like this.
n = 1
Output : 1 (Trivial Case)
n = 2
Output : <Output of case n-1> 2 <Output of case n-1>
which turns out to be 1 2 1
n = 3
Output : <Output of case n-1> 3 <Output of case n-1>
which turns out to be <Output of case 2> 3 <Output of case 2>
which is 1 2 1 3 1 2 1
This is the recursion pattern. Of course it's a pretty tough pattern - recursion has never been an easy thing to do.
I understand the pattern as you mentioned. But I cannot think of how to code this in recursive pattern. This is my first try at recursion and first programming class in Java.
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Sam Mercs wrote:
I understand the pattern as you mentioned. But I cannot think of how to code this in recursive pattern. This is my first try at recursion and first programming class in Java.
Have you ever coded anything recursively at all?
If not you should give something a little easier a try first. Maybe these problems might help a bit...
Factorial using recursion -> http://www.javabat.com/prob/p154669
Fibonacci using recursion -> http://www.javabat.com/prob/p120015
Once you get the hang of recursion its pretty easy to write.
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Don't get me started about those stupid light bulbs. |