Duffinian numbers: Difference between revisions

Added Easylang
(Add BCPL)
(Added Easylang)
 
(16 intermediate revisions by 7 users not shown)
Line 120:
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">duffinian_numbers←{
sigma ← +/(⍸0=⍳|⊢)
duff ← sigma((1=∨)∧⊣>1+⊢)⊢
⎕←'First 50 Duffinian numbers:'
⎕←5 10⍴(⊢(/⍨)duff¨)⍳220
⎕←'First 15 Duffinian triplets:'
⎕←(0 1 2∘.+⍨⊢(/⍨)0 1 2(⊃∧.⌽)(⊂duff¨))⍳8500
}</syntaxhighlight>
{{out}}
<pre>First 50 Duffinian numbers:
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
First 15 Duffinian triplets:
63 64 65
323 324 325
511 512 513
721 722 723
899 900 901
1443 1444 1445
2303 2304 2305
2449 2450 2451
3599 3600 3601
3871 3872 3873
5183 5184 5185
5617 5618 5619
6049 6050 6051
6399 6400 6401
8449 8450 8451</pre>
=={{header|AppleScript}}==
As is often the case with these tasks, it takes as much code to format the output as it does to get the numbers. :)
Line 250 ⟶ 282:
12481 12482 12483
13447 13448 13449"</syntaxhighlight>
 
=={{header|Arturo}}==
 
Line 704 ⟶ 737:
</pre>
 
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">word MAXSIGMA = 10000;
[MAXSIGMA+1]word sigma;
 
proc calcsigma() void:
word i, j;
for i from 0 upto MAXSIGMA do sigma[i] := 0 od;
for i from 1 upto MAXSIGMA do
for j from i by i upto MAXSIGMA do
sigma[j] := sigma[j] + i
od
od
corp
 
proc gcd(word a, b) word:
word c;
while b > 0 do
c := a % b;
a := b;
b := c;
od;
a
corp
 
proc duff(word n) bool:
sigma[n] > n+1 and gcd(n, sigma[n]) = 1
corp
 
proc triplet(word n) bool:
duff(n) and duff(n+1) and duff(n+2)
corp
 
proc first(word n; proc(word n)bool pred; proc(word i,n)void cb) void:
word i, cur;
cur := 0;
for i from 1 upto n do
while cur := cur + 1; not pred(cur) do od;
cb(i, cur)
od
corp
 
proc tablenum(word i, n) void:
write(n:5);
if i%10 = 0 then writeln() fi
corp
 
proc tripletline(word i, n) void:
writeln(i:2, ' ', n:6, n+1:6, n+2:6)
corp
 
proc main() void:
calcsigma();
writeln("First 50 Duffinian numbers:");
first(50, duff, tablenum);
writeln();
 
writeln("First 15 Duffinian triplets:");
first(15, triplet, tripletline)
corp</syntaxhighlight>
{{out}}
<pre>First 50 Duffinian numbers:
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
 
First 15 Duffinian triplets:
1 63 64 65
2 323 324 325
3 511 512 513
4 721 722 723
5 899 900 901
6 1443 1444 1445
7 2303 2304 2305
8 2449 2450 2451
9 3599 3600 3601
10 3871 3872 3873
11 5183 5184 5185
12 5617 5618 5619
13 6049 6050 6051
14 6399 6400 6401
15 8449 8450 8451</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func gcd a b .
while b <> 0
h = b
b = a mod b
a = h
.
return a
.
func sumdiv num .
d = 2
repeat
quot = num div d
until d > quot
if num mod d = 0
sum += d
if d <> quot
sum += quot
.
.
d += 1
.
return sum + 1
.
func isduff n .
if isprim n = 0 and gcd sumdiv n n = 1
return 1
.
return 0
.
proc duffs . .
print "First 50 Duffinian numbers:"
n = 4
repeat
if isduff n = 1
write n & " "
cnt += 1
.
until cnt = 50
n += 1
.
cnt = 0
n = 4
print "\n\nFirst 15 Duffinian triplets:"
repeat
if isduff n = 1 and isduff (n + 1) = 1 and isduff (n + 2) = 1
print n & " - " & n + 2
cnt += 1
.
until cnt = 15
n += 1
.
.
duffs
</syntaxhighlight>
 
=={{header|Factor}}==
Line 790 ⟶ 974:
BOOL result = NO
if ( fn IsPrime(n) == NO and&& fn GCD( fn SumDiv(n), n ) == 1 ) then exit fn = YES
end fn = result
 
Line 1,261 ⟶ 1,445:
8449 8450 8451
</pre>
 
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
DIMENSION SIGMA(10000),OUTROW(10)
 
INTERNAL FUNCTION(AA,BB)
ENTRY TO GCD.
A = AA
B = BB
STEP WHENEVER A.E.B, FUNCTION RETURN A
WHENEVER A.G.B, A = A-B
WHENEVER A.L.B, B = B-A
TRANSFER TO STEP
END OF FUNCTION
 
INTERNAL FUNCTION(N)
ENTRY TO DUFF.
SIG = SIGMA(N)
FUNCTION RETURN SIG.G.N+1 .AND. GCD.(N,SIG).E.1
END OF FUNCTION
 
INTERNAL FUNCTION(N)
ENTRY TO TRIP.
FUNCTION RETURN DUFF.(N) .AND.
0 DUFF.(N+1) .AND. DUFF.(N+2)
END OF FUNCTION
 
THROUGH SZERO, FOR I=1, 1, I.G.10000
SZERO SIGMA(I) = 0
THROUGH SCALC, FOR I=1, 1, I.G.10000
THROUGH SCALC, FOR J=I, I, J.G.10000
SCALC SIGMA(J) = SIGMA(J) + I
 
PRINT COMMENT $ FIRST 50 DUFFINIAN NUMBERS$
CAND = 0
THROUGH DUFROW, FOR R=0, 1, R.GE.5
THROUGH DUFCOL, FOR C=0, 1, C.GE.10
SCHDUF THROUGH SCHDUF, FOR CAND=CAND+1, 1, DUFF.(CAND)
DUFCOL OUTROW(C) = CAND
DUFROW PRINT FORMAT ROWFMT,OUTROW(0),OUTROW(1),OUTROW(2),
0 OUTROW(3),OUTROW(4),OUTROW(5),OUTROW(6),
1 OUTROW(7),OUTROW(8),OUTROW(9)
 
PRINT COMMENT $ $
PRINT COMMENT $ FIRST 15 DUFFINIAN TRIPLETS$
CAND = 0
THROUGH DUFTRI, FOR S=0, 1, S.GE.15
SCHTRP THROUGH SCHTRP, FOR CAND=CAND+1, 1, TRIP.(CAND)
DUFTRI PRINT FORMAT TRIFMT,CAND,CAND+1,CAND+2
 
VECTOR VALUES ROWFMT = $10(I5)*$
VECTOR VALUES TRIFMT = $3(I7)*$
END OF PROGRAM</syntaxhighlight>
{{out}}
<pre>FIRST 50 DUFFINIAN NUMBERS
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
 
FIRST 15 DUFFINIAN TRIPLETS
63 64 65
323 324 325
511 512 513
721 722 723
899 900 901
1443 1444 1445
2303 2304 2305
2449 2450 2451
3599 3600 3601
3871 3872 3873
5183 5184 5185
5617 5618 5619
6049 6050 6051
6399 6400 6401
8449 8450 8451</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,300 ⟶ 1,561:
860671-860673 910115-910117 913951-913953 963271-963273 968255-968257
991231-991233</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Predicate functions that checks wether an integer is a Duffinian number or not */
duffinianp(n):=if n#1 and not primep(n) and gcd(n,divsum(n))=1 then true$
 
/* Function that returns a list of the first len Duffinian numbers */
duffinian_count(len):=block(
[i:1,count:0,result:[]],
while count<len do (if duffinianp(i) then (result:endcons(i,result),count:count+1),i:i+1),
result)$
 
/* Function that returns a list of the first len Duffinian triples */
duffinian_triples_count(len):=block(
[i:1,count:0,result:[]],
while count<len do (if map(duffinianp,[i,i+1,i+2])=[true,true,true] then (result:endcons([i,i+1,i+2],result),count:count+1),i:i+1),
result)$
 
/* Test cases */
/* First 50 Duffinian numbers */
duffinian_count(50);
 
/* First 15 Duffinian triples */
duffinian_triples_count(15);
</syntaxhighlight>
{{out}}
<pre>
[4,8,9,16,21,25,27,32,35,36,39,49,50,55,57,63,64,65,75,77,81,85,93,98,100,111,115,119,121,125,128,129,133,143,144,155,161,169,171,175,183,185,187,189,201,203,205,209,215,217]
 
[[63,64,65],[323,324,325],[511,512,513],[721,722,723],[899,900,901],[1443,1444,1445],[2303,2304,2305],[2449,2450,2451],[3599,3600,3601],[3871,3872,3873],[5183,5184,5185],[5617,5618,5619],[6049,6050,6051],[6399,6400,6401],[8449,8450,8451]]
</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE DuffinianNumbers;
FROM InOut IMPORT WriteCard, WriteString, WriteLn;
 
CONST
MaxSigma = 10000;
 
VAR
seen, cur: CARDINAL;
sigma: ARRAY [1..MaxSigma] OF CARDINAL;
 
PROCEDURE CalculateSigmaTable;
VAR i, j: CARDINAL;
BEGIN
FOR i := 1 TO MaxSigma DO
sigma[i] := 0
END;
FOR i := 1 TO MaxSigma DO
j := i;
WHILE j <= MaxSigma DO
INC(sigma[j], i);
INC(j, i);
END
END
END CalculateSigmaTable;
 
PROCEDURE GCD(a, b: CARDINAL): CARDINAL;
VAR c: CARDINAL;
BEGIN
WHILE b # 0 DO
c := a MOD b;
a := b;
b := c
END;
RETURN a
END GCD;
 
PROCEDURE IsDuffinian(n: CARDINAL): BOOLEAN;
BEGIN
RETURN (sigma[n] > n+1) AND (GCD(n, sigma[n]) = 1)
END IsDuffinian;
 
PROCEDURE IsDuffinianTriple(n: CARDINAL): BOOLEAN;
BEGIN
RETURN IsDuffinian(n) AND IsDuffinian(n+1) AND IsDuffinian(n+2)
END IsDuffinianTriple;
 
BEGIN
CalculateSigmaTable;
WriteString("First 50 Duffinian numbers:");
WriteLn;
cur := 0;
FOR seen := 1 TO 50 DO
REPEAT INC(cur) UNTIL IsDuffinian(cur);
WriteCard(cur, 4);
IF seen MOD 10 = 0 THEN WriteLn END
END;
 
WriteLn;
WriteString("First 15 Duffinian triples:");
WriteLn;
cur := 0;
FOR seen := 1 TO 15 DO
REPEAT INC(cur) UNTIL IsDuffinianTriple(cur);
WriteCard(cur, 6);
WriteCard(cur+1, 6);
WriteCard(cur+2, 6);
WriteLn
END
END DuffinianNumbers.</syntaxhighlight>
{{out}}
<pre>First 50 Duffinian numbers:
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
 
First 15 Duffinian triples:
63 64 65
323 324 325
511 512 513
721 722 723
899 900 901
1443 1444 1445
2303 2304 2305
2449 2450 2451
3599 3600 3601
3871 3872 3873
5183 5184 5185
5617 5618 5619
6049 6050 6051
6399 6400 6401
8449 8450 8451</pre>
 
=={{header|Nim}}==
Line 1,378 ⟶ 1,765:
</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
isDuffinian(n) = (!isprime(n)) && (gcd(n, sigma(n)) == 1);
 
testDuffinians()=
{
print("First 50 Duffinian numbers:");
count = 0; n = 2;
while(count < 50,
if (isDuffinian(n),
print1(n, " ");
count++;
);
n++;
);
 
print("\n\nFifteen Duffinian triplets:");
count = 0; n = 2;
while (count < 15,
if (isDuffinian(n) && isDuffinian(n + 1) && isDuffinian(n + 2),
print(n, " ", n + 1, " ", n + 2);
count++;
);
n++;
);
}
 
testDuffinians();
</syntaxhighlight>
{{out}}
<pre>
First 50 Duffinian numbers:
4 8 9 16 21 25 27 32 35 36 39 49 50 55 57 63 64 65 75 77 81 85 93 98 100 111 115 119 121 125 128 129 133 143 144 155 161 169 171 175 183 185 187 189 201 203 205 209 215 217
 
