How to resolve the algorithm Humble numbers step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Humble numbers step by step in the Lua programming language

Table of Contents

Problem Statement

Humble numbers are positive integers which have   no   prime factors   >   7.

Humble numbers are also called   7-smooth numbers,   and sometimes called   highly composite, although this conflicts with another meaning of   highly composite numbers.

Another way to express the above is:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Humble numbers step by step in the Lua programming language

Source code in the lua programming language

function isHumble(n)
    local n2 = math.floor(n)

    if n2 <= 1 then
        return true
    end
    if n2 % 2 == 0 then
        return isHumble(n2 / 2)
    end
    if n2 % 3 == 0 then
        return isHumble(n2 / 3)
    end
    if n2 % 5 == 0 then
        return isHumble(n2 / 5)
    end
    if n2 % 7 == 0 then
        return isHumble(n2 / 7)
    end

    return false
end

function main()
    local limit = 10000
    local humble = {0, 0, 0, 0, 0, 0, 0, 0, 0}
    local count = 0
    local num = 1

    while count < limit do
        if isHumble(num) then
            local buffer = string.format("%d", num)
            local le = string.len(buffer)
            if le > 9 then
                break
            end
            humble[le] = humble[le] + 1

            if count < 50 then
                io.write(num .. " ")
            end
            count = count + 1
        end
        num = num + 1
    end
    print("\n")

    print("Of the first " .. count .. " humble numbers:")
    for num=1,9 do
        print(string.format("%5d have %d digits", humble[num], num))
    end
end

main()


  

You may also check:How to resolve the algorithm Loops/Foreach step by step in the K programming language
You may also check:How to resolve the algorithm Factorial step by step in the Klong programming language
You may also check:How to resolve the algorithm Special characters step by step in the OCaml programming language
You may also check:How to resolve the algorithm Pythagorean quadruples step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the Go programming language