On Tue, 18 Jul 2006 16:09:07 +0200, gamo <(E-Mail Removed)> wrote:

>> >Does anyone knows how to write a sub for gcd?
^^^^^^^^^^^^^

^^^^^^^^^^^^^

>> Yes I know!

>>

>> Ok, now I know you don't believe me...

>>

>> sub gcd { $_[1]?gcd($_[1],$_[0]%$_[1]):$_[0] }

>>

>I belive you, but it is just a gcd of a PAIR of numbers.
First of all with the information you supplied (see above!) it may

have been whatever. I *guessed* you were referring to the Greatest

Common Divisor, and supplied *a* sub for the most obvious case that

occurred to me. BTW, you do know that amongst other things

gcd(n,a,b,...) = gcd(gcd(n,a),b,...) = gcd(gcd(n,a),gcd(n,b),...),

don't you? Well, the following will work for any *positive* number of

arguments.

sub gcd { my $n=pop;@_?gcd($n?map gcd($n,$_%$n),@_:@_):$n }

Please notice the simmetry between the @_?...:$n construct and the

$n?...:@_ one. Isn't it amazing?

>I find this in the ToolBox module

>

>sub gcd {
Well, if you had it, why are you asking in the first place?!? Note:

this will work for *any* number of arguments.

Michele

--

{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr

(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='

..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,

256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,