PDA

View Full Version : [SOLVED] Recursion in Python



blazemore
November 2nd, 2009, 01:13 PM
I need some help implementing the following as a Python (Or anything really) function.


myLuc(0) = 0
myLuc(1) = 1
myLuc(n) = 3 myLuc(n − 1) − myLuc(n − 2) if n > 1


Can anyone do this, in python or a c-like pseudocode?

Paul Miller
November 2nd, 2009, 01:18 PM
This sounds distressingly like a homework assignment, but I'm going to assume it's not.

In Python:



def myLuc (n):
if n == 0 or n == 1: return n
else: return 3 * myLuc (n-1) - myLuc (n-2)


will do the right thing assuming n is a positive integer. However, it will be very slow.

blazemore
November 2nd, 2009, 01:20 PM
Actually I'm just teaching myself Python.

fiddler616
November 2nd, 2009, 08:00 PM
Actually I'm just teaching myself Python.
Why don't you get a tutorial?

fiddler616
November 2nd, 2009, 08:01 PM
c-like pseudocode?

Actually I'm just teaching myself Python.
??

blazemore
November 2nd, 2009, 08:58 PM
I just needed the logic. I know the Python syntax.

Arndt
November 3rd, 2009, 11:10 AM
I just needed the logic. I know the Python syntax.

My first thought was that what you wrote in the first post already was perfectly clear pseudo-code.

blazemore
November 3rd, 2009, 01:10 PM
It was that "if n > 1" at the end. I understand it now.