Duffinian numbers: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 38:
=={{header|ALGOL 68}}==
Constructs a table of divisor counts without doing any divisions.
<langsyntaxhighlight lang="algol68">BEGIN # find Duffinian numbers: non-primes relatively prime to their divisor count #
INT max number := 500 000; # largest number we will consider #
# iterative Greatest Common Divisor routine, returns the gcd of m and n #
Line 87:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 122:
=={{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. :)
<langsyntaxhighlight lang="applescript">on aliquotSum(n)
if (n < 2) then return 0
set sum to 1
Line 221:
end task
 
return task(50, 20, 3) -- First 50 Duffinians, first 20 3-item tuplets.</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"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
Line 249:
11663 11664 11665
12481 12482 12483
13447 13448 13449"</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">duffinian?: function [n]->
and? [not? prime? n]
[
Line 286:
loop split.every: 5 first15 'row [
print map row 'item -> pad.right as.code item 17
]</langsyntaxhighlight>
 
{{out}}
Line 303:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <numeric>
Line 347:
}
std::cout << '\n';
}</langsyntaxhighlight>
 
{{out}}
Line 380:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit.smart grouping io kernel lists
lists.lazy math math.primes math.primes.factors math.statistics
prettyprint sequences sequences.deep ;
Line 397:
 
"First 15 Duffinian triplets:" print
15 triples ltake list>array simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 428:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 479:
fmt.Println(s[1 : len(s)-1])
}
}</langsyntaxhighlight>
 
{{out}}
Line 510:
Implementation:
 
<langsyntaxhighlight Jlang="j">sigmasum=: >:@#.~/.~&.q:
composite=: 1&< * 0 = 1&p:
duffinian=: composite * 1 = ] +. sigmasum</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> 5 10$(#~ duffinian) 1+i.1000
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
Line 537:
6049 6050 6051
6399 6400 6401
8449 8450 8451</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function σ(n)
Line 568:
 
testDuffinians()
</langsyntaxhighlight>{{out}}
<pre>
First 50 Duffinian numbers:
Line 593:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[DuffianQ]
DuffianQ[n_Integer] := CompositeQ[n] \[And] CoprimeQ[DivisorSigma[1, n], n]
dns = Select[DuffianQ][Range[1000000]];
Take[dns, UpTo[50]]
triplets = ToString[dns[[#]]] <> "\[LongDash]" <> ToString[dns[[# + 2]]] & /@ SequencePosition[Differences[dns], {1, 1}][[All, 1]]
Multicolumn[triplets, {Automatic, 5}, Appearance -> "Horizontal"]</langsyntaxhighlight>
{{out}}
First 50 Duffinian numbers and Duffinian triplets below a million:
Line 633:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say state>;
Line 658:
 
say 'First 39 Duffinian triplets:';
say table 3, @triples;</langsyntaxhighlight>
{{out}}
<pre>First 50 Duffinian numbers:
Line 683:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">duffinian</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #004600;">false</span><span style="color: #0000FF;">}</span>
Line 706:
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">pad_tail</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"[%d,%d,%d]"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">s</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">24</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 50 Duffinian triplets:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 730:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
my @duffinians = lazy (3..*).hyper.grep: { !.is-prime && $_ gcd .&divisors.sum == 1 };
Line 739:
put "\nFirst 40 Duffinian triplets:\n" ~
((^∞).grep: -> $n { (@duffinians[$n] + 1 == @duffinians[$n + 1]) && (@duffinians[$n] + 2 == @duffinians[$n + 2]) })[^40]\
.map( { "({@duffinians[$_ .. $_+2].join: ', '})" } ).batch(4)».fmt("%-24s").join: "\n";</langsyntaxhighlight>
{{out}}
<pre>First 50 Duffinian numbers:
Line 761:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_duffinian(n) {
n.is_composite && n.is_coprime(n.sigma)
}
Line 771:
15.by{|n| ^3 -> all {|k| is_duffinian(n+k) } }.each {|n|
printf("(%s, %s, %s)\n", n, n+1, n+2)
}</langsyntaxhighlight>
 
{{out}}
Line 800:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./math" for Int, Nums
import "./seq" for Lst
import "./fmt" for Fmt
Line 826:
}
System.print("\nFirst 50 Duffinian triplets:")
for (chunk in Lst.chunks(triplets[0..49], 4)) Fmt.print("$-25s", chunk)</langsyntaxhighlight>
 
{{out}}
Line 854:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 916:
N:= N+1;
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits