How to resolve the algorithm Harshad or Niven series step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Harshad or Niven series step by step in the Wren 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 Wren programming language
Source code in the wren programming language
var niven = Fiber.new {
var n = 1
while (true) {
var i = n
var sum = 0
while (i > 0) {
sum = sum + i%10
i = (i/10).floor
}
if (n%sum == 0) Fiber.yield(n)
n = n + 1
}
}
System.print("The first 20 Niven numbers are:")
for (i in 1..20) {
System.write("%(niven.call()) ")
}
System.write("\n\nThe first Niven number greater than 1000 is: ")
while (true) {
var niv = niven.call()
if (niv > 1000) {
System.print(niv)
break
}
}
You may also check:How to resolve the algorithm Stream merge step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Object Pascal programming language
You may also check:How to resolve the algorithm String matching step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the Sidef programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the Ada programming language