Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
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
Old Mar 9th, 2008, 9:04 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 10th, 2008, 6:54 AM   #3
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5 emdiesse is on a distinguished road
Re: Recursion to compute sequences.

Quote:
Originally Posted by Ooble View Post
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)
Ahah. That makes alot more sense to me now. I don't know why i had an F parametre in the last one, lol. If i didn't want an external loop is there a way to place it inside or is that bad programming?

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);
   }  
}
emdiesse is offline   Reply With Quote
Old Mar 10th, 2008, 1:25 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 10th, 2008, 5:18 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Recursion to compute sequences.

try this:
python Syntax (Toggle Plain Text)
  1. def myseq(k, run):
  2. if k >= 500:
  3. return 0
  4. print k
  5. if run == 1:
  6. return myseq((2 * k) + 1, 0)
  7. else:
  8. return myseq((2 * k) - 1, 1)
  9.  
  10. def main():
  11. myseq(2, 0)
  12.  
  13. main()
OpenLoop is offline   Reply With Quote
Reply

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 5:52 PM.

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