Increasing gaps between consecutive Niven numbers: Difference between revisions

Content deleted Content added
Elfish (talk | contribs)
No edit summary
Trizen (talk | contribs)
Added Sidef
 
(3 intermediate revisions by 3 users not shown)
Line 672:
</pre>
 
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func digsum n sum .
sum += 1
while n > 0 and n mod 10 = 0
sum -= 9
n = n div 10
.
return sum
.
func divisible n d .
if d mod 2 = 0 and n mod 2 = 1
return 0
.
return if n mod d = 0
.
numfmt 0 8
previous = 1
print " Gap index Gap Niven index Niven number"
print " --------- --- ----------- ------------"
for niven = 1 to 10000000
sum = digsum niven sum
if divisible niven sum = 1
if niven > previous + gap
gap_index += 1
gap = niven - previous
print gap_index & gap & " " & niven_index & " " & previous
.
previous = niven
niven_index += 1
.
.
</syntaxhighlight>
{{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
</pre>
 
=={{header|Fortran}}==
Line 788 ⟶ 849:
31 258 547,594,831 9,879,997,824
32 276 1,039,028,518 18,879,988,824</pre>
 
 
=={{header|FreeBASIC}}==
Line 2,091 ⟶ 2,151:
tell: say arg(1); return</syntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br> !-->
 
=={{header|RPL}}==
{{works with|RPL|HP49-C}}
« →STR 0.
1. PICK3 SIZE '''FOR''' j
OVER j DUP SUB STR→ +
'''NEXT''' NIP
» '<span style="color:blue">∑DIG</span>' STO
« '''DO''' 1 + '''UNTIL''' DUPDUP <span style="color:blue">∑DIG</span> / FP NOT '''END'''
» '<span style="color:blue">NEXTNIVEN</span>' STO
« { { 1 1 1 1 } } 1 1 1 → res gap gapindex nivindex
« 1
'''WHILE''' res SIZE 15 < '''REPEAT'''
DUP <span style="color:blue">NEXTNIVEN</span> DUP2 SWAP -
'''IF''' DUP gap >
'''THEN''' DUP 'gap' STO 'gapindex' INCR SWAP nivindex 5 ROLL
4 →LIST 1 →LIST 'res' SWAP STO+
'''ELSE''' ROT DROP2 '''END'''
'nivindex' 1 STO+
'''END'''
DROP res
» » '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { { 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 } }
</pre>
 
=={{header|Ruby}}==
Line 2,132 ⟶ 2,220:
126 6292149 88996914
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 2,219 ⟶ 2,308:
31 258 547,594,831 9,879,997,824
32 276 1,039,028,518 18,879,988,824
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var threshold = 10_000_000
 
say "Gap Index of gap Starting Niven"
for (var (n=1, index=0, gap=0, prev=1); index <= threshold; ++n) {
n.is_div(n.digits_sum) || next
if ((var diff = (n - prev)) > gap) {
gap = diff
printf("%3d %15s %15s\n", gap, index, prev)
}
prev = n
++index
}</syntaxhighlight>
{{out}}
<pre>
Gap Index of gap Starting Niven
1 1 1
2 10 10
6 11 12
7 26 63
8 28 72
10 32 90
12 83 288
14 102 378
18 143 558
23 561 2889
32 716 3784
36 1118 6480
44 2948 19872
45 4194 28971
54 5439 38772
60 33494 297864
66 51544 478764
72 61588 589860
88 94748 989867
90 265336 2879865
99 800054 9898956
108 3750017 49989744
126 6292149 88996914
</pre>
 
Line 2,225 ⟶ 2,355:
{{libheader|Wren-fmt}}
Limited to Niven numbers up to 1 billion in order to finish in a reasonable time (a little under 2 minutes on my machine).
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var newSum // recursive
Line 2,267 ⟶ 2,397:
var g = n - pn
if (g > pg) {
SystemFmt.print("%(Fmt.d(3,$3d g)) %(Fmt.dc(13$,13d i)) $,14d", %(Fmt.dc(14g, i, pn))")
pg = g
}