How to resolve the algorithm Largest number divisible by its digits step by step in the Nanoquery 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 Nanoquery 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 Nanoquery programming language

Source code in the nanoquery programming language

s = ""

def uniqueDigits(i)
        global s

        // returns true, if unique digits, false otherwise
        for k in range(0, len(s) - 2)
                for l in range(k + 1, len(s) - 1)
                        if (s[l] = "0") || (s[l] = "5")
                                //0 or 5 digit
                                return false
                        end

                        if s[k] = s[l]
                                //non-unique digit
                                return false
                        end
                end
        end

        return true
end

def testNumber(i)
        global s

        //Tests, if i is divisible by all its digits (0 is not a digit already)
        j = 0
        divisible = true
        for ch in s
                j = int(ch)
                divisible = (i % j) = 0
                if not divisible
                        return false
                end
        end

        return true
end

i = 98764321
isUnique = true
canBeDivided = true

while i > 0
        s = str(i)
        isUnique = uniqueDigits(i)
        if isUnique
                //Number has unique digits
                canBeDivided = testNumber(i)
                if canBeDivided
                        println "Number found: " + i
                        i = 0
                end
        end
        i -= 1
end

  

You may also check:How to resolve the algorithm Pangram checker step by step in the Frink programming language
You may also check:How to resolve the algorithm Stack step by step in the Scala programming language
You may also check:How to resolve the algorithm Inverted index step by step in the Phix programming language
You may also check:How to resolve the algorithm Closest-pair problem step by step in the D programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Processing programming language