Duffinian numbers: Difference between revisions
Content added Content deleted
m (add RPL) |
(Created Nim solution.) |
||
Line 794: | Line 794: | ||
860671-860673 910115-910117 913951-913953 963271-963273 968255-968257 |
860671-860673 910115-910117 913951-913953 963271-963273 968255-968257 |
||
991231-991233</pre> |
991231-991233</pre> |
||
=={{header|Nim}}== |
|||
{{trans|ALGOL 68}} |
|||
<syntaxhighlight lang="Nim">import std/[algorithm, math, strformat] |
|||
const MaxNumber = 500_000 |
|||
# Construct a table of the divisor counts. |
|||
var ds: array[1..MaxNumber, int] |
|||
ds.fill 1 |
|||
for i in 2..MaxNumber: |
|||
for j in countup(i, MaxNumber, i): |
|||
ds[j] += i |
|||
# Set the divisor counts of non-Duffinian numbers to 0. |
|||
ds[1] = 0 # 1 is not Duffinian. |
|||
for n in 2..MaxNumber: |
|||
let nds = ds[n] |
|||
if nds == n + 1 or gcd(n, nds) != 1: |
|||
# "n" is prime or is not relatively prime to its divisor sum. |
|||
ds[n] = 0 |
|||
# Show the first 50 Duffinian numbers. |
|||
echo "First 50 Duffinian numbers:" |
|||
var dcount = 0 |
|||
var n = 1 |
|||
while dcount < 50: |
|||
if ds[n] != 0: |
|||
stdout.write &" {n:3}" |
|||
inc dcount |
|||
if dcount mod 25 == 0: |
|||
echo() |
|||
inc n |
|||
echo() |
|||
# Show the Duffinian triplets below MaxNumber. |
|||
echo &"The Duffinian triplets up to {MaxNumber}:" |
|||
dcount = 0 |
|||
for n in 3..MaxNumber: |
|||
if ds[n - 2] != 0 and ds[n - 1] != 0 and ds[n] != 0: |
|||
inc dcount |
|||
stdout.write &" {(n - 2, n - 1, n): ^24}" |
|||
stdout.write if dcount mod 4 == 0: '\n' else: ' ' |
|||
echo() |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
The output is identical to that of the Algol 68 program, but the formatting is different. |
|||
<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 |
|||
The Duffinian triplets up to 500000: |
|||
(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) (175231, 175232, 175233) (177607, 177608, 177609) |
|||
(181475, 181476, 181477) (186623, 186624, 186625) (188497, 188498, 188499) (197191, 197192, 197193) |
|||
(199711, 199712, 199713) (202499, 202500, 202501) (211249, 211250, 211251) (230399, 230400, 230401) |
|||
(231199, 231200, 231201) (232561, 232562, 232563) (236195, 236196, 236197) (242063, 242064, 242065) |
|||
(243601, 243602, 243603) (248003, 248004, 248005) (260099, 260100, 260101) (260641, 260642, 260643) |
|||
(272483, 272484, 272485) (274575, 274576, 274577) (285155, 285156, 285157) (291599, 291600, 291601) |
|||
(293763, 293764, 293765) (300303, 300304, 300305) (301087, 301088, 301089) (318095, 318096, 318097) |
|||
(344449, 344450, 344451) (354481, 354482, 354483) (359551, 359552, 359553) (359999, 360000, 360001) |
|||
(367235, 367236, 367237) (374543, 374544, 374545) (403201, 403202, 403203) (406801, 406802, 406803) |
|||
(417697, 417698, 417699) (419903, 419904, 419905) (423199, 423200, 423201) (435599, 435600, 435601) |
|||
(468511, 468512, 468513) (470449, 470450, 470451) (488071, 488072, 488073) |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |