Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Mar 9th, 2008, 6:17 PM   #1
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
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?
emdiesse is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC