How to resolve the algorithm Largest number divisible by its digits step by step in the C# programming language
How to resolve the algorithm Largest number divisible by its digits step by step in the C# 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 C# programming language
The first code uses a Greatest Common Divisor (gcd)
method to find the greatest common divisor of two numbers, and a Least Common Multiple (lcmd)
method using an iterative approach to find the least common multiple of the digits of a number in a given base. It then finds the largest number with seven distinct digits that is divisible by all its digits, excluding zero, in base 10 and base 16. It demonstrates the use of Stopwatch
to measure the execution time and prints the results.
The second code provides two ChkDigs
and ChkHDigs
methods to check if a number has distinct digits and is divisible by each digit in base 10 and hexadecimal, respectively. It then uses the decline
method to generate a sequence of numbers within a range with a given step and filters the sequence based on the ChkDigs
or ChkHDigs
criteria to find the largest qualifying number. It also uses Stopwatch
to measure the execution time and prints the results for base 10 and base 16.
Source code in the csharp programming language
using System;
class Program {
static int gcd(int a, int b) { return b > 0 ? gcd(b, a % b) : a; }
// returns least common multiple of digits of x in base b
static int lcmd(long x, int b) {
int r = (int)(x % b), a; x /= b; while (x > 0) {
r = (r * (a = (int)(x % b))) / gcd(r, a); x /= b; } return r; }
static void Main(string[] args) {
var sw = System.Diagnostics.Stopwatch.StartNew();
long mx = 987654321; // all digits except zero
mx = 98764321; // omitting 5 because even numbers are lost
mx /= 10; // 7 digs because 8 digs won't divide by 3
long skip = lcmd(mx, 10), i; bool nDup;
for (i = mx - mx % skip; ; i -= skip) {
var s = i.ToString().ToCharArray(); Array.Sort(s);
if (s[0] == '0') continue; // no zeros
nDup = true; // checking for duplicate digits or digit five
for (int j = 0, k = 1; k < s.Length; j = k++)
if (s[j] == s[k] || s[k] == '5') { nDup = false; break; }
if (nDup) break; } sw.Stop(); // found it
Console.Write("base 10 = {0} in {1} μs\n", i,
1000 * sw.Elapsed.TotalMilliseconds);
sw.Restart();
mx = 0xfedcba987654321; // all 15 digits used, no zero
skip = lcmd(mx >> 4, 16); // digit one implied, so omit it
for (i = mx - mx % skip; ; i -= skip) {
var s = i.ToString("x").ToCharArray(); Array.Sort(s);
if (s[0] == '0') continue; // no zeros
nDup = true; // checking for duplicate digits
for (int j = 0, k = 1; k < s.Length; j = k++)
if (s[j] == s[k]) { nDup = false; break; }
if (nDup) break; } sw.Stop(); // found it
Console.Write("base 16 = {0} in {1} ms", i.ToString("x"),
sw.Elapsed.TotalMilliseconds); } }
using System;
using System.Linq;
using System.Collections.Generic;
class Program {
static IEnumerable<long> decline(long min, long max, long stp) {
long lmt = (min / stp) * stp;
for (long i = (max / stp) * stp; i >= lmt; i -= stp)
yield return i;
}
static bool ChkDigs(long number) {
var set = new HashSet<char>();
return number
.ToString()
.All(d => d > '0'
&& number % (d - '0') == 0
&& set.Add(d));
}
static bool ChkHDigs(long number) {
const string hDigs = "0123456789abcdef";
var set = new HashSet<char>();
return number
.ToString("x")
.All(d => d > '0'
&& number % hDigs.IndexOf(d) == 0
&& set.Add(d));
}
static void Main() {
var sw = System.Diagnostics.Stopwatch.StartNew();
long min = 1236798, // lowest possible seven digit number
max = 9876312, // high limit
stp = 9 * 8 * 7, // skip numbers without this factor
result = decline(min, max, stp)
.Where(ChkDigs)
.First();
sw.Stop();
Console.Write("Base 10 = {0} in {1} ms", result,
sw.Elapsed.TotalMilliseconds);
sw.Restart();
min = 0x123456789abcdef; // lowest possible 15 digit number
max = 0xfedcba987654321; // high limit
stp = 15*14*13*12*11; // skip numbers without this factor
result = decline(min, max, stp)
.Where(ChkHDigs)
.First();
sw.Stop();
Console.Write("\nBase 16 = {0} in {1} sec",
result.ToString("x"), sw.Elapsed.TotalSeconds);
}
}
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Lang programming language
You may also check:How to resolve the algorithm Abstract type step by step in the jq programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the C# programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Raku programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Erlang programming language