Created
January 19, 2023 20:31
-
-
Save stefanroeck/cb811884e277fb5c193e9df07bdb3662 to your computer and use it in GitHub Desktop.
Find all numbers a and b with distinct digits where a-b=42137
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("numbertest", () => { | |
const digitsOf = (n: number): number[] => { | |
const digits = []; | |
const s = n.toString(); | |
for (let i = 0; i < s.length; i++) { | |
const digit = parseInt(s.charAt(i)); | |
digits.push(digit); | |
} | |
return digits; | |
} | |
const containsEachDigitOnce = (digits: number[]): boolean => { | |
return digits.indexOf(0) === -1 && new Set(digits).size === digits.length; | |
} | |
const containEachDigitOnce = (first: number, second: number): boolean => { | |
const joinedDigits = digitsOf(first).concat(digitsOf(second)); | |
return containsEachDigitOnce(joinedDigits); | |
} | |
it("find all numbers with the result where each digit 1-9 only occurrs once", () => { | |
const solutions = []; | |
const substractionResult = 42137; | |
for (let first = substractionResult; first < substractionResult + 10000; first++) { | |
if (containsEachDigitOnce(digitsOf(first))) { | |
for (let second = 1000; second < 9999; second++) { | |
if (containEachDigitOnce(first, second)) { | |
if (first - second === substractionResult) { | |
solutions.push(`${first}-${second}=${substractionResult}`); | |
} | |
} | |
} | |
} | |
} | |
console.log(solutions); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
zu kompliziert :(