How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

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

  • The provided Go code generates and finds Harshad (Niven) numbers with gaps. Harshad numbers are numbers that are divisible by the sum of their digits. The code calculates the next Harshad number and the gap between the current and previous Harshad number, and prints the results.

  • It defines two functions, newSum and newHarshard, that generate sequences of numbers.

  • newSum creates a closure that generates a sequence of numbers starting from 1 and incrementing by 1. It returns a function that returns the next number in the sequence.

  • newHarshard creates a closure that generates a sequence of Harshad numbers. It starts with the first Harshad number, 1, and repeatedly generates the next Harshad number until it reaches a certain limit (in this case, 20 billion). It returns a function that returns the next Harshad number in the sequence.

  • The commatize function formats a number with commas as thousand separators.

  • The main function:

    1. Prints a header row.
    2. Initializes variables to keep track of the previous highest gap and the previous Niven number.
    3. Enters a loop that generates Harshad numbers.
    4. For each Harshad number, it calculates the gap between the current and previous Niven number.
    5. If the gap is greater than the previous highest gap, it prints the gap, index of the gap, and starting Niven number.
    6. Updates the previous highest gap and previous Niven number.
  • The code generates and finds Harshad numbers with gaps, printing the results in the following format:

Gap    Index of gap   Starting Niven
===   =============   =============="
235   1,000,000,000   9,999,999,977
465   2,000,000,000   19,999,999,954
  • This indicates that the largest gap between consecutive Harshad numbers found was 465, which occurred between the 2 billionth and 3 billionth Harshad numbers, with the starting Niven number being 19,999,999,954.

Source code in the go programming language

package main

import "fmt"

type is func() uint64

func newSum() is {
    var ms is
    ms = func() uint64 {
        ms = newSum()
        return ms()
    }
    var msd, d uint64
    return func() uint64 {
        if d < 9 {
            d++
        } else {
            d = 0
            msd = ms()
        }
        return msd + d
    }
}

func newHarshard() is {
    i := uint64(0)
    sum := newSum()
    return func() uint64 {
        for i++; i%sum() != 0; i++ {
        }
        return i
    }
}

func commatize(n uint64) string {
    s := fmt.Sprintf("%d", n)
    le := len(s)
    for i := le - 3; i >= 1; i -= 3 {
        s = s[0:i] + "," + s[i:]
    }
    return s
}

func main() {
    fmt.Println("Gap    Index of gap   Starting Niven")
    fmt.Println("===   =============   ==============")
    h := newHarshard()
    pg := uint64(0) // previous highest gap
    pn := h()       // previous Niven number
    for i, n := uint64(1), h(); n <= 20e9; i, n = i+1, h() {
        g := n - pn
        if g > pg {
            fmt.Printf("%3d   %13s   %14s\n", g, commatize(i), commatize(pn))
            pg = g
        }
        pn = n
    }
}


  

You may also check:How to resolve the algorithm Price fraction step by step in the Quackery programming language
You may also check:How to resolve the algorithm Fractal tree step by step in the Wren programming language
You may also check:How to resolve the algorithm Munching squares step by step in the Racket programming language
You may also check:How to resolve the algorithm Set step by step in the Java programming language
You may also check:How to resolve the algorithm S-expressions step by step in the Perl programming language