Greatest common divisor: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 128:
return a;
}</lang>
 
=={{header|Ada}}==
<lang ada>with Ada.Text_Io; use Ada.Text_Io;
Line 350 ⟶ 351:
End
GCD(B,A^B)</lang>
 
=={{header|Batch File}}==
Recursive method
<lang dos>:: gcd.cmd
@echo off
:gcd
if "%2" equ "" goto :instructions
if "%1" equ "" goto :instructions
 
if %2 equ 0 (
set final=%1
goto :done
)
set /a res = %1 %% %2
call :gcd %2 %res%
goto :eof
 
:done
echo gcd=%final%
goto :eof
 
:instructions
echo Syntax:
echo GCD {a} {b}
echo.</lang>
 
=={{header|BASIC}}==
Line 422 ⟶ 398:
{{out}}
<pre>17</pre>
 
=={{header|Batch File}}==
Recursive method
<lang dos>:: gcd.cmd
@echo off
:gcd
if "%2" equ "" goto :instructions
if "%1" equ "" goto :instructions
 
if %2 equ 0 (
set final=%1
goto :done
)
set /a res = %1 %% %2
call :gcd %2 %res%
goto :eof
 
:done
echo gcd=%final%
goto :eof
 
:instructions
echo Syntax:
echo GCD {a} {b}
echo.</lang>
 
=={{header|BBC BASIC}}==
Line 574 ⟶ 575:
return u * k;
}</lang>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <numeric>
 
int main() {
std::cout << "The greatest common divisor of 12 and 18 is " << std::gcd(12, 18) << " !\n";
}</lang>
 
{{libheader|Boost}}
<lang cpp>#include <boost/math/common_factor.hpp>
#include <iostream>
 
int main() {
std::cout << "The greatest common divisor of 12 and 18 is " << boost::math::gcd(12, 18) << "!\n";
}</lang>
 
{{out}}
<pre>The greatest common divisor of 12 and 18 is 6!</pre>
 
