Recursion

Please, I need an example of recursive function with 2 or more parameters, i.e. the euclidean formula for maximum common divisor.
thank you very much,
Mattia

You mean, like this? :

Compute the Greatest Common Divisor.

def gcd(a,b)
return a if b == 0
return gcd(b, a % b)
end

···

On Thu, Mar 13, 2003 at 09:57:25AM +0900, Mattia Peronio wrote:

Please, I need an example of recursive function with 2 or more
parameters, i.e. the euclidean formula for maximum common divisor.
thank you very much,
Mattia


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

“Daniel Carrera” dcarrera@math.umd.edu wrote in message
news:20030313010257.GA3381@math.umd.edu…

You mean, like this? :

Compute the Greatest Common Divisor.

def gcd(a,b)
return a if b == 0
return gcd(b, a % b)
end

Please, I need an example of recursive function with 2 or more
parameters, i.e. the euclidean formula for maximum common divisor.
thank you very much,
Mattia

May I suggest that we do not directly solve what appears to be home
assignments?

Mikkel

···

On Thu, Mar 13, 2003 at 09:57:25AM +0900, Mattia Peronio wrote:

MikkelFJ wrote:

“Daniel Carrera” dcarrera@math.umd.edu wrote in message
news:20030313010257.GA3381@math.umd.edu…

You mean, like this? :

Compute the Greatest Common Divisor.

def gcd(a,b)
return a if b == 0
return gcd(b, a % b)
end

Please, I need an example of recursive function with 2 or more
parameters, i.e. the euclidean formula for maximum common divisor.
thank you very much,
Mattia

May I suggest that we do not directly solve what appears to be home
assignments?

Indeed. And as a punishment for this offense, the offending party should
write out the decimal represenatation of

A(5, 5)

where

A(0, n) = n + 1
A(m+1, 0) = A(m, 1)
A(m+1, n+1) = A(m, A(m+1, n))

(Ackermann’s function)

Oops. Now I guess I gotta do it, too :wink:

···

On Thu, Mar 13, 2003 at 09:57:25AM +0900, Mattia Peronio wrote:

Forget about that!
Being a novice, I was thinking I not to be able to correctly implement recursion.
I discovered my problem was not this.
At that time the ruby interpreter behaved in a strange mood.
When passed a parameter like this to the function

def hallo(a)
print a
end

hallo( x**y*x)

it gave me an error → undefined local variable or method `*’
so I had to add a line to have my function working

def hallo(a)
print a
end

b= x**y*x
hallo( b)

and all worked correctly. Notice that this problem occurred only at that time.

Thanks for your patience.

···

On Fri, 14 Mar 2003 00:44:38 +0900, Joel VanderWerf wrote:

MikkelFJ wrote:

“Daniel Carrera” dcarrera@math.umd.edu wrote in message
news:20030313010257.GA3381@math.umd.edu…

You mean, like this? :

Compute the Greatest Common Divisor.

def gcd(a,b)
return a if b == 0
return gcd(b, a % b)
end

On Thu, Mar 13, 2003 at 09:57:25AM +0900, Mattia Peronio wrote:

Please, I need an example of recursive function with 2 or more
parameters, i.e. the euclidean formula for maximum common divisor.
thank you very much,
Mattia

May I suggest that we do not directly solve what appears to be home
assignments?

Indeed. And as a punishment for this offense, the offending party
should
write out the decimal represenatation of

A(5, 5)

where

A(0, n) = n + 1
A(m+1, 0) = A(m, 1)
A(m+1, n+1) = A(m, A(m+1, n))

(Ackermann’s function)

Oops. Now I guess I gotta do it, too :wink: