Increasing gaps between consecutive Niven numbers: Difference between revisions
Content deleted Content added
Added XPL0 example. |
Added Sidef |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 572:
27 153 179,838,772 2,998,895,823</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Windows,SysUtils,StdCtrls}}
Because the code requires almost two minutes to run, it has optional callback that supplies information that can be used to power a progress bar.
<syntaxhighlight lang="Delphi">
function SumDigits(N: integer): integer;
{Sum the integers in a number}
var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
until N<1;
end;
function IsNiven(N: integer): boolean;
{Test if N is evenly divisible by sum}
{i.e. is it a Niven Number}
var Sum: integer;
begin
Sum:=SumDigits(N);
Result:=(N mod Sum)=0;
end;
function GetNextNiven(Start: integer): integer;
{Get the next Niven number after Start}
begin
repeat Inc(Start)
until IsNiven(Start);
Result:=Start;
end;
procedure ShowNivenGaps(Memo: TMemo; Prog: TProgress);
{Show when gaps between sucessive Niven Numbers changes}
var I: integer;
var N1,N2,Gap: integer;
var Cnt: integer;
const Limit = 65000000;
begin
Memo.Lines.Add(' Gap Gap Niven Niven Next');
Memo.Lines.Add('Index Value Index Number Niven Number');
Memo.Lines.Add('----- ----- -------- --------- ------------');
Gap:=0; Cnt:=0;
N1:=GetNextNiven(0);
for I:=1 to Limit do
begin
{Get next Niven and test if Gap has changed}
N2:=GetNextNiven(N1);
if (N2-N1)>Gap then
begin
Gap:=N2-N1;
Inc(Cnt);
Memo.Lines.Add(Format('%5d%6d%15.0n%15.0n%15.0n',[Cnt,Gap,I+0.0,N1+0.0,N2+0.0]));
end;
N1:=N2;
if Assigned(Prog) and ((I mod 100000)=0) then Prog(MulDiv(100,I,Limit));
end;
end;
</syntaxhighlight>
{{out}}
<pre>
Gap Gap Niven Niven Next
Index Value Index Number Niven Number
----- ----- -------- --------- ------------
1 1 1 1 2
2 2 10 10 12
3 6 11 12 18
4 7 26 63 70
5 8 28 72 80
6 10 32 90 100
7 12 83 288 300
8 14 102 378 392
9 18 143 558 576
10 23 561 2,889 2,912
11 32 716 3,784 3,816
12 36 1,118 6,480 6,516
13 44 2,948 19,872 19,916
14 45 4,194 28,971 29,016
15 54 5,439 38,772 38,826
16 60 33,494 297,864 297,924
17 66 51,544 478,764 478,830
18 72 61,588 589,860 589,932
19 88 94,748 989,867 989,955
20 90 265,336 2,879,865 2,879,955
21 99 800,054 9,898,956 9,899,055
22 108 3,750,017 49,989,744 49,989,852
23 126 6,292,149 88,996,914 88,997,040
24 135 44,194,186 689,988,915 689,989,050
25 144 55,065,654 879,987,906 879,988,050
26 150 61,074,615 989,888,823 989,888,973
</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 688 ⟶ 849:
31 258 547,594,831 9,879,997,824
32 276 1,039,028,518 18,879,988,824</pre>
=={{header|FreeBASIC}}==
Line 1,991 ⟶ 2,151:
tell: say arg(1); return</syntaxhighlight>
{{out|output|text= 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,032 ⟶ 2,220:
126 6292149 88996914
</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 2,119 ⟶ 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,125 ⟶ 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="
var newSum // recursive
Line 2,167 ⟶ 2,397:
var g = n - pn
if (g > pg) {
pg = g
}
|