Jump to content

Increasing gaps between consecutive Niven numbers: Difference between revisions

(Python example)
Line 600:
276 1,039,028,518 18,879,988,824
</pre>
 
=={{header|Nim}}==
{{trans|C++}}
<lang Nim>import strformat
 
func digitsSum(n, sum: uint64): uint64 =
## Returns the sum of the digits of n given the sum of the digits of n - 1.
result = sum + 1
var n = n
while n > 0 and n mod 10 == 0:
dec result, 9
n = n div 10
 
func divisible(n, d: uint64): bool {.inline.} =
if (d and 1) == 0 and (n and 1) == 1:
return false
result = n mod d == 0
 
when isMainModule:
 
echo "Gap index Gap Niven index Niven number"
 
var
niven, gap, sum, nivenIndex = 0u64
previous, gapIndex = 1u64
 
while gapIndex <= 32:
inc niven
sum = digitsSum(niven, sum)
if divisible(niven, sum):
if niven > previous + gap:
gap = niven - previous
echo fmt"{gapIndex:9d} {gap:4d} {nivenIndex:12d} {previous:13d}"
inc gapIndex
previous = niven
inc nivenIndex</lang>
 
{{out}}
<pre>Gap index Gap Niven index Niven number
1 1 1 1
2 2 10 10
3 6 11 12
4 7 26 63
5 8 28 72
6 10 32 90
7 12 83 288
8 14 102 378
9 18 143 558
10 23 561 2889
11 32 716 3784
12 36 1118 6480
13 44 2948 19872
14 45 4194 28971
15 54 5439 38772
16 60 33494 297864
17 66 51544 478764
18 72 61588 589860
19 88 94748 989867
20 90 265336 2879865
21 99 800054 9898956
22 108 3750017 49989744
23 126 6292149 88996914
24 135 44194186 689988915
25 144 55065654 879987906
26 150 61074615 989888823
27 153 179838772 2998895823
28 192 399977785 6998899824
29 201 497993710 8889999624
30 234 502602764 8988988866
31 258 547594831 9879997824
32 276 1039028518 18879988824</pre>
 
=={{header|Pascal}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.