How to resolve the algorithm Largest number divisible by its digits step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Largest number divisible by its digits step by step in the Lua programming language

Table of Contents

Problem Statement

Find the largest base 10 integer whose digits are all different,   and   is evenly divisible by each of its individual digits.

These numbers are also known as   Lynch-Bell numbers,   numbers   n   such that the (base ten) digits are all different (and do not include zero)   and   n   is divisible by each of its individual digits.

135   is evenly divisible by   1,   3,   and   5.

Note that the digit zero (0) can not be in the number as integer division by zero is undefined. The digits must all be unique so a base ten number will have at most 9 digits. Feel free to use analytics and clever algorithms to reduce the search space your example needs to visit, but it must do an actual search. (Don't just feed it the answer and verify it is correct.)

Do the same thing for hexadecimal.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Largest number divisible by its digits step by step in the Lua programming language

Source code in the lua programming language

function isDivisible(n)
    local t = n
    local a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    while t ~= 0 do
        local r = t % 10
        if r == 0 then
            return false
        end
        if n % r ~= 0 then
            return false
        end
        if a[r + 1] > 0 then
            return false
        end
        a[r + 1] = 1
        t = math.floor(t / 10)
    end

    return true
end

for i=9999999999,0,-1 do
    if isDivisible(i) then
        print(i)
        break
    end
end


  

You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the bc programming language
You may also check:How to resolve the algorithm Arrays step by step in the REBOL programming language
You may also check:How to resolve the algorithm Box the compass step by step in the Red programming language
You may also check:How to resolve the algorithm Plasma effect step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Giuga numbers step by step in the Ring programming language