=={{header|c sharp|C#}}==
Line 689 ⟶ 671:
GCD of 36 and 35 is 1
</pre>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <numeric>
 
int main() {
std::cout << "The greatest common divisor of 12 and 18 is " << std::gcd(12, 18) << " !\n";
}</lang>
 
{{libheader|Boost}}
<lang cpp>#include <boost/math/common_factor.hpp>
#include <iostream>
 
int main() {
std::cout << "The greatest common divisor of 12 and 18 is " << boost::math::gcd(12, 18) << "!\n";
}</lang>
 
{{out}}
<pre>The greatest common divisor of 12 and 18 is 6!</pre>
 
=={{header|Clojure}}==
Line 863 ⟶ 864:
gcd(24 ,-112 )= -8
</pre>
 
=={{header|D}}==
<lang d>import std.stdio, std.numeric;
Line 1,151 ⟶ 1,153:
end
</lang>
 
=={{header|Elena}}==
{{trans|C#}}
Line 1,240 ⟶ 1,243:
<pre>4
</pre>
 
 
=={{header|ERRE}}==
Line 1,431 ⟶ 1,433:
 
பதிப்பி "நீங்கள் தந்த இரு எண்களின் மீபொவ (மீப்பெரு பொது வகுத்தி, GCD) = ", மீபொவ(அ, ஆ)
</lang>
 
=={{header|Free Pascal}}==
See [[#Pascal / Delphi / Free Pascal]].
 
=={{header|Frege}}==
<lang fsharp>module gcd.GCD where
 
pure native parseInt java.lang.Integer.parseInt :: String -> Int
 
gcd' a 0 = a
gcd' a b = gcd' b (a `mod` b)
 
main args = do
(a:b:_) = args
println $ gcd' (parseInt a) (parseInt b)
</lang>
 
Line 1,589 ⟶ 1,575:
 
<tt>gcd_bin(40902, 24140)</tt> takes us about '''2.5''' µsec
 
=={{header|Free Pascal}}==
See [[#Pascal / Delphi / Free Pascal]].
 
=={{header|FreeBASIC}}==
Line 1,624 ⟶ 1,613:
<pre>GCD(111111111111111, 11111) = 11111
GCD(111111111111111, 111) = 111</pre>
 
=={{header|Frege}}==
<lang fsharp>module gcd.GCD where
 
pure native parseInt java.lang.Integer.parseInt :: String -> Int
 
gcd' a 0 = a
gcd' a b = gcd' b (a `mod` b)
 
main args = do
(a:b:_) = args
println $ gcd' (parseInt a) (parseInt b)
</lang>
 
=={{header|Frink}}==
Line 2,071 ⟶ 2,073:
end;
[a,b] | rgcd ;</lang>
 
=={{header|Julia}}==
Julia includes a built-in <code>gcd</code> function:
Line 2,130 ⟶ 2,133:
17
</pre>
 
 
=={{header|Liberty BASIC}}==
Line 2,166 ⟶ 2,168:
return x
end gcd</lang>
 
 
=={{header|Logo}}==
Line 2,224 ⟶ 2,225:
else gcd(a, b % a)
)</lang>
 
 
=={{header|M2000 Interpreter}}==
Line 2,258:
3
</lang>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 2,681 ⟶ 2,680:
Success: Results of iterative and recursive methods match
</pre>
 
=={{header|NewLISP}}==
<lang NewLISP>(gcd 12 36)
Line 2,795:
GCD of 40902 and 24140 : 34
</pre>
 
=={{header|Objeck}}==
<lang objeck>
Line 3,087 ⟶ 3,088:
gcd_mpu 7114798/s 17714% 2930% 1057% 797% 275% --
</pre>
 
=={{header|Perl 6}}==
 
===Iterative===
<lang perl6>sub gcd (Int $a is copy, Int $b is copy) {
$a & $b == 0 and fail;
($a, $b) = ($b, $a % $b) while $b;
return abs $a;
}</lang>
 
===Recursive===
<lang perl6>multi gcd (0, 0) { fail }
multi gcd (Int $a, 0) { abs $a }
multi gcd (Int $a, Int $b) { gcd $b, $a % $b }</lang>
 
===Concise===
<lang perl6>my &gcd = { ($^a.abs, $^b.abs, * % * ... 0)[*-2] }</lang>
 
===Actually, it's a built-in infix===
<lang perl6>my $gcd = $a gcd $b;</lang>
Because it's an infix, you can use it with various meta-operators:
<lang perl6>[gcd] @list; # reduce with gcd
@alist Zgcd @blist; # lazy zip with gcd
@alist Xgcd @blist; # lazy cross with gcd
@alist »gcd« @blist; # parallel gcd</lang>
 
=={{header|Phix}}==
Line 3,157 ⟶ 3,133:
gcd({57,0,-45,-18,90,447}) -- 3
</pre>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(de gcd (A B)
(until (=0 B)
(let M (% A B)
(setq A B B M) ) )
(abs A) )</lang>
 
=={{header|PHP}}==
Line 3,193 ⟶ 3,162:
}
</lang>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(de gcd (A B)
(until (=0 B)
(let M (% A B)
(setq A B B M) ) )
(abs A) )</lang>
 
=={{header|PL/I}}==
Line 3,241 ⟶ 3,217:
} def
</lang>
 
=={{header|PowerShell}}==
===Recursive Euclid Algorithm===
Line 3,291 ⟶ 3,268:
gcd(X, Y, D):- X =< Y, !, Z is Y - X, gcd(X, Z, D).
gcd(X, Y, D):- gcd(Y, X, D).</lang>
 
 
=={{header|PureBasic}}==
Line 3,459 ⟶ 3,435:
(check-equal? (gcd 0 14) 14)
(check-equal? (gcd 13 0) 13))</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
===Iterative===
<lang perl6>sub gcd (Int $a is copy, Int $b is copy) {
$a & $b == 0 and fail;
($a, $b) = ($b, $a % $b) while $b;
return abs $a;
}</lang>
 
===Recursive===
<lang perl6>multi gcd (0, 0) { fail }
multi gcd (Int $a, 0) { abs $a }
multi gcd (Int $a, Int $b) { gcd $b, $a % $b }</lang>
 
===Concise===
<lang perl6>my &gcd = { ($^a.abs, $^b.abs, * % * ... 0)[*-2] }</lang>
 
===Actually, it's a built-in infix===
<lang perl6>my $gcd = $a gcd $b;</lang>
Because it's an infix, you can use it with various meta-operators:
<lang perl6>[gcd] @list; # reduce with gcd
@alist Zgcd @blist; # lazy zip with gcd
@alist Xgcd @blist; # lazy cross with gcd
@alist »gcd« @blist; # parallel gcd</lang>
 
=={{header|Rascal}}==
Line 3,500 ⟶ 3,502:
24140 40902 gcd</lang>
{{out}}<pre>34</pre>
 
=={{header|REBOL}}==
<lang rebol>gcd: func [
Line 3,680 ⟶ 3,683:
wend
end function </lang>
 
 
=={{header|Rust}}==
Line 3,732 ⟶ 3,734:
3999
13</lang>
 
=={{header|Sass/SCSS}}==
 
Iterative Euclid's Algorithm
 
<lang coffeescript>
@function gcd($a,$b) {
@while $b > 0 {
$c: $a % $b;
$a: $b;
$b: $c;
}
@return $a;
}
</lang>
 
=={{header|Sather}}==
Line 3,799 ⟶ 3,816:
end;
end;</lang>
 
=={{header|Sass/SCSS}}==
 
Iterative Euclid's Algorithm
 
<lang coffeescript>
@function gcd($a,$b) {
@while $b > 0 {
$c: $a % $b;
$a: $b;
$b: $c;
}
@return $a;
}
</lang>
 
=={{header|Scala}}==
Line 4,199 ⟶ 4,201:
b.is_zero ? a.abs : gcd(b, a % b);
}</lang>
 
=={{header|Simula}}==
For a recursive variant, see [[Sum multiples of 3 and 5#Simula|Sum multiples of 3 and 5]].
Line 4,331 ⟶ 4,334:
return n * k / GCD(n, k);
}</lang>
 
=={{header|SQL}}==
Demonstration of Oracle 12c WITH Clause Enhancements
Line 4,629 ⟶ 4,633:
 
</lang>
 
 
=={{header|TXR}}==
Line 4,660 ⟶ 4,663:
return b ? gcd_rec(b, a % b) : Math.abs(a);
}</lang>
 
 
=={{header|uBasic/4tH}}==
Line 4,684 ⟶ 4,686:
 
0 OK, 0:205</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
Line 5,095 ⟶ 5,098:
 
jr gcd</lang>
 
 
=={{header|zkl}}==
10,333

edits