Created
June 22, 2020 13:41
-
-
Save treeform/63ab865d3ad8812604c1d3b024186e61 to your computer and use it in GitHub Desktop.
This file contains hidden or 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[10, int32] = | |
| for i in 1 .. 9: | |
| result[i] = int32(i ^ i) | |
| const | |
| MAX = 440_000_000.int32 | |
| cache = genCache() | |
| func isMunchausen(number: int32): bool = | |
| var | |
| n = number | |
| total = 0 | |
| while n > 0: | |
| let digit = n mod 10 | |
| total += cache[digit] | |
| if total > number: | |
| return false | |
| n = n div 10 | |
| 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