Skip to content

Instantly share code, notes, and snippets.

@treeform
Created June 22, 2020 13:41
Show Gist options
  • Save treeform/63ab865d3ad8812604c1d3b024186e61 to your computer and use it in GitHub Desktop.
Save treeform/63ab865d3ad8812604c1d3b024186e61 to your computer and use it in GitHub Desktop.
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