Skip to content

Instantly share code, notes, and snippets.

@Moligaloo
Last active August 29, 2015 13:56
Show Gist options
  • Save Moligaloo/9267261 to your computer and use it in GitHub Desktop.
Save Moligaloo/9267261 to your computer and use it in GitHub Desktop.
Check a number is a narcissistic number or not, and print the narcissistic number in [0,1000] the detailed definition of a narcissistic number is at http://zh.wikipedia.org/wiki/%E6%B0%B4%E4%BB%99%E8%8A%B1%E6%95%B0
#!/usr/bin/env io
Number isNarcissistic := method(base,
base ifNil(base = 10)
digits := self toBase(base) asList map(fromBase(base))
self == digits map(** digits size) sum
)
# print the narcissistic number in range [0, 1000] (decimal)
Range 0 to(1000) asList select(isNarcissistic) println
# => list(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment