Anonymous user
Greatest common divisor: Difference between revisions
added ruby
(+ Pascal) |
(added ruby) |
||
Line 3:
=={{header|Ada}}==
Output:
Line 70:
=={{header|C}}==
===Iterative Euclid algorithm===
===Recursive Euclid algorithm===
}</c>
===Iterative binary algorithm===
<
gcd_bin(int u, int v) {
int t, k;
Line 125 ⟶ 124:
}
return u * k;
}</c>
▲</pre>
===Notes on performance===
Line 228 ⟶ 226:
=={{header|Java}}==
===Iterative===
for(long loop= factor;loop > 1;loop--){
return
}</java>
return 1;▼
===Recursive===
}</java>
=={{header|Joy}}==
Line 310 ⟶ 307:
=={{header|OCaml}}==
=={{header|Pascal}}==
<pascal>function gcd(a, b: integer): integer;▼
▲function gcd(a, b: integer): integer;
var
tmp: integer;
Line 329 ⟶ 325:
end;
gcd := a
end;</pascal>
=={{header|Perl}}==
===Iterative Euclid algorithm===
my ($u, $v) = @_;
($u, $
$u = $v;▼
}</perl>
▲ }
===Recursive Euclid algorithm===
my ($u, $v) = @_;
}</perl>
===Iterative binary algorithm===
my ($u, $v) = @_;
($u, $
if
my $k =
▲ }
$
}
my $t = ($
while
}
▲ while ($t & 1 == 0) {
if
}
}</perl>
▲ }
===Notes on performance===
Output on 'Intel(R) Pentium(R) 4 CPU 1.50GHz' / Linux / Perl 5.8.8:
Line 444 ⟶ 435:
=={{header|Python}}==
===Iterative Euclid algorithm===
===Recursive Euclid algorithm===
'''Interpreter:''' [[Python]] 2.5
===Tests===
Line 468 ⟶ 459:
===Iterative binary algorithm===
See [[The Art of Computer Programming]] by Knuth (Vol.2)
===Notes on performance===
Line 498 ⟶ 489:
<code>gcd_bin(40902, 24140)</code> takes us about '''41''' usec
=={{header|Ruby}}==
That is already available as the ''gcd'' method of integers:
irb(main):001:0> require 'rational'
=> true
irb(main):002:0> 40902.gcd(24140)
=> 34
Here's an implementation:
<ruby>def gcd(u, v)
u, v = u.abs, v.abs
while v > 0
end
v
end</ruby>
=={{header|SETL}}==
Line 521 ⟶ 529:
the gcd of 49865 and 69811 is 9973
=={{header|Scheme}}==
or using the standard function included with Scheme (takes any number of arguments):
=={{header|TI-83 BASIC}}==
|