Fifteen Duffinian triplets:
63 64 65
323 324 325
511 512 513
721 722 723
899 900 901
1443 1444 1445
2303 2304 2305
2449 2450 2451
3599 3600 3601
3871 3872 3873
5183 5184 5185
5617 5618 5619
6049 6050 6051
6399 6400 6401
8449 8450 8451
 
</pre>
=={{header|Perl}}==
{{libheader|ntheory}}
Line 1,475 ⟶ 1,915:
[166463,166464,166465] [167041,167042,167043]
</pre>
=={{header|PL/I}}==
<syntaxhighlight lang="pli">duffinianNumbers: procedure options(main);
%replace MAXSIGMA by 10000;
declare sigma (1:MAXSIGMA) fixed;
 
calculateSigmaTable: procedure;
declare (i, j) fixed;
do i=1 to MAXSIGMA;
sigma(i) = 0;
end;
do i=1 to MAXSIGMA;
do j=i to MAXSIGMA by i;
sigma(j) = sigma(j) + i;
end;
end;
end calculateSigmaTable;
 
gcd: procedure(aa,bb) returns(fixed);
declare (a, aa, b, bb, c) fixed;
a = aa;
b = bb;
do while(b > 0);
c = mod(a,b);
a = b;
b = c;
end;
return(a);
end gcd;
 
duffinian: procedure(n) returns(bit);
declare n fixed;
return(sigma(n) > n+1 & gcd(n, sigma(n)) = 1);
end duffinian;
 
triplet: procedure(n) returns(bit);
declare n fixed;
return(duffinian(n) & duffinian(n+1) & duffinian(n+2));
end triplet;
 
declare (i, n) fixed;
 
call calculateSigmaTable;
put skip list('First 50 Duffinian numbers:');
put skip;
n=0;
do i=1 to 50;
do n=n+1 repeat(n+1) while(^duffinian(n)); end;
put edit(n) (F(5));
if mod(i,10) = 0 then put skip;
end;
 
put skip;
put skip list('First 15 Duffinian triplets:');
n=0;
do i=1 to 15;
do n=n+1 repeat(n+1) while(^triplet(n)); end;
put skip edit(n, n+1, n+2) (F(7),F(7),F(7));
end;
end duffinianNumbers;</syntaxhighlight>
{{out}}
<pre>First 50 Duffinian numbers:
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
 
 
First 15 Duffinian triplets:
63 64 65
323 324 325
511 512 513
721 722 723
899 900 901
1443 1444 1445
2303 2304 2305
2449 2450 2451
3599 3600 3601
3871 3872 3873
5183 5184 5185
5617 5618 5619
6049 6050 6051
6399 6400 6401
8449 8450 8451</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
PR$CHAR: PROCEDURE (C); DECLARE C BYTE; CALL BDOS(2,C); END PR$CHAR;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
PR$NUM: PROCEDURE (N, WIDTH);
DECLARE N ADDRESS, WIDTH BYTE;
DECLARE S (6) BYTE INITIAL ('.....$');
DECLARE P ADDRESS, DG BASED P BYTE;
P = .S(5);
DIGIT:
P = P - 1;
DG = '0' + N MOD 10;
IF WIDTH > 0 THEN WIDTH = WIDTH - 1;
IF (N := N / 10) > 0 THEN GO TO DIGIT;
CALL PRINT(P);
DO WHILE WIDTH > 0;
CALL PR$CHAR(' ');
WIDTH = WIDTH - 1;
END;
END PR$NUM;
 
DECLARE MAX$SIGMA LITERALLY '10$001';
DECLARE SIGMA (MAX$SIGMA) ADDRESS;
CALC$SIGMA: PROCEDURE;
DECLARE (I, J) ADDRESS;
DO I = 1 TO MAX$SIGMA-1;
SIGMA(I) = 0;
END;
DO I = 1 TO MAX$SIGMA-1;
DO J = I TO MAX$SIGMA-1 BY I;
SIGMA(J) = SIGMA(J) + I;
END;
END;
END CALC$SIGMA;
 
GCD: PROCEDURE (X, Y) ADDRESS;
DECLARE (X, Y, Z) ADDRESS;
DO WHILE Y > 0;
Z = X MOD Y;
X = Y;
Y = Z;
END;
RETURN X;
END GCD;
 
