McNuggets problem: Difference between revisions

Content added Content deleted
m (→‎{{header|R}}: Syntax highlighting.)
(→‎{{header|R}}: Mostly syntax improvements.)
Line 1,677: Line 1,677:


There are two natural approaches. The first is to generate all valid x, y, and z and then apply the function:
There are two natural approaches. The first is to generate all valid x, y, and z and then apply the function:
<lang rsplus>allInputs<-expand.grid(x=0:(100%/%6),y=0:(100%/%9),z=0:(100%/%20))
<lang rsplus>allInputs <- expand.grid(x = 0:(100 %/% 6), y = 0:(100 %/% 9), z = 0:(100 %/% 20))
mcNuggets<-do.call(function(x,y,z) 6*x + 9*y + 20*z, allInputs)</lang>
mcNuggets <- do.call(function(x, y, z) 6 * x + 9 * y + 20 * z, allInputs)</lang>
The second is to find all of the valid 6x, 9y, and 20z, and then sum them:
The second is to find all of the valid 6x, 9y, and 20z, and then sum them:
<lang rsplus>mcNuggets2<-rowSums(expand.grid(seq(0,100,6),seq(0,100,9),seq(0,100,20)))</lang>
<lang rsplus>mcNuggets2 <- rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20)))</lang>
Either way, we get identical results, as checked by:
Either way, we get identical results, as checked by:
<lang rsplus>all(mcNuggets==mcNuggets2)</lang>
<lang rsplus>all(mcNuggets == mcNuggets2)</lang>
For our final answer, note that our choice to remove values from the vector 0:100 means our outputs will already be sorted, unique, and no greater than 100.
For our final answer, note that our choice to remove values from the vector 0:100 means our outputs will already be sorted, unique, and no greater than 100.
<lang rsplus>results<-setdiff(0:100,mcNuggets)
<lang rsplus>results <- setdiff(0:100, mcNuggets)
cat("The non-McNuggets numbers that are no greater than 100 are:",results,"\nThe largest is",max(results),"\n")</lang>
cat("The non-McNuggets numbers that are no greater than 100 are:", results, "\nThe largest is", max(results), "\n")</lang>
Ultimately, this can be done in one line:
Ultimately, this can be done in one line:
<lang rsplus>max(setdiff(0:100,rowSums(expand.grid(seq(0,100,6),seq(0,100,9),seq(0,100,20)))))</lang>
<lang rsplus>max(setdiff(0:100, rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20)))))</lang>
However, using seq without naming its arguments is considered bad practice. It works here, but breaking this code up is probably a better idea.
{{output}}
{{output}}
<pre>> all(mcNuggets==mcNuggets2)
<pre>> all(mcNuggets == mcNuggets2)
[1] TRUE</pre>
[1] TRUE</pre>
<pre>The non-McNuggets numbers that are no greater than 100 are: 1 2 3 4 5 7 8 10 11 13 14 16 17 19 22 23 25 28 31 34 37 43
<pre>The non-McNuggets numbers that are no greater than 100 are: 1 2 3 4 5 7 8 10 11 13 14 16 17 19 22 23 25 28 31 34 37 43


The largest is 43 </pre>
The largest is 43 </pre>
<pre>> max(setdiff(0:100,rowSums(expand.grid(seq(0,100,6),seq(0,100,9),seq(0,100,20)))))
<pre>> max(setdiff(0:100, rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20)))))
[1] 43</pre>
[1] 43</pre>