![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
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? |
|
|
|
| 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 |