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?