How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the Delphi programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the Delphi programming language
Table of Contents
Problem Statement
Note: Niven numbers are also called Harshad numbers.
Niven numbers are positive integers which are evenly divisible by the sum of its digits (expressed in base ten). Evenly divisible means divisible with no remainder.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the Delphi programming language
Source code in the delphi programming language
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;
You may also check:How to resolve the algorithm Substitution cipher step by step in the Pike programming language
You may also check:How to resolve the algorithm Set consolidation step by step in the VBScript programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the OCaml programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the Mathematica/Wolfram Language programming language