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

Source code in the wren programming language

import "./fmt" for Fmt

var newSum // recursive
newSum = Fn.new {
    var ms // also recursive
    ms = Fn.new {
        ms = newSum.call()
        return ms.call()
    }
    var msd = 0
    var d = 0
    return Fn.new {
        if (d < 9) {
            d = d + 1
        } else {
            d = 0
            msd = ms.call()
        }
        return msd + d
    }
}

var newHarshard = Fn.new {
    var  i = 0
    var sum = newSum.call()
    return Fn.new {
        i = i + 1
        while (i%sum.call() != 0) i = i + 1
        return i
    }
}

System.print("Gap    Index of gap   Starting Niven")
System.print("===   =============   ==============")
var h = newHarshard.call() 
var pg = 0        // previous highest gap
var pn = h.call() // previous Niven number
var i = 1
var n = h.call()
while (n <= 1e9) {
    var g = n - pn
    if (g > pg) {
        Fmt.print("$3d   $,13d   $,14d", g, i, pn)
        pg = g
    }
    pn = n
    i = i + 1
    n = h.call()
}


  

You may also check:How to resolve the algorithm Guess the number step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Convex hull step by step in the Mercury programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the Python programming language
You may also check:How to resolve the algorithm CUSIP step by step in the Quackery programming language