Coprimes: Difference between revisions

2,878 bytes added ,  1 month ago
m
m (syntax highlighting fixup automation)
 
(8 intermediate revisions by 7 users not shown)
Line 572:
i := i + 1;
end loop;</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
function GreatestCommonDivisor(A,B: integer): integer;
begin
if B = 0 then Result:=A
else Result:=GreatestCommonDivisor( B, A mod B);
end;
 
 
function IsCoprime(A,B: integer): boolean;
begin
Result:=GreatestCommonDivisor(A,B)=1;
end;
 
 
const TestNumbers: array [0..4] of TPoint =
((X:21;Y:15),(X:17;Y:23),(X:36;Y:12),(X:18;Y:29),(X:60;Y:15));
 
 
procedure ShowCoprimes(Memo: TMemo);
var I: integer;
var S: string;
var TN: TPoint;
begin
for I:=0 to High(TestNumbers) do
begin
TN:=TestNumbers[I];
S:=IntToStr(TN.X)+' '+IntToStr(TN.Y)+' is ';
if IsCoprime(TN.X,TN.Y) then S:=S+'coprime'
else S:=S+'not coprime';
Memo.Lines.Add(S);
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
21 15 is not coprime
17 23 is coprime
36 12 is not coprime
18 29 is coprime
60 15 is not coprime
Elapsed Time: 5.729 ms.
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
func gcd a b .
while b <> 0
h = b
b = a mod b
a = h
.
return a
.
proc test p[] . .
if gcd p[1] p[2] = 1
print p[]
.
.
pairs[][] = [ [ 21 15 ] [ 17 23 ] [ 36 12 ] [ 18 29 ] [ 60 15 ] ]
for i to len pairs[][]
test pairs[i][]
.
</syntaxhighlight>
{{out}}
<pre>
[ 17 23 ]
[ 18 29 ]
</pre>
 
=={{header|F_Sharp|F#}}==
Line 584 ⟶ 662:
18 and 29 are coprime
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
Line 807 ⟶ 886:
{{out}}
<pre>{False, True, False, True, False}</pre>
 
=={{header|MATLAB}}==
<syntaxhighlight lang="matlab">disp(coprime([21, 17, 36, 18, 60], [15, 23, 12, 29, 15]));
 
function coprimes = coprime(a,b)
gcds = gcd(a,b) == 1;
coprimes(1,:) = a(gcds);
coprimes(2,:) = b(gcds);
end</syntaxhighlight>
 
{{out}}
<pre> 17 18
23 29</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Taken the list of pairs and making a sublist of those pairs that are coprimes */
pairs:[[21,15],[17,23],[36,12],[18,29],[60,15]]$
sublist(pairs,lambda([x],apply('gcd,x)=1));
</syntaxhighlight>
{{out}}
<pre>
[[17,23],[18,29]]
</pre>
 
=={{header|Nim}}==
Line 820 ⟶ 923:
18 and 29 are coprimes.
60 and 15 are not coprimes.</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec is_coprime a = function
| 0 -> a = 1
| b -> is_coprime b (a mod b)
 
let () =
let p (a, b) =
Printf.printf "%u and %u are%s coprime\n" a b (if is_coprime a b then "" else " not")
in
List.iter p [21, 15; 17, 23; 36, 12; 18, 29; 60, 15]</syntaxhighlight>
{{out}}
<pre>
21 and 15 are not coprime
17 and 23 are coprime
36 and 12 are not coprime
18 and 29 are coprime
60 and 15 are not coprime
</pre>
 
=={{header|Perl}}==
Line 1,135 ⟶ 1,257:
Found 2 coprimes
done...
</pre>
 
=={{header|RPL}}==
<code>GCD</code> is defined at [[Greatest common divisor#RPL|Greatest common divisor]]
{{works with|HP|28}}
≪ → pairs
≪ { }
1 pairs SIZE '''FOR''' j
pairs j GET
DUP LIST→ DROP
'''IF''' <span style="color:blue">GCD</span> 1 == '''THEN''' 1 →LIST + '''ELSE''' DROP '''END'''
'''NEXT'''
≫ '<span style="color:blue">→COPR</span>' STO
 
{{21 15} {17 23} {36 12} {18 29} {60 15}} <span style="color:blue">→COPR</span>
{{out}}
<pre>
1: { { 17 23 } { 18 29 } }
</pre>
 
Line 1,160 ⟶ 1,300:
{{libheader|Wren-math}}
Two numbers are coprime if their GCD is 1.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var pairs = [[21,15],[17,23],[36,12],[18,29],[60,15]]
1,982

edits