Last active
June 22, 2020 14:03
-
-
Save treeform/c657d0ca6ac7338e04d58d362a52858c to your computer and use it in GitHub Desktop.
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
import math | |
func genCache(): array[100, int] = | |
for i in 0 .. 99: | |
let | |
a = i mod 10 | |
b = (i div 10) mod 10 | |
result[i] = int(a ^ a) + int(b ^ b) | |
const | |
MAX = 440_000_000.int | |
cache = genCache() | |
func isMunchausen(number: int): bool = | |
var | |
n = number | |
total = 0 | |
while n > 0: | |
let digit2 = n mod 100 | |
total += cache[digit2] | |
if total > number: | |
return false | |
n = n div 100 | |
total == number | |
proc main() = | |
var output = "" | |
for i in 1 ..< MAX: | |
if (i > 0) and (i mod 1_000_000 == 0): | |
output.add "# " | |
output.addInt i | |
output.add "\n" | |
if isMunchausen(i): | |
output.addInt i | |
output.add "\n" | |
echo output | |
when isMainModule: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment