Colorful numbers: Difference between revisions
Content added Content deleted
(In) |
|||
Line 395: | Line 395: | ||
<pre>[0,1,2,3,4,5,6,7,8,9,10,23,24,25,26,27,28,29,32,34,35,36,37,38,39,42,43,45,46,47,48,49,52,53,54,56,57,58,59,62,63,64,65,67,68,69,72,73,74,75,76,78,79,82,83,84,85,86,87,89,92,93,94,95,96,97,98](solution1) |
<pre>[0,1,2,3,4,5,6,7,8,9,10,23,24,25,26,27,28,29,32,34,35,36,37,38,39,42,43,45,46,47,48,49,52,53,54,56,57,58,59,62,63,64,65,67,68,69,72,73,74,75,76,78,79,82,83,84,85,86,87,89,92,93,94,95,96,97,98](solution1) |
||
98765432(solution2)</pre> |
98765432(solution2)</pre> |
||
=={{header|Haskell (alternate version)}}== |
|||
<lang haskell>import Data.List (inits, nub, tails, unfoldr) |
|||
-- Non-empty subsequences containing only consecutive elements from the |
|||
-- argument. For example: |
|||
-- |
|||
-- consecs [1,2,3] => [[1],[1,2],[1,2,3],[2],[2,3],[3]] |
|||
consecs :: [a] -> [[a]] |
|||
consecs = drop 1 . ([] :) . concatMap (drop 1 . inits) . tails |
|||
-- The list of digits in the argument, from least to most significant. The |
|||
-- number 0 is represented by the empty list. |
|||
toDigits :: Int -> [Int] |
|||
toDigits = unfoldr step |
|||
where step 0 = Nothing |
|||
step n = let (q, r) = n `quotRem` 10 in Just (r, q) |
|||
-- True if and only if all the argument's elements are distinct. |
|||
allDistinct :: [Int] -> Bool |
|||
allDistinct ns = length ns == length (nub ns) |
|||
-- True if and only if the argument is a colorful number. |
|||
isColorful :: Int -> Bool |
|||
isColorful = allDistinct . map product . consecs . toDigits |
|||
main :: IO () |
|||
main = do |
|||
let smalls = filter isColorful [0..99] |
|||
putStrLn $ "Small colorful numbers: " ++ show smalls |
|||
let start = 98765432 |
|||
largest = head $ dropWhile (not . isColorful) [start, start-1 ..] |
|||
putStrLn $ "Largest colorful number: " ++ show largest</lang> |
|||
{{out}} |
|||
<pre> |
|||
$ colorful |
|||
Small colorful numbers: [0,1,2,3,4,5,6,7,8,9,23,24,25,26,27,28,29,32,34,35,36,37,38,39,42,43,45,46,47,48,49,52,53,54,56,57,58,59,62,63,64,65,67,68,69,72,73,74,75,76,78,79,82,83,84,85,86,87,89,92,93,94,95,96,97,98] |
|||
Largest colorful number: 98746253 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |