Humble numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Redefined humbles in terms of Data.Set)
Line 658: Line 658:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Numbers.Primes (primeFactors)
<lang haskell>import Data.Set (deleteFindMin, fromList, union)
import Control.Arrow ((&&&))
import Control.Arrow ((&&&))
import Data.List (group)
import Data.List (group)
import Data.Bool (bool)
import Data.Bool (bool)



----------------------HUMBLE NUMBERS-----------------------
----------------------HUMBLE NUMBERS-----------------------

humbles :: [Integer]
humbles :: [Integer]
humbles =
humbles = filter (all (< 8) . primeFactors) [1 ..]
let go sofar = x : go (union pruned $ fromList ((x *) <$> [2, 3, 5, 7]))
where
(x, pruned) = deleteFindMin sofar
in go $ fromList [1]


-- humbles = filter (all (< 8) . primeFactors) [1 ..]
---------------------------TEST----------------------------
---------------------------TEST----------------------------
main :: IO ()
main :: IO ()
Line 675: Line 678:
mapM_ (putStrLn . concat) $
mapM_ (putStrLn . concat) $
chunksOf 10 $ (justifyRight 4 ' ' . show) <$> take 50 humbles
chunksOf 10 $ (justifyRight 4 ' ' . show) <$> take 50 humbles
putStrLn "\nCount of humble numbers for each digit length 1-6:"
putStrLn "\nCount of humble numbers for each digit length 1-20:"
mapM_ print $ take 6 $ (head &&& length) <$> group (digitCount <$> humbles)
mapM_ print $ take 20 $ (head &&& length) <$> group (digitCount <$> humbles)


digitCount :: Integer -> Int
digitCount :: Integer -> Int
Line 703: Line 706:
81 84 90 96 98 100 105 108 112 120
81 84 90 96 98 100 105 108 112 120


Count of humble numbers for each digit length 1-6:
Count of humble numbers for each digit length 1-20:
(1,9)
(1,9)
(2,36)
(2,36)
Line 709: Line 712:
(4,197)
(4,197)
(5,356)
(5,356)
(6,579)</pre>
(6,579)
(7,882)

(8,1272)
(9,1767)
(10,2381)
(11,3113)
(12,3984)
(13,5002)
(14,6187)
(15,7545)
(16,9081)
(17,10815)
(18,12759)
(19,14927)
(20,17323)</pre>


=={{header|Julia}}==
=={{header|Julia}}==