Anonymous user
Increasing gaps between consecutive Niven numbers: Difference between revisions
Increasing gaps between consecutive Niven numbers (view source)
Revision as of 19:28, 25 April 2020
, 4 years agono edit summary
No edit summary |
|||
Line 215:
276 1,039,028,518 18,879,988,824
</pre>
=={{header|Haskell}}==
<lang haskell>{-# LANGUAGE NumericUnderscores #-}
import Control.Monad (guard)
import Text.Printf (printf)
import Data.List (intercalate)
import Data.List.Split (chunksOf)
nivens :: [Int]
nivens = do
n <- [1..]
guard (n `rem` digitSum n == 0)
pure n
where
digitSum = sum . map (read . pure) . show
findGaps :: [(Int, Int, Int)]
findGaps = go (zip [1..] nivens) 0
where
go [] n = []
go r@((c, currentNiven):(_, nextNiven):xs) lastGap
| gap > lastGap = (gap, c, currentNiven) : go (tail r) gap
| otherwise = go (tail r) lastGap
where
gap = nextNiven - currentNiven
go (x:xs) _ = []
thousands :: Int -> String
thousands = reverse . intercalate "," . chunksOf 3 . reverse . show
main :: IO ()
main = do
printf row "Gap" "Index of Gap" "Starting Niven"
mapM_ (\(gap, gapIndex, niven) -> printf row (show gap) (thousands gapIndex) (thousands niven))
$ takeWhile (\(_, gapIndex, _) -> gapIndex < 10_000_000) findGaps
where
row = "%5s%15s%15s\n"</lang>
{{out}}
<pre>
Gap Index of Gap Starting Niven
1 1 1
2 10 10
6 11 12
7 26 63
8 28 72
10 32 90
12 83 288
14 102 378
18 143 558
23 561 2,889
32 716 3,784
36 1,118 6,480
44 2,948 19,872
45 4,194 28,971
54 5,439 38,772
60 33,494 297,864
66 51,544 478,764
72 61,588 589,860
88 94,748 989,867
90 265,336 2,879,865
99 800,054 9,898,956
108 3,750,017 49,989,744
126 6,292,149 88,996,914
</pre>
=={{header|J}}==
<lang J>
|