DUFF: PROCEDURE (N) BYTE;
DECLARE N ADDRESS;
RETURN SIGMA(N) > N+1 AND GCD(N, SIGMA(N)) = 1;
END DUFF;
 
DUFF$TRIPLE: PROCEDURE (N) BYTE;
DECLARE N ADDRESS;
RETURN DUFF(N) AND DUFF(N+1) AND DUFF(N+2);
END DUFF$TRIPLE;
 
DECLARE N ADDRESS, I BYTE;
 
CALL CALC$SIGMA;
CALL PRINT(.('FIRST 50 DUFFINIAN NUMBERS:',13,10,'$'));
N = 0;
DO I = 1 TO 50;
DO WHILE NOT DUFF(N := N+1); END;
CALL PR$NUM(N, 4);
IF I MOD 10 = 0 THEN CALL PRINT(.(13,10,'$'));
END;
 
CALL PRINT(.(13,10,'FIRST 15 DUFFINIAN TRIPLES:',13,10,'$'));
N = 0;
DO I = 1 TO 15;
DO WHILE NOT DUFF$TRIPLE(N := N+1); END;
CALL PR$NUM(N, 6);
CALL PR$NUM(N+1, 6);
CALL PR$NUM(N+2, 6);
CALL PRINT(.(13,10,'$'));
END;
 
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre>FIRST 50 DUFFINIAN NUMBERS:
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
 
FIRST 15 DUFFINIAN TRIPLES:
63 64 65
323 324 325
511 512 513
721 722 723
899 900 901
1443 1444 1445
2303 2304 2305
2449 2450 2451
3599 3600 3601
3871 3872 3873
5183 5184 5185
5617 5618 5619
6049 6050 6051
6399 6400 6401
8449 8450 8451</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
Line 1,508 ⟶ 2,139:
j+=1
</syntaxhighlight>
 
=={{header|Quackery}}==
 
Line 1,667 ⟶ 2,299:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require "prime"
 
class Integer
 
def proper_divisors(prim_div = prime_division)
return [] if self == 1
primes = prim_div.flat_map{|prime, freq| [prime] * freq}
(1...primes.size).each_with_object([1]) do |n, res|
primes.combination(n).map{|combi| res << combi.inject(:*)}
end.flatten.uniq
end
 
def duffinian?
pd = prime_division
return false if pd.sum(&:last) < 2
gcd(proper_divisors(pd).sum + self) == 1
end
 
end
 
n = 50
puts "The first #{n} Duffinian numbers:"
(1..).lazy.select(&:duffinian?).first(n).each_slice(10) do |slice|
puts "%4d" * slice.size % slice
end
 
puts "\nThe first #{n} Duffinian triplets:"
(1..).each_cons(3).lazy.select{|slice| slice.all?(&:duffinian?)}.first(n).each do |group|
puts "%8d" * group.size % group
end
</syntaxhighlight>
{{out}}
<pre>The first 50 Duffinian numbers:
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
 
The first 50 Duffinian triplets:
63 64 65
323 324 325
511 512 513
721 722 723
899 900 901
1443 1444 1445
2303 2304 2305
2449 2450 2451
3599 3600 3601
3871 3872 3873
5183 5184 5185
5617 5618 5619
6049 6050 6051
6399 6400 6401
8449 8450 8451
10081 10082 10083
10403 10404 10405
11663 11664 11665
12481 12482 12483
13447 13448 13449
13777 13778 13779
15841 15842 15843
17423 17424 17425
19043 19044 19045
26911 26912 26913
30275 30276 30277
36863 36864 36865
42631 42632 42633
46655 46656 46657
47523 47524 47525
53137 53138 53139
58563 58564 58565
72961 72962 72963
76175 76176 76177
79523 79524 79525
84099 84100 84101
86527 86528 86529
94177 94178 94179
108899 108900 108901
121103 121104 121105
125315 125316 125317
128017 128018 128019
129599 129600 129601
137287 137288 137289
144399 144400 144401
144721 144722 144723
154567 154568 154569
158403 158404 158405
166463 166464 166465
167041 167042 167043
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func is_duffinian(n) {
Line 1,702 ⟶ 2,426:
(8449, 8450, 8451)
</pre>
 
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
 
1,985

edits