How to resolve the algorithm Harshad or Niven series step by step in the ALGOL W programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Harshad or Niven series step by step in the ALGOL W 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 ALGOL W programming language

Source code in the algol programming language

begin % find members of the Harshad/Niven series - numbers divisible by the sum of their digits %
    % returns the next member of the series above n                                             %
    integer procedure nextHarshad ( integer value n ) ;
    begin
        integer h, s;
        h := n;
        while begin
                  integer v;
                  v := h := h + 1;
                  s := 0;
                  while v > 0 do begin
                      s := s + v rem 10;
                      v :=     v div 10
                  end while_v_gt_0 ;
                  h rem s not = 0
        end do begin end;
        h
    end nextHarshad ;
    integer h;
    % show the first 20 members of the seuence %
    write( "First 20 Harshad/Niven numbers:" );
    h := 0;
    for i := 1 until 20 do begin
        h := nextHarshad( h );
        writeon( i_w := 1, s_w := 0, " ", h )
    end for_i ;
    write( i_w := 1, s_w := 0, "First Harshad/Niven number > 1000: ", nextHarshad( 1000 ) );
end.

  

You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Doomsday rule step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Array length step by step in the D programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the VBA programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Nemerle programming language