Inconsummate numbers in base 10: Difference between revisions

Created Nim solution.
(Created Nim solution.)
Line 523:
The 1000th:
6996
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim>import std/[math, strformat, sugar]
 
const
D = 4 # Sufficient to find the one thousandth first, as required.
N = 10^D # Size of sieve.
M = 9 * (D + 1) * N # Maximum value for the number to divide.
 
func digitalSum(n: Natural): int =
## Return the digital sum of "n".
var n = n
while n != 0:
inc result, n mod 10
n = n div 10
 
# Fill a sieve to find consummate numbers.
var isConsummate: array[1..N, bool]
for n in 1..M:
let ds = n.digitalSum
if n mod ds == 0:
let q = n div ds
if q <= N:
isConsummate[q] = true
 
# Build list of inconsummate numbers.
let inconsummateNums = collect:
for n, c in isConsummate:
if not c: n
 
echo "First 50 inconsummate numbers in base 10:"
for i in 0..49:
stdout.write &"{inconsummateNums[i]:3}"
stdout.write if i mod 10 == 9: '\n' else: ' '
 
echo &"\nOne thousandth is {inconsummateNums[999]}."
</syntaxhighlight>
 
{{out}}
<pre>First 50 inconsummate numbers in base 10:
62 63 65 75 84 95 161 173 195 216
261 266 272 276 326 371 372 377 381 383
386 387 395 411 416 422 426 431 432 438
441 443 461 466 471 476 482 483 486 488
491 492 493 494 497 498 516 521 522 527
 
One thousandth is 6996.
</pre>
 
256

edits