Narcissistic decimal number: Difference between revisions
→Haskell: -> representing unordered digit combinations as their power sums
m (→JS ES6: Updated the preamble) |
(→Haskell: -> representing unordered digit combinations as their power sums) |
||
Line 1,311:
As summing the nth power of the digits is unaffected by digit order, we can reduce the search space by filtering digit combinations of given length and arbitrary order, rather than filtering a full integer sequence.
In this way we can find the 25th narcissistic number after '''
<lang haskell>import Data.
import Data.
import Control.Arrow (second)
narcissiOfLength :: Int -> [Int]
narcissiOfLength n = filter (isDaffodil n) (digitPowerSums n)
isDaffodil :: Int -> Int -> Bool
isDaffodil e n = length ds == e && powerSum e ds == n
where
ds = digitList n
powerSum :: Int -> [Int] -> Int
Line 1,332 ⟶ 1,336:
else Nothing)
where
treeGrowth n pps =
if n > 0
then
(n - 1)
(if null
then
else
(second (ps +) <$> take (pd + 1) nthPowers))
pps)
else pps
main :: IO ()
--main = print $
main = print $ 0 : concatMap narcissiOfLength [1 .. 7]</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>
|