How to resolve the algorithm Harshad or Niven series step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Harshad or Niven series step by step in the PL/I programming language

Table of Contents

Problem Statement

The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example,   42   is a Harshad number as   42   is divisible by   (4 + 2)   without remainder. Assume that the series is defined as the numbers in increasing order.

The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to:

Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Harshad or Niven series step by step in the PL/I programming language

Source code in the pl/i programming language

*process source or(!) xref attributes;
 niven: Proc Options(main);
 /*********************************************************************
 * 08-06.2013 Walter Pachl translated from Rexx
 *                         with a slight improvement:  Do j=y+1 By 1;
 *********************************************************************/
 Dcl (ADDR,HBOUND,MOD,SUBSTR,VERIFY) Builtin;
 Dcl SYSPRINT Print;

 Dcl (x,y) dec fixed(8);
 x=20;
 y=1000;
 Begin;
   Dcl (n(x),j) Dec Fixed(8);
   Dcl ni Bin Fixed(31) Init(0);
   Dcl result Char(100) Var Init('');
 loop:
   Do j=1 By 1;
     If mod(j,sumdigs(j))=0 Then Do;
       ni+=1;
       n(ni)=j;
       result=result!!' '!!d2c(j);
       If ni=x Then Leave loop;
       End;
     End;
   Put Edit('first 20 Niven numbers: ',result)(Skip,a,a);
   Do j=y+1 By 1;
     If mod(j,sumdigs(j))=0 Then
       Leave;
     End;
   Put Edit('first Niven number > ',d2c(y),' is: ',d2c(j))(Skip,4(a));
   End;

 sumDigs: proc(z) Returns(Dec Fixed(3));
 Dcl z Pic'(8)9';
 Dcl d(8) Pic'9' Based(addr(z));
 Dcl i Bin Fixed(31);
 Dcl sd Dec Fixed(3) Init(0);
 Do i=1 To hbound(d);
   sd+=d(i);
   End;
 Return(sd);
 End;

 d2c: Proc(z) Returns(char(8) Var);
 Dcl z Pic'(8)z';
 Dcl p Bin Fixed(31);
 p=verify(z,' ');
 Return(substr(z,p));
 End;

 End;

  

You may also check:How to resolve the algorithm Command-line arguments step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the C programming language
You may also check:How to resolve the algorithm Order disjoint list items step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the C# programming language