Skip to content

Instantly share code, notes, and snippets.

@benkamphaus
Created November 17, 2016 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benkamphaus/f933d23aea0e5c2b4674ab8e111887c6 to your computer and use it in GitHub Desktop.
Save benkamphaus/f933d23aea0e5c2b4674ab8e111887c6 to your computer and use it in GitHub Desktop.
Find narcissistic numbers in Clojure (non-optimized)
(defn narcissistic?
"Returns true if n with m digits is equal to each digit in n to the
m exponent."
[n]
(let [digits (map #(Character/digit % 10) (str n))
n-digits (count digits)
exps (map #(Math/pow % n-digits) digits)]
(== n (apply + exps))))
(take 10 (filter narcissistic? (iterate inc 10)))
;; (153 370 371 407 1634 8208 9474 54748 92727 93084)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment