Narcissistic decimal number: Difference between revisions
→Reduced search (unordered digit combinations): ( reshaped digitGroups function )
(→Haskell Reduced search (unordered digit combinations): simplified powerSum function) |
(→Reduced search (unordered digit combinations): ( reshaped digitGroups function )) |
||
Line 1,083:
narcissiOfLength :: Int -> [Int]
narcissiOfLength n = powerSum n <$> filter (isDaffodil n) (
where
isDaffodil n ds = (sort . digitList . powerSum n) ds == ds
Line 1,089:
powerSum :: Int -> [Int] -> Int
powerSum n = foldr ((+) . (^ n)) 0
risingDigits 0 ns = ns▼
risingDigits n [] = risingDigits (n - 1) $ (: []) <$> digits▼
risingDigits n ns = risingDigits (n - 1) $ foldMap prependHeadMinus ns▼
where▼
prependHeadMinus xxs@(x:xs) = (: xxs) <$> [0 .. x]▼
digits = [0 .. 9] :: [Int]
Line 1,103 ⟶ 1,097:
main :: IO ()
main = print $ 0 : concat (narcissiOfLength <$> [1 .. 7])
digitGroups :: Int -> [[Int]]
digitGroups nDigits = risingDigits nDigits []
▲ where
▲ risingDigits 0 ns = ns
▲ risingDigits n [] = risingDigits (n - 1) $ (: []) <$> digits
▲ risingDigits n ns = risingDigits (n - 1) $ foldMap prependHeadMinus ns
▲ prependHeadMinus xxs@(x:xs) = (: xxs) <$> [0 .. x]</lang>
{{Out}}
<pre>[0,1,2,3,4,5,6,7,8,9,153,370,371,407,1634,8208,9474,54748,92727,93084,548834,1741725,4210818,9800817,9926315]</pre>
|