Increasing gaps between consecutive Niven numbers: Difference between revisions

Content added Content deleted
(Added XPL0 example.)
No edit summary
Line 571: Line 571:
26 150 61,074,615 989,888,823
26 150 61,074,615 989,888,823
27 153 179,838,772 2,998,895,823</pre>
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>