Narcissistic decimal number: Difference between revisions

→‎Haskell - Reduced search: (used unfoldr for digitList)
(→‎Haskell - Reduced search: (used unfoldr for digitList))
Line 1,313:
In this way we can find the 25th narcissistic number after '''(sum $ (length . digitGroups) <$> [1..7]) == 19447''' tests – an improvement on the exhaustive trawl through '''9926315''' integers.
 
<lang haskell>import Data.List (sort, unfoldr)
 
narcissiOfLength :: Int -> [Int]
Line 1,324:
 
digitList :: Int -> [Int]
digitList 0 = []
unfoldr
digitList n = rem n 10 : digitList (quot n 10)
(\x ->
if x > 0
then let (q, r) = quotRem x 10
in Just (r, q)
else Nothing)
 
digitGroups :: Int -> [[Int]]
9,659

edits