Last active
August 29, 2015 13:56
-
-
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
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
#!/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