Narcissistic decimal number: Difference between revisions

Content added Content deleted
Line 1,083: Line 1,083:


narcissiOfLength :: Int -> [Int]
narcissiOfLength :: Int -> [Int]
narcissiOfLength n = powerSum 0 n <$> filter (isDaffodil n) (risingDigits n [])
narcissiOfLength n = powerSum n <$> filter (isDaffodil n) (risingDigits n [])
where
where
isDaffodil n ds = (sort . digitList . powerSum 0 n) ds == ds
isDaffodil n ds = (sort . digitList . powerSum n) ds == ds


powerSum :: Int -> Int -> [Int] -> Int
powerSum :: Int -> [Int] -> Int
powerSum a _ [] = a
powerSum n = foldr ((+) . (^ n)) 0
powerSum a n (x:xs) = powerSum (a + x ^ n) n xs


risingDigits 0 ns = ns
risingDigits 0 ns = ns