How to resolve the algorithm Largest number divisible by its digits step by step in the Go programming language
How to resolve the algorithm Largest number divisible by its digits step by step in the Go 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 Go programming language
The provided Go code contains two variations of a program that finds the largest decimal and hexadecimal numbers divisible by each of their unique digits. Here's a detailed explanation of both versions:
Decimal Version (First Code Block):
Function divByAll:
- It takes an integer
num
and a slice of bytesdigits
representing the unique digits ofnum
. - It iterates through the
digits
slice, converting each byte to an integer (the actual digit) usingint(digit-'0')
. - It checks if
num
is divisible by each of the digits. If it's not divisible by any digit, it returns false. Otherwise, it returns true.
Main Function:
- It calculates
magic
, which is the product of integers from 9 down to 7. This value is used to find numbers divisible by these digits. - It calculates
high
, which is the largest number divisible bymagic
that fits within a 32-bit integer. - It starts a loop from
high
down tomagic
. - For each number
i
, it checks the following conditions to filter out numbers that don't meet the criteria:- Ends with '0' (because it requires distinct digits).
- Contains '0' or '5'.
- Doesn't contain unique digits (checks if the length of the converted string representation of
i
is the same as the number of unique digits).
- If all conditions are met, it checks if
i
is divisible by all its unique digits using thedivByAll
function. - If
divByAll
returns true, it printsi
as the largest decimal number that meets the criteria and returns.
Hexadecimal Version (Second Code Block):
Function divByAll:
- This version is similar to the decimal version, but it handles hexadecimal digits.
- It takes an integer
num
and a slice of bytesdigits
. - It iterates through the
digits
slice, converting each byte to an integer usingint64(digit-'0')
orint64(digit-'W')
depending on whether it's a number or a letter. - It checks if
num
is divisible by each of the digits. If it's not divisible by any digit, it returns false. Otherwise, it returns true.
Main Function:
- It calculates
magic
, which is the product of integers from 15 down to 11. This value is used to find numbers divisible by these digits. - It calculates
high
, which is the largest number divisible bymagic
that fits within a 64-bit integer. - It starts a loop from
high
down tomagic
. - For each number
i
, it checks the following conditions:- Ends with '0' (because it requires distinct digits).
- Contains '0'.
- Doesn't contain unique digits.
- It converts
i
to a hexadecimal string usingstrconv.FormatInt
and ensures it's in lowercase. - It checks if
i
is divisible by all its unique digits using thedivByAll
function. - If
divByAll
returns true, it printsi
(in hexadecimal format) as the largest hexadecimal number that meets the criteria and returns.
Source code in the go programming language
package main
import (
"fmt"
"strconv"
"strings"
)
func divByAll(num int, digits []byte) bool {
for _, digit := range digits {
if num%int(digit-'0') != 0 {
return false
}
}
return true
}
func main() {
magic := 9 * 8 * 7
high := 9876432 / magic * magic
for i := high; i >= magic; i -= magic {
if i%10 == 0 {
continue // can't end in '0'
}
s := strconv.Itoa(i)
if strings.ContainsAny(s, "05") {
continue // can't contain '0'or '5'
}
var set = make(map[byte]bool)
var sd []byte // distinct digits
for _, b := range []byte(s) {
if !set[b] {
set[b] = true
sd = append(sd, b)
}
}
if len(sd) != len(s) {
continue // digits must be unique
}
if divByAll(i, sd) {
fmt.Println("Largest decimal number is", i)
return
}
}
}
package main
import (
"fmt"
"strconv"
"strings"
)
func divByAll(num int64, digits []byte) bool {
for _, digit := range digits {
var d int64
if digit <= '9' {
d = int64(digit - '0')
} else {
d = int64(digit - 'W')
}
if num%d != 0 {
return false
}
}
return true
}
func main() {
var magic int64 = 15 * 14 * 13 * 12 * 11
high := 0xfedcba987654321 / magic * magic
for i := high; i >= magic; i -= magic {
if i%16 == 0 {
continue // can't end in '0'
}
s := strconv.FormatInt(i, 16) // always generates lower case a-f
if strings.IndexByte(s, '0') >= 0 {
continue // can't contain '0'
}
var set = make(map[byte]bool)
var sd []byte // distinct digits
for _, b := range []byte(s) {
if !set[b] {
set[b] = true
sd = append(sd, b)
}
}
if len(sd) != len(s) {
continue // digits must be unique
}
if divByAll(i, sd) {
fmt.Printf("Largest hex number is %x\n", i)
return
}
}
}
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the Python programming language
You may also check:How to resolve the algorithm Water collected between towers step by step in the D programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Unicon programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Forth programming language
You may also check:How to resolve the algorithm String case step by step in the Scheme programming language