![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 4
![]() |
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 {
private static int printSequence(int F, int k) {
if(k ==1) return 2;
if(k ==2) return 3;
F = printSequence(F, k-1) + (2*printSequence(F, k-2));
System.out.print(F + ", ");
return F;
}
public static void main(String[] args) {
int k = 8;
int F = 3;
printSequence(F, k);
}
}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? |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
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):
if k == 1:
return 2
elif k == 2:
return 3
else:
return sequence(k - 1) + 2 * sequence(k - 2)
def main():
for i in xrange(1, 10):
print sequence(i) |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 4
![]() |
Re: Recursion to compute sequences.
Quote:
public class TDSeq {
private static int F = 0;
private static void printSequence(int i) {
for(int k=1;k<=i;k++){ //can i place this in calcSequence?
System.out.print(calcSequence(k) + ",");
}
}
private static int calcSequence(int k) {
if(k ==1) return 2;
else if(k ==2){ return 3;}
else return ( calcSequence(k-1) + (2*calcSequence(k-2)) );
}
public static void main(String[] args) {
int i = 7;
printSequence(i);
}
} |
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
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.
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Re: Recursion to compute sequences.
try this:
python Syntax (Toggle Plain Text)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Understanding Recursion | 357mag | C++ | 18 | Aug 7th, 2007 9:39 AM |
| How to compute grades using Coldfusion? | anyer_ast!g | Other Web Development Languages | 5 | Oct 18th, 2006 6:59 AM |
| Recursion and Scheme | Jessehk | Coder's Corner Lounge | 8 | Feb 4th, 2006 7:46 AM |
| Debug recursion method() | pr0gm3r | Java | 3 | Oct 11th, 2005 12:33 PM |
| Recursion | Mjordan2nd | Coder's Corner Lounge | 22 | Jul 7th, 2005 11:26 AM |