![]() |
Recursion to compute sequences.
Hello. I am attempting to use recursion to output the sequence of numbers 2, 3, 7, 13, 27, 53, ...
:
public class TDSeq {Now this give a horrible output: 7, 13, 7, 27, 7, 13, 53, 7, 13, 7, 27, 107, 7, 13, 7, 27, 7, 13, 53, 213, No where near the aim of: 2, 3, 7, 13, 27, 53, ... Now I really can't get my head around this, however I would really like to learn how to use recursion effectively. Any one got any help or advice they can offer? |
Re: Recursion to compute sequences.
You have two main problems. One is that you print the numbers right after you generate them, not when you've finished the recursion, and the other is that you never print 2 or 3.
Here's some pseudocode (actually Python code, but they're essentially the same thing) that'll do it, though it's not efficient. You'd need to work on some kind of caching for that. :
def sequence(k): |
Re: Recursion to compute sequences.
Quote:
:
public class TDSeq { |
Re: Recursion to compute sequences.
Thinking about it, it doesn't make much sense. The numbers aren't parsed through in order, so they wouldn't be printed in order. I can't think of a way to do it right now... hopefully it'll come to me soon.
|
Re: Recursion to compute sequences.
try this:
:
|
| All times are GMT -5. The time now is 4:20 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC