In today’s Programming Praxis exercise, our goal is to determine all the numbers that are not McNugget numbers, i.e. numbers that cannot be created by summing multiples of 6, 9 and 20. Let’s get started, shall we?

A quick import:

import Data.List

The code is pretty straightforward: just take all the numbers up to 180 that cannot be created by a linear combination of 6, 9 and 20.

notMcNuggets :: [Integer]
notMcNuggets = [1..180] \\
[a+b+c | a <- [0,6..180], b <- [0,9..180-a], c <- [0,20..180-a-b]]

To test whether everything works correctly:

main :: IO ()
main = print notMcNuggets

Yup. Nice and simple.

### Like this:

Like Loading...

*Related*

Tags: bonsai, code, Haskell, kata, mcnugget, numbers, praxis, programming

This entry was posted on December 9, 2011 at 1:24 pm and is filed under Programming Praxis. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

January 24, 2012 at 8:56 pm |

Here is an alternate implementation where [6,9,20] can be replaced with any list of integers:

isSumOf::[Integer]->Integer->Bool

isSumOf (x:[]) y = (mod y x) == 0

isSumOf (x:xs) y = (any $ isSumOf xs) [y,y-x..0]

mcNugget = isSumOf [6,9,20]

*Main Data.List> filter (not . mcNugget) [1..10000]

[1,2,3,4,5,7,8,10,11,13,14,16,17,19,22,23,25,28,31,34,37,43]

*Main Data.List> isSumOf [3852,1171,3257] 91873

False

*Main Data.List> isSumOf [3852,1171,3257] 91874

True