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

Published on 12 May 2024 09:40 PM

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

Source code in the clu programming language

digit_sum = proc (n: int) returns (int)
    sum: int := 0
    while n > 0 do
        sum := sum + n // 10
        n := n / 10
    end
    return (sum)
end digit_sum 

harshads = iter (n: int) yields (int)
    while true do
        n := n + 1
        if n // digit_sum(n) = 0 then yield(n) end
    end
end harshads

start_up = proc ()
    po: stream := stream$primary_output()
    count: int := 0
    
    stream$putl(po, "First 20 Harshad numbers: ")
    for h: int in harshads(0) do
        stream$putright(po, int$unparse(h), 3)
        count := count + 1
        if count = 20 then break end
    end
    
    stream$puts(po, "\nFirst Harshad number above 1000: ")
    for h: int in harshads(1000) do
        stream$putl(po, int$unparse(h))
        break
    end
end start_up

  

You may also check:How to resolve the algorithm Reduced row echelon form step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the PHP programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Nim programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the C# programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the JavaScript programming language