How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the XPL0 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 XPL0 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 XPL0 programming language

Source code in the xpl0 programming language

func DigitSum(N, Sum);  \Return sum of digits in N given sum of digits in N-1
int  N, Sum;
[Sum:= Sum+1;
while N > 0 and rem(N/10) = 0 do
   [Sum:= Sum -9;
   N:= N/10;
   ];
return Sum;
];

int Previous, Gap, S, NivenIndex, GapIndex, Niven;
def Tab = 9;

[Previous:= 1;
Gap:= 0;
S:= 0;
NivenIndex:= 0;
GapIndex:= 1;

Text(0, "Index   Gap     Index   Niven^m^j");

Niven:= 1;

while GapIndex <= 23 do
   [S:= DigitSum(Niven, S);
   if rem(Niven/S) = 0 then
      [if Niven > Previous + Gap then
         [Gap:= Niven - Previous;
         IntOut(0, GapIndex);  ChOut(0, Tab);
         IntOut(0, Gap);  ChOut(0, Tab);
         IntOut(0, NivenIndex);  ChOut(0, Tab);
         IntOut(0, Previous);  CrLf(0);
         GapIndex:= GapIndex+1;
         ];
      Previous:= Niven;
      NivenIndex:= NivenIndex+1;
      ];
   Niven:= Niven+1;
   ];
]

  

You may also check:How to resolve the algorithm Primes - allocate descendants to their ancestors step by step in the Perl programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Bitmap/Write a PPM file step by step in the Aime programming language
You may also check:How to resolve the algorithm Isograms and heterograms step by step in the Raku programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Go programming language