Narcissistic decimal number: Difference between revisions

Content added Content deleted
(→‎Haskell - Reduced search: (used unfoldr for digitList))
Line 1,313: 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.
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)
<lang haskell>import Data.List (sort, unfoldr)


narcissiOfLength :: Int -> [Int]
narcissiOfLength :: Int -> [Int]
Line 1,324: Line 1,324:


digitList :: Int -> [Int]
digitList :: Int -> [Int]
digitList 0 = []
digitList =
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]]
digitGroups :: Int -> [[Int]]