McNuggets problem: Difference between revisions
ReeceGoding (talk | contribs) (New R solution.) |
Not a robot (talk | contribs) (Add Refal) |
||
(64 intermediate revisions by 27 users not shown) | |||
Line 13: | Line 13: | ||
number (a number ''n'' which cannot be expressed with ''6x + 9y + 20z = n'' |
number (a number ''n'' which cannot be expressed with ''6x + 9y + 20z = n'' |
||
where ''x'', ''y'' and ''z'' are natural numbers). |
where ''x'', ''y'' and ''z'' are natural numbers). |
||
<br><br> |
|||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">V nuggets = Set(0..100) |
|||
L(s, n, t) cart_product(0 .. 100 I/ 6, |
|||
0 .. 100 I/ 9, |
|||
0 .. 100 I/ 20) |
|||
nuggets.discard(6*s + 9*n + 20*t) |
|||
print(max(nuggets))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
43 |
|||
</pre> |
|||
=={{header|8080 Assembly}}== |
|||
<syntaxhighlight lang="asm"> org 100h |
|||
lxi h,200h ; Zero out a page to keep nugget flags |
|||
xra a |
|||
znugs: mov m,a |
|||
inr l |
|||
jnz znugs |
|||
lxi b,101 ; B = 6 stepper, C = 101 (limit) |
|||
loopa: mov d,b ; D = 9 stepper |
|||
loopb: mov l,d ; L = 20 stepper |
|||
loopc: inr m ; Mark nugget |
|||
mvi a,20 ; 20 step |
|||
add l |
|||
mov l,a |
|||
cmp c |
|||
jc loopc |
|||
mvi a,9 ; 9 step |
|||
add d |
|||
mov d,a |
|||
cmp c |
|||
jc loopb |
|||
mvi a,6 ; 6 step |
|||
add b |
|||
mov b,a |
|||
cmp c |
|||
jc loopa |
|||
mov l,c ; Find largest number not seen |
|||
scan: dcr l |
|||
dcr m |
|||
jp scan |
|||
mov a,l |
|||
mvi b,'0'-1 ; B = high digit |
|||
digit: inr b |
|||
sui 10 |
|||
jnc digit |
|||
adi '0'+10 ; A = low digit |
|||
lxi h,digits+1 |
|||
mov m,a ; Store digits |
|||
dcx h |
|||
mov m,b |
|||
xchg |
|||
mvi c,9 ; CP/M print string |
|||
jmp 5 |
|||
digits: db 0,0,'$' ; Placeholder for output</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|ABC}}== |
|||
<syntaxhighlight lang="abc">PUT {1..100} IN non.nuggets |
|||
PUT 0 IN a |
|||
WHILE a <= 100: |
|||
PUT a IN b |
|||
WHILE b <= 100: |
|||
PUT b IN c |
|||
WHILE c <= 100: |
|||
IF c in non.nuggets: |
|||
REMOVE c FROM non.nuggets |
|||
PUT c+20 IN c |
|||
PUT b+9 IN b |
|||
PUT a+6 IN a |
|||
WRITE "Maximum non-McNuggets number:", max non.nuggets/</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43</pre> |
|||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!">PROC Main() |
|||
BYTE x,y,z,n |
|||
BYTE ARRAY nuggets(101) |
|||
FOR n=0 TO 100 |
|||
DO |
|||
nuggets(n)=0 |
|||
OD |
|||
FOR x=0 TO 100 STEP 6 |
|||
DO |
|||
FOR y=0 TO 100 STEP 9 |
|||
DO |
|||
FOR z=0 TO 100 STEP 20 |
|||
DO |
|||
n=x+y+z |
|||
IF n<=100 THEN |
|||
nuggets(n)=1 |
|||
FI |
|||
OD |
|||
OD |
|||
OD |
|||
n=100 |
|||
DO |
|||
IF nuggets(n)=0 THEN |
|||
PrintF("The largest non McNugget number is %B%E",n) |
|||
EXIT |
|||
ELSEIF n=0 THEN |
|||
PrintE("There is no result") |
|||
EXIT |
|||
ELSE |
|||
n==-1 |
|||
FI |
|||
OD |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/McNuggets_problem.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
The largest non McNugget number is 43 |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure McNugget is |
procedure McNugget is |
||
Line 38: | Line 162: | ||
end if; |
end if; |
||
end loop; |
end loop; |
||
end McNugget;</ |
end McNugget;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 45: | Line 169: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# Solve the McNuggets problem: find the largest n <= 100 for which there # |
# Solve the McNuggets problem: find the largest n <= 100 for which there # |
||
# are no non-negative integers x, y, z such that 6x + 9y + 20z = n # |
# are no non-negative integers x, y, z such that 6x + 9y + 20z = n # |
||
Line 52: | Line 176: | ||
FOR i FROM LWB sum TO UPB sum DO sum[ i ] := FALSE OD; |
FOR i FROM LWB sum TO UPB sum DO sum[ i ] := FALSE OD; |
||
FOR x FROM 0 BY 6 TO max nuggets DO |
FOR x FROM 0 BY 6 TO max nuggets DO |
||
FOR y FROM |
FOR y FROM x BY 9 TO max nuggets DO |
||
FOR z FROM |
FOR z FROM y BY 20 TO max nuggets DO |
||
sum[ z ] := TRUE |
|||
IF nuggets <= max nuggets THEN sum[ nuggets ] := TRUE FI |
|||
OD # z # |
OD # z # |
||
OD # y # |
OD # y # |
||
Line 61: | Line 184: | ||
# show the highest number that cannot be formed # |
# show the highest number that cannot be formed # |
||
INT largest := -1; |
INT largest := -1; |
||
FOR i FROM UPB sum BY -1 TO LWB sum WHILE largest := |
FOR i FROM UPB sum BY -1 TO LWB sum WHILE sum[ largest := i ] DO SKIP OD; |
||
print( ( "The largest non McNugget number is: " |
print( ( "The largest non McNugget number is: " |
||
, whole( largest, 0 ) |
, whole( largest, 0 ) |
||
Line 67: | Line 190: | ||
) |
) |
||
) |
) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
The largest non McNugget number is: 43 |
The largest non McNugget number is: 43 |
||
</pre> |
</pre> |
||
=={{header|APL}}== |
|||
{{works with|Dyalog APL}} |
|||
<syntaxhighlight lang="apl">100 (⌈/(⍳⊣)~(⊂⊢)(+/×)¨(,⎕IO-⍨(⍳∘⌊÷))) 6 9 20</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Generalised for other set sizes, and for other triples of natural numbers. |
Generalised for other set sizes, and for other triples of natural numbers. |
||
Uses NSMutableSet, through the AppleScript ObjC interface: |
Uses NSMutableSet, through the AppleScript ObjC interface: |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" |
||
use framework "Foundation" |
use framework "Foundation" |
||
use scripting additions |
use scripting additions |
||
Line 252: | Line 381: | ||
on setMember(x, objcSet) |
on setMember(x, objcSet) |
||
missing value is not (objcSet's member:(x)) |
missing value is not (objcSet's member:(x)) |
||
end setMember</ |
end setMember</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>43</pre> |
<pre>43</pre> |
||
=={{header|Arturo}}== |
|||
{{trans|Ruby}} |
|||
<syntaxhighlight lang="rebol">nonMcNuggets: function [lim][ |
|||
result: new 0..lim |
|||
loop range.step:6 1 lim 'x [ |
|||
loop range.step:9 1 lim 'y [ |
|||
loop range.step:20 1 lim 'z |
|||
-> 'result -- sum @[x y z] |
|||
] |
|||
] |
|||
return result |
|||
] |
|||
print max nonMcNuggets 100</syntaxhighlight> |
|||
{{out}} |
|||
<pre>46</pre> |
|||
=={{header|Asymptote}}== |
|||
<syntaxhighlight lang="Asymptote">bool[] n; |
|||
for(int i = 0; i <= 100; ++i) { n[i] = false; } |
|||
int k; |
|||
for (int a = 0; a < 100/6; ++a) { |
|||
for (int b = 0; b < 100/9; ++b) { |
|||
for (int c = 0; c < 100/20; ++c) { |
|||
k = a*6 + b*9 + c*20; |
|||
if (k <= 100) { n[k] = true; } |
|||
} |
|||
} |
|||
} |
|||
for (int k = 100; k >= 0; --k) { |
|||
if (n[k] != true) { |
|||
write("Maximum non-McNuggets number is: ", k); |
|||
break; |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f MCNUGGETS_PROBLEM.AWK |
# syntax: GAWK -f MCNUGGETS_PROBLEM.AWK |
||
# converted from Go |
# converted from Go |
||
Line 277: | Line 450: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
43 |
43 |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
|||
<syntaxhighlight lang="basic">10 DEFINT A-Z: DIM F(100) |
|||
20 FOR A=0 TO 100 STEP 6 |
|||
30 FOR B=A TO 100 STEP 9 |
|||
40 FOR C=B TO 100 STEP 20 |
|||
50 F(C)=-1 |
|||
60 NEXT C,B,A |
|||
70 FOR A=100 TO 0 STEP -1 |
|||
80 IF NOT F(A) THEN PRINT A: END |
|||
90 NEXT A</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 43</pre> |
|||
==={{header|Applesoft BASIC}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
<syntaxhighlight lang="qbasic">100 dim nuggets(100) |
|||
110 for six = 0 to 100/6 |
|||
120 for nine = 0 to 100/9 |
|||
130 for twenty = 0 to 100/20 |
|||
140 n = six*6+nine*9+twenty*20 |
|||
150 if n <= 100 then nuggets(n) = 1 |
|||
160 next twenty |
|||
170 next nine |
|||
180 next six |
|||
190 for n = 100 to 1 step -1 |
|||
200 if nuggets(n) <> 1 then print "Maximum non-McNuggets number is: ";n : goto 250 |
|||
240 next n |
|||
250 end</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|BASIC256}}=== |
|||
<syntaxhighlight lang="vb">arraybase 1 |
|||
dim nuggets(100) |
|||
for six = 0 To 100/6 |
|||
for nine = 0 To 100/9 |
|||
for twenty = 0 To 100/20 |
|||
n = six*6 + nine*9 + twenty*20 |
|||
if n <= 100 then nuggets[n] = true |
|||
next twenty |
|||
next nine |
|||
next six |
|||
for n = 100 to 1 step -1 |
|||
if nuggets[n] = false then |
|||
print "Maximum non-McNuggets number is: "; n |
|||
exit for |
|||
end if |
|||
next n</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|Chipmunk Basic}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
<syntaxhighlight lang="qbasic">100 dim nuggets(100) |
|||
110 for six = 0 to 100/6 |
|||
120 for nine = 0 to 100/9 |
|||
130 for twenty = 0 to 100/20 |
|||
140 n = six*6+nine*9+twenty*20 |
|||
150 if n <= 100 then nuggets(n) = 1 |
|||
160 next twenty |
|||
170 next nine |
|||
180 next six |
|||
190 for n = 100 to 1 step -1 |
|||
200 if nuggets(n) <> 1 then |
|||
210 print "Maximum non-McNuggets number is: ";n |
|||
220 end |
|||
230 endif |
|||
240 next n |
|||
250 end</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|Gambas}}=== |
|||
<syntaxhighlight lang="vbnet">Public l[101] As Integer |
|||
Public Sub Main() |
|||
Dim a As Integer, b As Integer, c As Integer, n As Integer |
|||
For a = 0 To 100 / 6 |
|||
For b = 0 To 100 / 9 |
|||
For c = 0 To 100 / 20 |
|||
n = a * 6 + b * 9 + c * 20 |
|||
If n <= 100 Then l[n] = True |
|||
Next |
|||
Next |
|||
Next |
|||
For n = 100 To 1 Step -1 |
|||
If Not l[n] Then |
|||
Print "Maximum non-McNuggets number is: "; n |
|||
Break |
|||
End If |
|||
Next |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|GW-BASIC}}=== |
|||
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes. |
|||
==={{header|Minimal BASIC}}=== |
|||
{{works with|QBasic}} |
|||
{{works with|QuickBasic}} |
|||
{{works with|Applesoft BASIC}} |
|||
{{works with|BASICA}} |
|||
{{works with|Chipmunk Basic}} |
|||
{{works with|GW-BASIC}} |
|||
{{works with|MSX BASIC}} |
|||
{{works with|Quite BASIC}} |
|||
<syntaxhighlight lang="qbasic">10 DIM N(100) : rem 10 ARRAY N for Quite BASIC |
|||
20 FOR A = 0 TO 100/6 |
|||
30 FOR B = 0 TO 100/9 |
|||
40 FOR C = 0 TO 100/20 |
|||
50 LET K = A*6+B*9+C*20 |
|||
60 IF K <= 100 THEN 80 |
|||
70 GOTO 90 |
|||
80 LET N(K) = 1 |
|||
90 NEXT C |
|||
100 NEXT B |
|||
110 NEXT A |
|||
120 FOR K = 100 TO 1 STEP -1 |
|||
130 IF N(K) <> 1 THEN 160 |
|||
140 NEXT K |
|||
150 STOP |
|||
160 PRINT "Maximum non-McNuggets number is: "; K |
|||
170 END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|MSX Basic}}=== |
|||
{{works with|MSX BASIC|any}} |
|||
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes. |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">OpenConsole() |
|||
Define n.i |
|||
Dim nuggets.i(100) |
|||
For six.i = 0 To 100/6 |
|||
For nine.i = 0 To 100/9 |
|||
For twenty.i = 0 To 100/20 |
|||
n = six*6 + nine*9 + twenty*20 |
|||
If n <= 100 |
|||
nuggets(n) = #True |
|||
EndIf |
|||
Next twenty |
|||
Next nine |
|||
Next six |
|||
For n = 100 To 1 Step -1 |
|||
If nuggets(n) = #False |
|||
PrintN("Maximum non-McNuggets number is: " + Str(n)) |
|||
Break |
|||
EndIf |
|||
Next n |
|||
PrintN(#CRLF$ + "Press ENTER to exit"): Input() |
|||
CloseConsole()</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|Quite BASIC}}=== |
|||
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes. |
|||
==={{header|Run BASIC}}=== |
|||
{{works with|Just BASIC}} |
|||
{{works with|Liberty BASIC}} |
|||
<syntaxhighlight lang="vb">dim nuggets(100) |
|||
for six = 0 to 100/6 |
|||
for nine = 0 to 100/9 |
|||
for twenty = 0 to 100/20 |
|||
n = six*6 + nine*9 + twenty*20 |
|||
if n <= 100 then nuggets(n) = 1 |
|||
next twenty |
|||
next nine |
|||
next six |
|||
for n = 100 to 1 step -1 |
|||
if nuggets(n) <> 1 then |
|||
print "Maximum non-McNuggets number is: "; n |
|||
end |
|||
end if |
|||
next n</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">OPTION BASE 0 |
|||
DIM nuggets(100) |
|||
FOR n = 0 TO 100 |
|||
LET nuggets(n) = 0 |
|||
NEXT n |
|||
FOR six = 0 TO 100/6 |
|||
FOR nine = 0 TO 100/9 |
|||
FOR twenty = 0 TO 100/20 |
|||
LET n = six*6 + nine*9 + twenty*20 |
|||
IF n <= 100 THEN LET nuggets(n) = 1 |
|||
NEXT twenty |
|||
NEXT nine |
|||
NEXT six |
|||
FOR n = 100 TO 1 STEP -1 |
|||
IF nuggets(n) <> 1 THEN |
|||
PRINT "Maximum non-McNuggets number is: "; n |
|||
EXIT FOR |
|||
END IF |
|||
NEXT n |
|||
END</syntaxhighlight> |
|||
==={{header|XBasic}}=== |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="qbasic">PROGRAM "McNuggets problem" |
|||
VERSION "0.0000" |
|||
DECLARE FUNCTION Entry () |
|||
FUNCTION Entry () |
|||
DIM N[100] |
|||
FOR A = 0 TO 100/6 |
|||
FOR B = 0 TO 100/9 |
|||
FOR C = 0 TO 100/20 |
|||
K = A*6+B*9+C*20 |
|||
IF K <= 100 THEN N[K] = 1 |
|||
NEXT C |
|||
NEXT B |
|||
NEXT A |
|||
FOR K = 100 TO 1 STEP -1 |
|||
IF N[K] <> 1 THEN PRINT "Maximum non-McNuggets number is: "; K : EXIT FOR |
|||
NEXT K |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
==={{header|Yabasic}}=== |
|||
<syntaxhighlight lang="vb">dim nuggets(100) |
|||
for six = 0 to 100/6 |
|||
for nine = 0 to 100/9 |
|||
for twenty = 0 to 100/20 |
|||
n = six*6 + nine*9 + twenty*20 |
|||
if n <= 100 nuggets(n) = true |
|||
next twenty |
|||
next nine |
|||
next six |
|||
for n = 100 to 1 step -1 |
|||
if nuggets(n) = false then |
|||
print "Maximum non-McNuggets number is: ", n |
|||
break |
|||
end if |
|||
next n</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is: 43</pre> |
|||
=={{header|BCPL}}== |
|||
<syntaxhighlight lang="bcpl">get "libhdr" |
|||
manifest $( limit = 100 $) |
|||
let start() be |
|||
$( let flags = vec limit |
|||
for i = 0 to limit do flags!i := false |
|||
for a = 0 to limit by 6 |
|||
for b = a to limit by 9 |
|||
for c = b to limit by 20 |
|||
do flags!c := true |
|||
for i = limit to 0 by -1 |
|||
unless flags!i |
|||
$( writef("Maximum non-McNuggets number: %N.*N", i) |
|||
finish |
|||
$) |
|||
$)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43.</pre> |
|||
=={{header|BQN}}== |
|||
<syntaxhighlight lang="bqn">100 ((↕⊣)(⌈´⊣×⊣¬∘∊⥊∘⊢)(<⊢)(+´×)¨(↕⌊∘÷)) 6‿9‿20</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int |
int |
||
Line 319: | Line 782: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 327: | Line 790: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<syntaxhighlight lang="c#"> |
|||
<lang c#> |
|||
using System; |
using System; |
||
Line 361: | Line 824: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Largest non-McNuggett Number less than 100: 43 |
Largest non-McNuggett Number less than 100: 43 |
||
</pre> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="c++"> |
|||
#include <cstdint> |
|||
#include <iostream> |
|||
#include <vector> |
|||
void mcnuggets(int32_t limit) { |
|||
std::vector<bool> mcnuggets_numbers(limit + 1, false); |
|||
for ( int32_t small = 0; small <= limit; small += 6 ) { |
|||
for ( int32_t medium = small; medium <= limit; medium += 9 ) { |
|||
for ( int32_t large = medium; large <= limit; large += 20 ) { |
|||
mcnuggets_numbers[large] = true; |
|||
} |
|||
} |
|||
} |
|||
for ( int32_t i = limit; i >= 0; --i ) { |
|||
if ( ! mcnuggets_numbers[i] ) { |
|||
std::cout << "Maximum non-McNuggets number is " << i << std::endl; |
|||
return; |
|||
} |
|||
} |
|||
} |
|||
int main() { |
|||
mcnuggets(100); |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Maximum non-McNuggets number is 43 |
|||
</pre> |
</pre> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn cart [colls] |
||
(if (empty? colls) |
(if (empty? colls) |
||
'(()) |
'(()) |
||
Line 379: | Line 875: | ||
(let [possible (distinct (map nuggets (cart (map range [18 13 6])))) |
(let [possible (distinct (map nuggets (cart (map range [18 13 6])))) |
||
mcmax (apply max (filter (fn [x] (not-any? #{x} possible)) (range 101)))] |
mcmax (apply max (filter (fn [x] (not-any? #{x} possible)) (range 101)))] |
||
(printf "Maximum non-McNuggets number is %d\n" mcmax))</ |
(printf "Maximum non-McNuggets number is %d\n" mcmax))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-McNuggets number is 43</pre> |
<pre>Maximum non-McNuggets number is 43</pre> |
||
=={{header|CLU}}== |
|||
<syntaxhighlight lang="clu">% Recursive nugget iterator. |
|||
% This yields all the nugget numbers of the given box sizes from start to max. |
|||
gen_nuggets = iter (start, max: int, sizes: sequence[int]) yields (int) |
|||
si = sequence[int] |
|||
if si$empty(sizes) then |
|||
yield(start) |
|||
else |
|||
for i: int in int$from_to_by(start, max, si$bottom(sizes)) do |
|||
for j: int in gen_nuggets(i, max, si$reml(sizes)) do |
|||
yield(j) |
|||
end |
|||
end |
|||
end |
|||
end gen_nuggets |
|||
start_up = proc () |
|||
max = 100 |
|||
ab = array[bool] |
|||
po: stream := stream$primary_output() |
|||
nuggets: ab := ab$fill(0,max+1,false) |
|||
for nugget: int in gen_nuggets(0, max, sequence[int]$[6,9,20]) do |
|||
nuggets[nugget] := true |
|||
end |
|||
maxn: int |
|||
for i: int in ab$indexes(nuggets) do |
|||
if ~nuggets[i] then maxn := i end |
|||
end |
|||
stream$putl(po, "Maximum non-McNuggets number: " || int$unparse(maxn)) |
|||
end start_up</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43</pre> |
|||
=={{header|COBOL}}== |
|||
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
|||
PROGRAM-ID. MCNUGGETS. |
|||
DATA DIVISION. |
|||
WORKING-STORAGE SECTION. |
|||
01 NUGGETS. |
|||
03 NUGGET-FLAGS PIC X OCCURS 100 TIMES. |
|||
88 IS-NUGGET VALUE 'X'. |
|||
01 A PIC 999. |
|||
01 B PIC 999. |
|||
01 C PIC 999. |
|||
PROCEDURE DIVISION. |
|||
BEGIN. |
|||
MOVE SPACES TO NUGGETS. |
|||
PERFORM A-LOOP VARYING A FROM 0 BY 6 |
|||
UNTIL A IS GREATER THAN 100. |
|||
MOVE 100 TO A. |
|||
FIND-LARGEST. |
|||
IF IS-NUGGET(A), SUBTRACT 1 FROM A, GO TO FIND-LARGEST. |
|||
DISPLAY 'Largest non-McNugget number: ', A. |
|||
STOP RUN. |
|||
A-LOOP. |
|||
PERFORM B-LOOP VARYING B FROM A BY 9 |
|||
UNTIL B IS GREATER THAN 100. |
|||
B-LOOP. |
|||
PERFORM C-LOOP VARYING C FROM B BY 20 |
|||
UNTIL C IS GREATER THAN 100. |
|||
C-LOOP. |
|||
IF C IS NOT EQUAL TO ZERO, MOVE 'X' TO NUGGET-FLAGS(C).</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Largest non-McNugget number: 043</pre> |
|||
=={{header|Comal}}== |
|||
<syntaxhighlight lang="comal">0010 limit#:=100 |
|||
0020 DIM nugget#(0:limit#) |
|||
0030 FOR a#:=0 TO limit# STEP 6 DO |
|||
0040 FOR b#:=a# TO limit# STEP 9 DO |
|||
0050 FOR c#:=b# TO limit# STEP 20 DO nugget#(c#):=TRUE |
|||
0060 ENDFOR b# |
|||
0070 ENDFOR a# |
|||
0080 FOR i#:=limit# TO 0 STEP -1 DO |
|||
0090 IF NOT nugget#(i#) THEN |
|||
0100 PRINT "Maximum non-McNuggets number: ",i# |
|||
0110 END |
|||
0120 ENDIF |
|||
0130 ENDFOR i#</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43</pre> |
|||
=={{header|Cowgol}}== |
|||
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
|||
const LIMIT := 100; |
|||
var flags: uint8[LIMIT+1]; |
|||
MemZero(&flags[0], @bytesof flags); |
|||
var a: @indexof flags; |
|||
var b: @indexof flags; |
|||
var c: @indexof flags; |
|||
a := 0; |
|||
while a <= LIMIT loop |
|||
b := a; |
|||
while b <= LIMIT loop |
|||
c := b; |
|||
while c <= LIMIT loop |
|||
flags[c] := 1; |
|||
c := c + 20; |
|||
end loop; |
|||
b := b + 9; |
|||
end loop; |
|||
a := a + 6; |
|||
end loop; |
|||
a := LIMIT; |
|||
loop |
|||
if flags[a] == 0 then |
|||
print("Maximum non-McNuggets number: "); |
|||
print_i32(a as uint32); |
|||
print_nl(); |
|||
break; |
|||
end if; |
|||
a := a - 1; |
|||
end loop;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43</pre> |
|||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang="dart">import 'dart:math'; |
||
main() { |
main() { |
||
var nuggets = List<int>.generate(101, (int index) => index); |
var nuggets = List<int>.generate(101, (int index) => index); |
||
Line 395: | Line 1,021: | ||
} |
} |
||
print('Largest non-McNuggets number: ${nuggets.reduce(max).toString() ?? 'none'}.'); |
print('Largest non-McNuggets number: ${nuggets.reduce(max).toString() ?? 'none'}.'); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Largest non-McNuggets number: 43.</pre> |
<pre>Largest non-McNuggets number: 43.</pre> |
||
=={{header|Draco}}== |
|||
<syntaxhighlight lang="draco">proc nonrec main() void: |
|||
byte LIMIT = 100; |
|||
[LIMIT+1] bool nugget; |
|||
byte a, b, c; |
|||
for a from 0 upto LIMIT do |
|||
nugget[a] := false |
|||
od; |
|||
for a from 0 by 6 upto LIMIT do |
|||
for b from a by 9 upto LIMIT do |
|||
for c from b by 20 upto LIMIT do |
|||
nugget[c] := true |
|||
od |
|||
od |
|||
od; |
|||
a := LIMIT; |
|||
while nugget[a] do a := a - 1 od; |
|||
writeln("Maximum non-McNuggets number: ", a) |
|||
corp</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43</pre> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
Line 405: | Line 1,056: | ||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="dyalect">func mcnugget(limit) { |
||
var sv = Array. |
var sv = Array.Empty(limit + 1, false) |
||
for s in 0. |
for s in 0^6..limit { |
||
for n in s. |
for n in s^9..limit { |
||
for t in n. |
for t in n^20..limit { |
||
sv[t] = true |
sv[t] = true |
||
} |
} |
||
} |
} |
||
} |
} |
||
for i in limit..0 { |
for i in limit^-1..0 { |
||
if !sv[i] { |
if !sv[i] { |
||
print("Maximum non-McNuggets number is \(i)") |
print("Maximum non-McNuggets number is \(i)") |
||
Line 422: | Line 1,073: | ||
} |
} |
||
mcnugget(100)</ |
mcnugget(100)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-McNuggets number is 43</pre> |
<pre>Maximum non-McNuggets number is 43</pre> |
||
=={{header|EasyLang}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight> |
|||
len l[] 100 |
|||
for a = 0 to 100 div 6 |
|||
for b = 0 to 100 div 9 |
|||
for c = 0 to 100 div 20 |
|||
n = a * 6 + b * 9 + c * 20 |
|||
if n >= 1 and n <= 100 |
|||
l[n] = 1 |
|||
. |
|||
. |
|||
. |
|||
. |
|||
for n = 100 downto 1 |
|||
if l[n] = 0 |
|||
print n |
|||
break 1 |
|||
. |
|||
. |
|||
</syntaxhighlight> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
Line 432: | Line 1,106: | ||
Uses MapSet and Comprehension |
Uses MapSet and Comprehension |
||
< |
<syntaxhighlight lang="elixir">defmodule Mcnugget do |
||
def solve(limit) do |
def solve(limit) do |
||
0..limit |
0..limit |
||
Line 454: | Line 1,128: | ||
Mcnugget.solve(100) |> IO.puts |
Mcnugget.solve(100) |> IO.puts |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 461: | Line 1,135: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// McNuggets. Nigel Galloway: October 28th., 2018 |
// McNuggets. Nigel Galloway: October 28th., 2018 |
||
let fN n g = Seq.initInfinite(fun ng->ng*n+g)|>Seq.takeWhile(fun n->n<=100) |
let fN n g = Seq.initInfinite(fun ng->ng*n+g)|>Seq.takeWhile(fun n->n<=100) |
||
printfn "%d" (Set.maxElement(Set.difference (set[1..100]) (fN 20 0|>Seq.collect(fun n->fN 9 n)|>Seq.collect(fun n->fN 6 n)|>Set.ofSeq))) |
printfn "%d" (Set.maxElement(Set.difference (set[1..100]) (fN 20 0|>Seq.collect(fun n->fN 9 n)|>Seq.collect(fun n->fN 6 n)|>Set.ofSeq))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 472: | Line 1,146: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: backtrack kernel math.ranges prettyprint sequences sets ; |
||
101 <iota> [ 0 6 9 20 [ 100 swap <range> amb-lazy ] tri@ ] bag-of diff last .</ |
101 <iota> [ 0 6 9 20 [ 100 swap <range> amb-lazy ] tri@ ] bag-of diff last .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
43 |
43 |
||
</pre> |
</pre> |
||
=={{header|FOCAL}}== |
|||
<syntaxhighlight lang="focal">01.10 F N=0,100;S T(N)=0 |
|||
01.20 F A=0,6,100;F B=A,9,100;F C=B,20,100;S T(C)=-1 |
|||
01.30 S N=101 |
|||
01.40 S N=N-1 |
|||
01.50 I (T(N))1.4 |
|||
01.60 T %3,N,! |
|||
01.70 Q</syntaxhighlight> |
|||
{{out}} |
|||
<pre>= 43</pre> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Dim As Integer l(100), a, b, c, n |
Dim As Integer l(100), a, b, c, n |
||
For a = 0 To 100/6 |
For a = 0 To 100/6 |
||
Line 494: | Line 1,179: | ||
Next n |
Next n |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
El mayor número que no sea McNugget es: 43 |
El mayor número que no sea McNugget es: 43 |
||
</pre> |
</pre> |
||
=={{header|Frink}}== |
|||
This is a nice demonstration for Frink's <CODE>multifor</CODE> loop which can perform arbitrarily-deeply-nested loops in a single statement. The "inner" (rightmost) loops can use values set by the "outer" (leftmost) as part of their bounds. |
|||
<syntaxhighlight lang="frink">a = toSet[0 to 100] |
|||
multifor [z,y,x] = [0 to 100 step 20, 0 to 100-z step 9, 0 to 100-z-y step 6] |
|||
a.remove[x+y+z] |
|||
println[max[a]]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
43 |
|||
</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn McNuggetsProblem |
|||
BOOL l(100) |
|||
long a, b, c, i, n |
|||
for a = 0 to 100/6 |
|||
for b = 0 to 100/9 |
|||
for c = 0 to 100/20 |
|||
n = a * 6 + b * 9 + c * 20 |
|||
if n <= 100 then l(n) = YES |
|||
next |
|||
next |
|||
next |
|||
for i = 100 to 1 step -1 |
|||
if l(i) == NO then print "The maximum non-McNuggets number less than 100 is: "; i: exit for |
|||
next |
|||
end fn |
|||
window 1,,( 0, 0, 450, 100 ) |
|||
fn McNuggetsProblem |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
The maximum non-McNuggets number less than 100 is: 43 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 524: | Line 1,258: | ||
func main() { |
func main() { |
||
mcnugget(100) |
mcnugget(100) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 532: | Line 1,266: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Set (Set, fromList, member) |
||
------------------------ MCNUGGETS ----------------------- |
|||
gaps :: [Int] |
|||
gaps = dropWhile (`member` mcNuggets) [100,99 .. 1] |
|||
mcNuggets :: Set Int |
mcNuggets :: Set Int |
||
mcNuggets = |
mcNuggets = |
||
let size = enumFromTo 0 . quot 100 |
let size = enumFromTo 0 . quot 100 |
||
in fromList $ |
in fromList $ |
||
size 6 |
size 6 |
||
\x -> |
>>= \x -> |
||
size 9 |
size 9 |
||
\y -> |
>>= \y -> |
||
size 20 |
size 20 |
||
\z -> |
>>= \z -> |
||
[ v |
|||
| let v = sum [6 * x, 9 * y, 20 * z], |
|||
101 > v |
|||
] |
|||
--------------------------- TEST ------------------------- |
|||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
(putStrLn . go) $ |
|||
dropWhile (`member` mcNuggets) [100, 99 .. 1] |
|||
case gaps of |
|||
where |
|||
x:_ -> show x |
|||
go (x : _) = show x |
|||
[] -> "No unreachable quantities found ..."</lang> |
|||
go [] = "No unreachable quantities found ..."</syntaxhighlight> |
|||
Or equivalently, making use of the list comprehension notation: |
Or equivalently, making use of the list comprehension notation: |
||
< |
<syntaxhighlight lang="haskell">import Data.Set (Set, fromList, member) |
||
gaps :: [Int] |
gaps :: [Int] |
||
Line 580: | Line 1,316: | ||
case gaps of |
case gaps of |
||
x:_ -> show x |
x:_ -> show x |
||
[] -> "No unreachable quantities found ..."</ |
[] -> "No unreachable quantities found ..."</syntaxhighlight> |
||
<pre>43</pre> |
<pre>43</pre> |
||
Line 587: | Line 1,323: | ||
Brute force solution: calculate all pure (just one kind of box) McNugget numbers which do not exceed 100, then compute all possible sums, and then remove those from the list of numbers up to 100 (which is obviously a McNugget number), then find the largest number remaining: |
Brute force solution: calculate all pure (just one kind of box) McNugget numbers which do not exceed 100, then compute all possible sums, and then remove those from the list of numbers up to 100 (which is obviously a McNugget number), then find the largest number remaining: |
||
< |
<syntaxhighlight lang="j"> >./(i.100)-.,+/&>{(* i.@>.@%~&101)&.>6 9 20 |
||
43</ |
43</syntaxhighlight> |
||
Technically, we could have used 100 in place of 101 when we were finding how many pure McNugget numbers were in each series (because 100 is obviously a McNugget number), but it's not like that's a problem, either. |
Technically, we could have used 100 in place of 101 when we were finding how many pure McNugget numbers were in each series (because 100 is obviously a McNugget number), but it's not like that's a problem, either. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class McNuggets { |
||
public static void main(String... args) { |
public static void main(String... args) { |
||
Line 647: | Line 1,383: | ||
return; |
return; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Largest non-McNugget number in the search space is 43</pre> |
<pre>Largest non-McNugget number in the search space is 43</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 731: | Line 1,467: | ||
main() |
main() |
||
); |
); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>43</pre> |
<pre>43</pre> |
||
Line 737: | Line 1,473: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{trans|Clojure}} |
{{trans|Clojure}} |
||
< |
<syntaxhighlight lang="jq">[ |
||
[range(18) as $n6 | |
[range(18) as $n6 | |
||
range(13) as $n9 | |
range(13) as $n9 | |
||
Line 748: | Line 1,484: | ||
select($possible|contains([$n])|not) |
select($possible|contains([$n])|not) |
||
] | |
] | |
||
max</ |
max</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>43</pre> |
<pre>43</pre> |
||
Line 754: | Line 1,490: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Simple brute force solution, though the BitSet would save memory considerably with larger max numbers. |
Simple brute force solution, though the BitSet would save memory considerably with larger max numbers. |
||
< |
<syntaxhighlight lang="julia">function mcnuggets(max) |
||
b = BitSet(1:max) |
b = BitSet(1:max) |
||
for i in 0:6:max, j in 0:9:max, k in 0:20:max |
for i in 0:6:max, j in 0:9:max, k in 0:20:max |
||
Line 763: | Line 1,499: | ||
println(mcnuggets(100)) |
println(mcnuggets(100)) |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
43 |
43 |
||
</pre> |
</pre> |
||
Line 769: | Line 1,505: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.71 |
||
fun mcnugget(limit: Int) { |
fun mcnugget(limit: Int) { |
||
Line 787: | Line 1,523: | ||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
mcnugget(100) |
mcnugget(100) |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 795: | Line 1,531: | ||
=={{header|Locomotive Basic}}== |
=={{header|Locomotive Basic}}== |
||
< |
<syntaxhighlight lang="locobasic">100 CLEAR |
||
110 DIM a(100) |
110 DIM a(100) |
||
120 FOR a=0 TO 100/6 |
120 FOR a=0 TO 100/6 |
||
Line 809: | Line 1,545: | ||
220 NEXT n |
220 NEXT n |
||
230 PRINT"The Largest non McNugget number is:";l |
230 PRINT"The Largest non McNugget number is:";l |
||
240 END</ |
240 END</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 815: | Line 1,551: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua"> |
||
function range(A,B) |
function range(A,B) |
||
return function() |
return function() |
||
Line 888: | Line 1,624: | ||
print(maximum(exclude(sum, range(1, N)))) |
print(maximum(exclude(sum, range(1, N)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 894: | Line 1,630: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|MACRO-11}}== |
||
<syntaxhighlight lang="macro11"> .TITLE NUGGET |
|||
<lang mathematica>Complement[Range[100], |
|||
.MCALL .TTYOUT,.EXIT |
|||
Select[6 #[[1]] + 9 #[[2]] + 20 #[[3]] & /@ |
|||
NUGGET::MOV #^D50,R1 |
|||
Tuples[Range[0, 17], 3], # < 101 &]][[-1]]</lang> |
|||
MOV #NUGBUF,R0 |
|||
CLEAR: CLR (R0)+ ; CLEAR BUFFER |
|||
SOB R1,CLEAR |
|||
MARK: MOV #^D100,R5 ; R5 = LIMIT |
|||
CLR R0 ; R0 = 6 STEPPER |
|||
1$: MOV R0,R1 ; R1 = 9 STEPPER |
|||
2$: MOV R1,R2 ; R2 = 20 STEPPER |
|||
3$: INCB NUGBUF(R2) ; MARK |
|||
ADD #^D20,R2 ; 20 STEP |
|||
CMP R2,R5 |
|||
BLT 3$ |
|||
ADD #^D9,R1 ; 9 STEP |
|||
CMP R1,R5 |
|||
BLT 2$ |
|||
ADD #^D6,R0 ; 6 STEP |
|||
CMP R0,R5 |
|||
BLT 1$ |
|||
SCAN: MOV #NUGBUF+^D100,R0 |
|||
1$: DEC R5 |
|||
MOVB -(R0),R1 |
|||
BNE 1$ |
|||
DIGIT: MOV #'0-1,R0 ; SPLIT DIGITS |
|||
1$: INC R0 |
|||
SUB #^D10,R5 |
|||
BCC 1$ |
|||
.TTYOUT ; HIGH DIGIT |
|||
MOV R5,R0 |
|||
ADD #'0+^D10,R0 |
|||
.TTYOUT ; LOW DIGIT |
|||
.EXIT |
|||
NUGBUF: .BLKB ^D100 |
|||
.END NUGGET</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|MAD}}== |
|||
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
|||
BOOLEAN NUGGET |
|||
DIMENSION NUGGET(101) |
|||
THROUGH CLEAR, FOR I=0, 1, I.G.100 |
|||
CLEAR NUGGET(I) = 0B |
|||
THROUGH MARK, FOR A=0, 6, A.G.100 |
|||
THROUGH MARK, FOR B=A, 9, B.G.100 |
|||
THROUGH MARK, FOR C=B, 20, C.G.100 |
|||
MARK NUGGET(C) = 1B |
|||
SEARCH THROUGH SEARCH, FOR I=100, -1, .NOT.NUGGET(I) |
|||
PRINT FORMAT F, I |
|||
VECTOR VALUES F = $29HMAXIMUM NON-MCNUGGET NUMBER: ,I2*$ |
|||
END OF PROGRAM </syntaxhighlight> |
|||
{{out}} |
|||
<pre>MAXIMUM NON-MCNUGGET NUMBER: 43</pre> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">FrobeniusNumber[{6, 9, 20}]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|Modula-2}}== |
|||
<syntaxhighlight lang="modula2">MODULE McNuggets; |
|||
FROM InOut IMPORT WriteCard, WriteString, WriteLn; |
|||
CONST Max = 100; |
|||
VAR a, b, c: CARDINAL; |
|||
nugget: ARRAY [0..Max] OF BOOLEAN; |
|||
BEGIN |
|||
FOR a := 0 TO Max DO |
|||
nugget[a] := FALSE; |
|||
END; |
|||
FOR a := 0 TO Max BY 6 DO |
|||
FOR b := a TO Max BY 9 DO |
|||
FOR c := b TO Max BY 20 DO |
|||
nugget[c] := TRUE; |
|||
END; |
|||
END; |
|||
END; |
|||
a := 100; |
|||
REPEAT DEC(a); UNTIL NOT nugget[a]; |
|||
WriteString("Maximum non-McNuggets number: "); |
|||
WriteCard(a, 2); |
|||
WriteLn(); |
|||
END McNuggets.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43</pre> |
|||
=={{header|MiniScript}}== |
|||
<syntaxhighlight lang="miniscript"> |
|||
n = range(0, 100) |
|||
for six in range(0, 100, 6) |
|||
for nine in range(0, 100, 9) |
|||
for twenty in range(0, 100, 20) |
|||
mcnuggets = six + nine + twenty |
|||
ix = n.indexOf(mcnuggets) |
|||
if ix != null then n.remove(ix) |
|||
end for |
|||
end for |
|||
end for |
|||
print "The largest non-McNugget number is " + n[-1] |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
The largest non-McNugget number is 43</pre> |
|||
43 |
|||
</pre> |
|||
=={{header|MiniZinc}}== |
=={{header|MiniZinc}}== |
||
<syntaxhighlight lang="minizinc"> |
|||
<lang MiniZinc> |
|||
%McNuggets. Nigel Galloway, August 27th., 2019 |
%McNuggets. Nigel Galloway, August 27th., 2019 |
||
var 0..99: n; |
var 0..99: n; |
||
Line 910: | Line 1,751: | ||
solve maximize n; |
solve maximize n; |
||
output [show(n)] |
output [show(n)] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 916: | Line 1,757: | ||
---------- |
---------- |
||
========== |
========== |
||
</pre> |
|||
=={{header|Nim}}== |
|||
<syntaxhighlight lang="nim">const Limit = 100 |
|||
var mcnuggets: array[0..Limit, bool] |
|||
for a in countup(0, Limit, 6): |
|||
for b in countup(a, Limit, 9): |
|||
for c in countup(b, Limit, 20): |
|||
mcnuggets[c] = true |
|||
for n in countdown(Limit, 0): |
|||
if not mcnuggets[n]: |
|||
echo "The largest non-McNuggets number is: ", n |
|||
break</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The largest non-McNuggets number is: 43</pre> |
|||
=={{header|Pascal}}== |
|||
A console program in Free Pascal. Same idea as the Raku solution, but without generalizing. We stop once we've found 6 consecutive integers that can be represented. |
|||
<syntaxhighlight lang="pascal"> |
|||
program McNuggets; |
|||
{$mode objfpc}{$H+} |
|||
const |
|||
ARRAY_SIZE_STEP = 20; // small, to demonstrate extending array dynamically |
|||
var |
|||
i, nr_consec : integer; |
|||
can_do : array of boolean; |
|||
begin |
|||
SetLength( can_do, ARRAY_SIZE_STEP); |
|||
can_do[0] := true; |
|||
nr_consec := 0; |
|||
i := 0; |
|||
repeat |
|||
inc(i); |
|||
if i >= Length( can_do) then SetLength( can_do, i + ARRAY_SIZE_STEP); |
|||
can_do[i] := ((i >= 6) and can_do[i - 6]) |
|||
or ((i >= 9) and can_do[i - 9]) |
|||
or ((i >= 20) and can_do[i - 20]); |
|||
if can_do[i] then begin |
|||
if can_do[i - 1] then inc( nr_consec) |
|||
else nr_consec := 1; |
|||
end |
|||
else nr_consec := 0; |
|||
until nr_consec = 6; |
|||
WriteLn ('Max that can''t be represented is ', i - 6); |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Max that can't be represented is 43 |
|||
</pre> |
</pre> |
||
Line 921: | Line 1,817: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use ntheory qw/forperm gcd vecmin/; |
||
sub Mcnugget_number { |
sub Mcnugget_number { |
||
Line 963: | Line 1,859: | ||
for my $counts ([6,9,20], [6,7,20], [1,3,20], [10,5,18], [5,17,44], [2,4,6], [3,6,15]) { |
for my $counts ([6,9,20], [6,7,20], [1,3,20], [10,5,18], [5,17,44], [2,4,6], [3,6,15]) { |
||
print 'Maximum non-Mcnugget number using ' . join(', ', @$counts) . ' is: ' . Mcnugget_number($counts) . "\n" |
print 'Maximum non-Mcnugget number using ' . join(', ', @$counts) . ' is: ' . Mcnugget_number($counts) . "\n" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-Mcnugget number using 6, 9, 20 is: 43 |
<pre>Maximum non-Mcnugget number using 6, 9, 20 is: 43 |
||
Line 974: | Line 1,870: | ||
===Perl using Regex=== |
===Perl using Regex=== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
$_ = 1 . 0 x 100; |
$_ = 1 . 0 x 100; |
||
1 while s/ (?=1) (?:.{6}|.{9}|.{20}) \K 0 /1/x; |
1 while s/ (?=1) (?:.{6}|.{9}|.{20}) \K 0 /1/x; |
||
/01*$/ and print "Maximum non-Mcnugget number is: $-[0]\n";</ |
/01*$/ and print "Maximum non-Mcnugget number is: $-[0]\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-Mcnugget number is: 43</pre> |
<pre>Maximum non-Mcnugget number is: 43</pre> |
||
Line 985: | Line 1,881: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<lang Phix>constant limit=100 |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
sequence nuggets = repeat(false,limit+1) |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">100</span> |
|||
for sixes=0 to limit by 6 do |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">nuggets</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
|||
for nines=sixes to limit by 9 do |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">sixes</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">6</span> <span style="color: #008080;">do</span> |
|||
for twenties=nines to limit by 20 do |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">nines</span><span style="color: #0000FF;">=</span><span style="color: #000000;">sixes</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span> |
|||
nuggets[twenties+1] = true |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">twenties</span><span style="color: #0000FF;">=</span><span style="color: #000000;">nines</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">20</span> <span style="color: #008080;">do</span> |
|||
end for |
|||
<span style="color: #000000;">nuggets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">twenties</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span> |
|||
end for |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end for |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
printf(1,"Maximum non-McNuggets number is %d\n", rfind(false,nuggets)-1)</lang> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Maximum non-McNuggets number is %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">rfind</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nuggets</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,001: | Line 1,900: | ||
Also, since it is a bit more interesting, a |
Also, since it is a bit more interesting, a |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<lang Phix>function Mcnugget_number(sequence counts) |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">Mcnugget_number</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">counts</span><span style="color: #0000FF;">)</span> |
|||
if gcd(counts)>1 then return "No maximum" end if |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">gcd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"No maximum"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
atom cmin = min(counts) |
|||
sequence meals = {} |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">cmin</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)</span> |
|||
sequence smin = {} |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">meals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">smin</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
|||
integer a = -1 |
|||
while true do |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> |
|||
a += 1 |
|||
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span> |
|||
for b=0 to a do |
|||
<span style="color: #000000;">a</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
for c=0 to b do |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">a</span> <span style="color: #008080;">do</span> |
|||
sequence s = {a, b, c} |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">b</span> <span style="color: #008080;">do</span> |
|||
for i=1 to factorial(3) do |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> |
|||
sequence p = permute(i,s) |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
integer k = sum(sq_mul(p,counts))+1 |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
if k>length(meals) then meals &= repeat(0,k-length(meals)) end if |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">))+</span><span style="color: #000000;">1</span> |
|||
meals[k] = 1 |
|||
<span style="color: #000080;font-style:italic;">-- atom k = sum(sq_mul(p,counts))+1</span> |
|||
end for |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">meals</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">meals</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">meals</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end for |
|||
<span style="color: #000000;">meals</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
end for |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
for i=1 to length(meals) do |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
if meals[i] then |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
if length(smin) and smin[$]+1=i-1 then |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">meals</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
smin = append(smin,i-1) |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">meals</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> |
|||
if length(smin)=cmin then exit end if |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">smin</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">smin</span><span style="color: #0000FF;">[$]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> |
|||
else |
|||
<span style="color: #000000;">smin</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">smin</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
|||
smin = {i-1} |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">smin</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">cmin</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end if |
|||
<span style="color: #008080;">else</span> |
|||
<span style="color: #000000;">smin</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> |
|||
end for |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if length(smin)=cmin then exit end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end while |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
return sprintf("%d",iff(smin[1]?smin[1]-1:0)) |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">smin</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">cmin</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end function |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">smin</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]?</span><span style="color: #000000;">smin</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span> |
|||
constant tests = {{6,9,20}, {6,7,20}, {1,3,20}, {10,5,18}, {5,17,44}, {2,4,6}, {3,6,15}} |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
for i=1 to length(tests) do |
|||
sequence ti = tests[i] |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">44</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">}}</span> |
|||
printf(1,"Maximum non-Mcnugget number using %s is: %s\n",{sprint(ti),Mcnugget_number(ti)}) |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
end for</lang> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Maximum non-Mcnugget number using %V is: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Mcnugget_number</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,053: | Line 1,956: | ||
Maximum non-Mcnugget number using {3,6,15} is: No maximum |
Maximum non-Mcnugget number using {3,6,15} is: No maximum |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
Using constraint modelling (cp solver). |
|||
<syntaxhighlight lang="picat">import cp. |
|||
go => |
|||
N :: 0..100, |
|||
foreach(X in 0..16, Y in 0..11, Z in 0..5) |
|||
6*X + 9*Y + 20*Z #!= N |
|||
end, |
|||
solve($[max(N)],N), |
|||
println(n=N).</syntaxhighlight> |
|||
{{out}} |
|||
<pre>n = 43</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de nuggets1 (M) |
||
(let Lst (range 0 M) |
(let Lst (range 0 M) |
||
(for A (range 0 M 6) |
(for A (range 0 M 6) |
||
Line 1,061: | Line 1,979: | ||
(for C (range B M 20) |
(for C (range B M 20) |
||
(set (nth Lst (inc C))) ) ) ) |
(set (nth Lst (inc C))) ) ) ) |
||
(apply max Lst) ) )</ |
(apply max Lst) ) )</syntaxhighlight> |
||
Generator from fiber: |
Generator from fiber: |
||
< |
<syntaxhighlight lang="picolisp">(de nugg (M) |
||
(co 'nugget |
(co 'nugget |
||
(for A (range 0 M 6) |
(for A (range 0 M 6) |
||
Line 1,073: | Line 1,991: | ||
(while (nugg 100) |
(while (nugg 100) |
||
(set (nth Lst @)) ) |
(set (nth Lst @)) ) |
||
(apply max Lst) ) )</ |
(apply max Lst) ) )</syntaxhighlight> |
||
Test versions against each other: |
Test versions against each other: |
||
< |
<syntaxhighlight lang="picolis">(test |
||
T |
T |
||
(= |
(= |
||
43 |
43 |
||
(nuggets1 100) |
(nuggets1 100) |
||
(nuggets2 100) ) )</ |
(nuggets2 100) ) )</syntaxhighlight> |
||
=={{header|PL/I}}== |
|||
<syntaxhighlight lang="pli">mcnugget: procedure options(main); |
|||
declare nugget(0:100) bit, (a, b, c) fixed; |
|||
do a=0 to 100; nugget(a) = '0'b; end; |
|||
do a=0 to 100 by 6; |
|||
do b=a to 100 by 9; |
|||
do c=b to 100 by 20; |
|||
nugget(c) = '1'b; |
|||
end; |
|||
end; |
|||
end; |
|||
do a=100 to 0 by -1; |
|||
if ^nugget(a) then do; |
|||
put skip list('Maximum non-McNuggets number:', a); |
|||
stop; |
|||
end; |
|||
end; |
|||
end mcnugget;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number: 43</pre> |
|||
=={{header|PL/M}}== |
|||
<syntaxhighlight lang="plm">100H: |
|||
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS; |
|||
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT; |
|||
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9, S); END PRINT; |
|||
PRINT$NUMBER: PROCEDURE (N); |
|||
DECLARE S (6) BYTE INITIAL ('...',13,10,'$'); |
|||
DECLARE P ADDRESS, (N, C BASED P) BYTE; |
|||
P = .S(3); |
|||
DIGIT: |
|||
P = P-1; |
|||
C = N MOD 10 + '0'; |
|||
N = N/10; |
|||
IF N>0 THEN GO TO DIGIT; |
|||
CALL PRINT(P); |
|||
END PRINT$NUMBER; |
|||
DECLARE (A, B, C) BYTE; |
|||
DECLARE NUGGET (101) BYTE; |
|||
DO A=0 TO 100; NUGGET(A) = 0; END; |
|||
DO A=0 TO 100 BY 6; |
|||
DO B=A TO 100 BY 9; |
|||
DO C=B TO 100 BY 20; |
|||
NUGGET(C) = -1; |
|||
END; |
|||
END; |
|||
END; |
|||
A = 100; |
|||
DO WHILE NUGGET(A); A = A-1; END; |
|||
CALL PRINT$NUMBER(A); |
|||
CALL EXIT; |
|||
EOF</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{trans|UNIX Shell}} |
{{trans|UNIX Shell}} |
||
< |
<syntaxhighlight lang="powershell">$possible = @{} |
||
For ($i=0; $i -lt 18; $i++) { |
For ($i=0; $i -lt 18; $i++) { |
||
For ($j=0; $j -lt 13; $j++) { |
For ($j=0; $j -lt 13; $j++) { |
||
Line 1,101: | Line 2,080: | ||
} |
} |
||
} |
} |
||
Write-Host "Maximum non-McNuggets number is $n"</ |
Write-Host "Maximum non-McNuggets number is $n"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-McNuggets number is 43</pre> |
<pre>Maximum non-McNuggets number is 43</pre> |
||
Line 1,108: | Line 2,087: | ||
===Python: REPL=== |
===Python: REPL=== |
||
It's a simple solution done on the command line: |
It's a simple solution done on the command line: |
||
< |
<syntaxhighlight lang="python">>>> from itertools import product |
||
>>> nuggets = set(range(101)) |
>>> nuggets = set(range(101)) |
||
>>> for s, n, t in product(range(100//6+1), range(100//9+1), range(100//20+1)): |
>>> for s, n, t in product(range(100//6+1), range(100//9+1), range(100//20+1)): |
||
Line 1,116: | Line 2,095: | ||
>>> max(nuggets) |
>>> max(nuggets) |
||
43 |
43 |
||
>>> </ |
>>> </syntaxhighlight> |
||
Single expression version (expect to be slower, however no noticeable difference on a Celeron B820 and haven't benchmarked): |
Single expression version (expect to be slower, however no noticeable difference on a Celeron B820 and haven't benchmarked): |
||
< |
<syntaxhighlight lang="python">>>> from itertools import product |
||
>>> max(x for x in range(100+1) if x not in |
>>> max(x for x in range(100+1) if x not in |
||
... (6*s + 9*n + 20*t for s, n, t in |
... (6*s + 9*n + 20*t for s, n, t in |
||
... product(range(100//6+1), range(100//9+1), range(100//20+1)))) |
... product(range(100//6+1), range(100//9+1), range(100//20+1)))) |
||
43 |
43 |
||
>>> </ |
>>> </syntaxhighlight> |
||
===Using Set Comprehension=== |
===Using Set Comprehension=== |
||
{{trans|FSharp}} |
{{trans|FSharp}} |
||
< |
<syntaxhighlight lang="python"> |
||
#Wherein I observe that Set Comprehension is not intrinsically dysfunctional. Nigel Galloway: October 28th., 2018 |
#Wherein I observe that Set Comprehension is not intrinsically dysfunctional. Nigel Galloway: October 28th., 2018 |
||
n = {n for x in range(0,101,20) for y in range(x,101,9) for n in range(y,101,6)} |
n = {n for x in range(0,101,20) for y in range(x,101,9) for n in range(y,101,6)} |
||
g = {n for n in range(101)} |
g = {n for n in range(101)} |
||
print(max(g.difference(n))) |
print(max(g.difference(n))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,145: | Line 2,124: | ||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''mcNuggets list monad''' |
||
from itertools import (chain, dropwhile) |
from itertools import (chain, dropwhile) |
||
Line 1,271: | Line 2,250: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,278: | Line 2,257: | ||
mcNuggetsByListMonad -> 43 |
mcNuggetsByListMonad -> 43 |
||
mcNuggetsByComprehension -> 43</pre> |
mcNuggetsByComprehension -> 43</pre> |
||
=={{header|Quackery}}== |
|||
<syntaxhighlight lang="quackery">0 temp put |
|||
100 6 / times |
|||
[ i 6 * |
|||
100 9 / times |
|||
[ dup i 9 * + |
|||
100 20 / times |
|||
[ dup i 20 * + |
|||
dup 101 < if |
|||
[ dup bit |
|||
temp take | temp put ] |
|||
drop ] |
|||
drop ] |
|||
drop ] |
|||
-1 temp take |
|||
101 times |
|||
[ dup i bit & 0 = |
|||
if |
|||
[ nip i swap |
|||
conclude ] ] |
|||
drop dup 0 < iff |
|||
[ drop |
|||
say "There are no non-McNugget numbers below 101" ] |
|||
else |
|||
[ say "The largest non-McNugget number below 101 is " |
|||
echo ] |
|||
char . emit</syntaxhighlight> |
|||
'''Output:''' |
|||
<pre>The largest non-McNugget number below 101 is 43.</pre> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
Line 1,283: | Line 2,293: | ||
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: |
||
< |
<syntaxhighlight 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)</ |
mcNuggets <- do.call(function(x, y, z) 6 * x + 9 * y + 20 * z, allInputs)</syntaxhighlight> |
||
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: |
||
< |
<syntaxhighlight lang="rsplus">mcNuggets2 <- rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20)))</syntaxhighlight> |
||
Either way, we get identical results, as checked by: |
Either way, we get identical results, as checked by: |
||
< |
<syntaxhighlight lang="rsplus">all(mcNuggets == mcNuggets2)</syntaxhighlight> |
||
For our final answer, note that our choice to remove values from the vector 0:100 means our outputs will already be sorted. |
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 |
<syntaxhighlight 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")</ |
cat("The non-McNuggets numbers that are no greater than 100 are:", results, "\nThe largest is", max(results), "\n")</syntaxhighlight> |
||
Ultimately, this can be done in one line: |
|||
<syntaxhighlight lang="rsplus">max(setdiff(0:100, rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20)))))</syntaxhighlight> |
|||
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 |
[1] TRUE</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))))) |
|||
[1] 43</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
{{trans|Python}} (one of them) |
{{trans|Python}} (one of them) |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(apply max (set->list (for*/fold ((s (list->set (range 1 101)))) |
(apply max (set->list (for*/fold ((s (list->set (range 1 101)))) |
||
((x (in-range 0 101 20)) |
((x (in-range 0 101 20)) |
||
(y (in-range x 101 9)) |
(y (in-range x 101 9)) |
||
(n (in-range y 101 6))) |
(n (in-range y 101 6))) |
||
(set-remove s n))))</ |
(set-remove s n))))</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,316: | Line 2,331: | ||
Finds the smallest count value, then looks for the first run of consecutive count totals able to be generated, that is at least the length of the smallest count size. From then on, every number can be generated by simply adding multiples of the minimum count to each of the totals in that run. |
Finds the smallest count value, then looks for the first run of consecutive count totals able to be generated, that is at least the length of the smallest count size. From then on, every number can be generated by simply adding multiples of the minimum count to each of the totals in that run. |
||
<lang |
<syntaxhighlight lang="raku" line>sub Mcnugget-number (*@counts) { |
||
return '∞' if 1 < [gcd] @counts; |
return '∞' if 1 < [gcd] @counts; |
||
Line 1,346: | Line 2,361: | ||
put "Maximum non-Mcnugget number using {$counts.join: ', '} is: ", |
put "Maximum non-Mcnugget number using {$counts.join: ', '} is: ", |
||
Mcnugget-number(|$counts) |
Mcnugget-number(|$counts) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-Mcnugget number using 6, 9, 20 is: 43 |
<pre>Maximum non-Mcnugget number using 6, 9, 20 is: 43 |
||
Line 1,355: | Line 2,370: | ||
Maximum non-Mcnugget number using 2, 4, 6 is: ∞ |
Maximum non-Mcnugget number using 2, 4, 6 is: ∞ |
||
Maximum non-Mcnugget number using 3, 6, 15 is: ∞</pre> |
Maximum non-Mcnugget number using 3, 6, 15 is: ∞</pre> |
||
=={{header|Refal}}== |
|||
<syntaxhighlight lang="refal">$ENTRY Go { |
|||
, <Iota 0 6 100>: e.A |
|||
, <Iota 0 9 100>: e.B |
|||
, <Iota 0 20 100>: e.C |
|||
, <Iota 0 1 100>: e.Nums |
|||
, <SumPairs (e.A) (<SumPairs (e.B) (e.C)>)>: e.Nuggets |
|||
, <RemoveAll (e.Nuggets) e.Nums>: e.NonNuggets |
|||
, e.NonNuggets: e.X s.Last |
|||
= <Prout 'The largest non-McNuggets number < 100 is: ' s.Last>; |
|||
}; |
|||
SumPairs { |
|||
() (e.Y) = ; |
|||
(s.I e.X) (e.Y) = <SumPairs1 s.I (e.Y)> <SumPairs (e.X) (e.Y)>; |
|||
}; |
|||
SumPairs1 { |
|||
s.I () = ; |
|||
s.I (s.X e.X) = <+ s.I s.X> <SumPairs1 s.I (e.X)>; |
|||
}; |
|||
Remove { |
|||
s.I e.X s.I e.Y = e.X <Remove s.I e.Y>; |
|||
s.I e.X = e.X; |
|||
}; |
|||
RemoveAll { |
|||
() e.X = e.X; |
|||
(s.R e.R) e.X = <RemoveAll (e.R) <Remove s.R e.X>>; |
|||
}; |
|||
Iota { |
|||
s.Start s.Step s.End, <Compare s.Start s.End>: { |
|||
'+' = ; |
|||
s.X = s.Start <Iota <+ s.Start s.Step> s.Step s.End>; |
|||
}; |
|||
};</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The largest non-McNuggets number < 100 is: 43</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,363: | Line 2,419: | ||
:* excludes meals that have a multiple order of nuggets |
:* excludes meals that have a multiple order of nuggets |
||
:* automatically computes the '''high''' value algebraically instead of using '''100'''. |
:* automatically computes the '''high''' value algebraically instead of using '''100'''. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm solves the McNuggets problem: the largest McNugget number for given meals. */ |
||
parse arg y /*obtain optional arguments from the CL*/ |
parse arg y /*obtain optional arguments from the CL*/ |
||
if y='' | y="," then y= 6 9 20 /*Not specified? Then use the defaults*/ |
if y='' | y="," then y= 6 9 20 /*Not specified? Then use the defaults*/ |
||
Line 1,407: | Line 2,463: | ||
do while $\==''; parse var $ y $; y= abs(y); if y==0 then iterate |
do while $\==''; parse var $ y $; y= abs(y); if y==0 then iterate |
||
do until y==0; parse value x//y y with y x; end |
do until y==0; parse value x//y y with y x; end |
||
end; return x</ |
end; return x</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,416: | Line 2,472: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
Nuggets = list(100) |
Nuggets = list(100) |
||
Line 1,436: | Line 2,492: | ||
ok |
ok |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Maximum non-McNuggets number is: 43 |
Maximum non-McNuggets number is: 43 |
||
</pre> |
|||
=={{header|RPL}}== |
|||
{{trans|Go}} |
|||
« → limit |
|||
« { } limit 1 + + 0 CON |
|||
0 limit '''FOR''' s |
|||
s limit '''FOR''' n |
|||
n limit '''FOR''' t |
|||
t 1 + 1 PUT |
|||
20 '''STEP''' |
|||
9 '''STEP''' |
|||
6 '''STEP''' |
|||
limit |
|||
'''WHILE''' DUP2 GET '''REPEAT''' 1 - '''END''' |
|||
1 + SWAP DROP |
|||
» » '<span style="color:blue">MCNUGTS</span>' STO |
|||
We can tweak a little bit the above traduction, to benefit from latest efficient built-in functions: |
|||
{{works with|HP|49}} |
|||
« → limit |
|||
« 0 limit NDUPN →LIST |
|||
0 limit '''FOR''' s |
|||
s limit '''FOR''' n |
|||
n limit '''FOR''' t |
|||
limit t - 1 + 1 PUT |
|||
20 '''STEP''' |
|||
9 '''STEP''' |
|||
6 '''STEP''' |
|||
0 POS limit SWAP - 1 + |
|||
» » '<span style="color:blue">MCNUGTS</span>' STO |
|||
100 <span style="color:blue">MCNUGTS</span> |
|||
{{out}} |
|||
<pre> |
|||
1: 43 |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ruby">def mcnugget(limit) |
||
sv = (0..limit).to_a |
sv = (0..limit).to_a |
||
Line 1,458: | Line 2,549: | ||
end |
end |
||
puts(mcnugget 100)</ |
puts(mcnugget 100)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,464: | Line 2,555: | ||
</pre> |
</pre> |
||
Generic solution, allowing for more or less then 3 portion-sizes: |
Generic solution, allowing for more or less then 3 portion-sizes: |
||
< |
<syntaxhighlight lang="ruby">limit = 100 |
||
nugget_portions = [6, 9, 20] |
nugget_portions = [6, 9, 20] |
||
arrs = nugget_portions.map{|n| 0.step(limit, n).to_a } |
arrs = nugget_portions.map{|n| 0.step(limit, n).to_a } |
||
hits = arrs.pop.product(*arrs).map(&:sum) |
hits = arrs.pop.product(*arrs).map(&:sum) |
||
p ((0..limit).to_a - hits).max # => 43</ |
p ((0..limit).to_a - hits).max # => 43</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 1,475: | Line 2,566: | ||
Generalization of Rødseth’s Algorithm explained in [https://parramining.blogspot.com/2019/09/generalization-of-rdseths-algorithm-for.html post]. |
Generalization of Rødseth’s Algorithm explained in [https://parramining.blogspot.com/2019/09/generalization-of-rdseths-algorithm-for.html post]. |
||
Working code: [https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1424a910a196fb3d0e964c754fbf325c Rust playground]. |
Working code: [https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1424a910a196fb3d0e964c754fbf325c Rust playground]. |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let test_cases = vec![ |
let test_cases = vec![ |
||
[6, 9, 20], |
[6, 9, 20], |
||
Line 1,563: | Line 2,654: | ||
(m * y + cc) / aa |
(m * y + cc) / aa |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,578: | Line 2,669: | ||
g(6, 15, 1) = -1 |
g(6, 15, 1) = -1 |
||
</pre> |
</pre> |
||
=={{header|SETL}}== |
|||
<syntaxhighlight lang="setl">program mcnuggets; |
|||
nuggets := +/+/ {{{ x + y + z |
|||
: x in [0, 6..100] } |
|||
: y in [0, 9..100] } |
|||
: z in [0, 20..100] }; |
|||
print(max/{n : n in [1..100] | not n in nuggets}); |
|||
end program;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func maxNugget(limit: Int) -> Int { |
||
var (max, sixes, nines, twenties, i) = (0, 0, 0, 0, 0) |
var (max, sixes, nines, twenties, i) = (0, 0, 0, 0, 0) |
||
Line 1,625: | Line 2,728: | ||
} |
} |
||
print(maxNugget(limit: 100))</ |
print(maxNugget(limit: 100))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,632: | Line 2,735: | ||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
< |
<syntaxhighlight lang="tailspin"> |
||
templates largestNonMcNuggetNumber |
templates largestNonMcNuggetNumber |
||
@: { largest: 0, mcNuggetNumbers: [1..$+20 -> 0] }; |
@: { largest: 0, mcNuggetNumbers: [1..$+20 -> 0] }; |
||
Line 1,643: | Line 2,746: | ||
100 -> largestNonMcNuggetNumber -> !OUT::write |
100 -> largestNonMcNuggetNumber -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,654: | Line 2,757: | ||
{{works with|ksh}} |
{{works with|ksh}} |
||
{{works with|zsh}} |
{{works with|zsh}} |
||
< |
<syntaxhighlight lang="bash">possible=() |
||
for (( i=0; i<18; ++i )); do |
for (( i=0; i<18; ++i )); do |
||
for (( j=0; j<13; ++j )); do |
for (( j=0; j<13; ++j )); do |
||
Line 1,673: | Line 2,776: | ||
done |
done |
||
printf 'Maximum non-McNuggets number is %d\n' $n</ |
printf 'Maximum non-McNuggets number is %d\n' $n</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-McNuggets number is 43</pre> |
<pre>Maximum non-McNuggets number is 43</pre> |
||
{{works with|sh}} |
{{works with|sh}} |
||
< |
<syntaxhighlight lang="bash">possible= |
||
i=0 |
i=0 |
||
while [ $i -lt 18 ]; do |
while [ $i -lt 18 ]; do |
||
Line 1,700: | Line 2,803: | ||
break |
break |
||
done |
done |
||
echo "Maximum non-McNuggets number is $n"</ |
echo "Maximum non-McNuggets number is $n"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Maximum non-McNuggets number is 43</pre> |
<pre>Maximum non-McNuggets number is 43</pre> |
||
=={{header|V (Vlang)}}== |
|||
{{trans|Go}} |
|||
<syntaxhighlight lang="v (vlang)">fn mcnugget(limit int) { |
|||
mut sv := []bool{len: limit+1} // all false by default |
|||
for s := 0; s <= limit; s += 6 { |
|||
for n := s; n <= limit; n += 9 { |
|||
for t := n; t <= limit; t += 20 { |
|||
sv[t] = true |
|||
} |
|||
} |
|||
} |
|||
for i := limit; i >= 0; i-- { |
|||
if !sv[i] { |
|||
println("Maximum non-McNuggets number is $i") |
|||
return |
|||
} |
|||
} |
|||
} |
|||
fn main() { |
|||
mcnugget(100) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Maximum non-McNuggets number is 43</pre> |
|||
=={{header|VTL-2}}== |
|||
<syntaxhighlight lang="vtl2">10 N=0 |
|||
20 :N+1)=0 |
|||
30 N=N+1 |
|||
40 #=100>N*20 |
|||
50 A=0 |
|||
60 B=A |
|||
70 C=B |
|||
80 :C+1)=160 |
|||
90 C=C+20 |
|||
100 #=100>C*80 |
|||
110 B=B+9 |
|||
120 #=100>B*70 |
|||
130 A=A+6 |
|||
140 #=100>A*60 |
|||
150 N=101 |
|||
160 N=N-1 |
|||
170 #=:N+1) |
|||
180 ?="Largest non-McNuggets number: "; |
|||
190 ?=N</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Largest non-McNuggets number: 43</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="wren">var mcnugget = Fn.new { |limit| |
||
var sv = List.filled(limit+1, false) |
var sv = List.filled(limit+1, false) |
||
var s = 0 |
var s = 0 |
||
Line 1,729: | Line 2,882: | ||
} |
} |
||
mcnugget.call(100)</ |
mcnugget.call(100)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,735: | Line 2,888: | ||
Maximum non-McNuggets number is 43 |
Maximum non-McNuggets number is 43 |
||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang="xpl0">int N, A(101), X, Y, Z; |
|||
[for N:= 0 to 100 do A(N):= false; |
|||
for X:= 0 to 100/6 do |
|||
for Y:= 0 to 100/9 do |
|||
for Z:= 0 to 100/20 do |
|||
[N:= 6*X + 9*Y + 20*Z; |
|||
if N <= 100 then A(N):= true; |
|||
]; |
|||
for N:= 100 downto 0 do |
|||
if A(N) = false then |
|||
[IntOut(0, N); |
|||
exit; |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="zkl">nuggets:=[0..101].pump(List()); // (0,1,2,3..101), mutable |
||
foreach s,n,t in ([0..100/6],[0..100/9],[0..100/20]) |
foreach s,n,t in ([0..100/6],[0..100/9],[0..100/20]) |
||
{ nuggets[(6*s + 9*n + 20*t).min(101)]=0 } |
{ nuggets[(6*s + 9*n + 20*t).min(101)]=0 } |
||
println((0).max(nuggets));</ |
println((0).max(nuggets));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 07:49, 7 May 2024
You are encouraged to solve this task according to the task description, using any language you may know.
From Wikipedia:
The McNuggets version of the coin problem was introduced by Henri Picciotto, who included it in his algebra textbook co-authored with Anita Wah. Picciotto thought of the application in the 1980s while dining with his son at McDonald's, working the problem out on a napkin. A McNugget number is the total number of McDonald's Chicken McNuggets in any number of boxes. In the United Kingdom, the original boxes (prior to the introduction of the Happy Meal-sized nugget boxes) were of 6, 9, and 20 nuggets.
- Task
Calculate (from 0 up to a limit of 100) the largest non-McNuggets
number (a number n which cannot be expressed with 6x + 9y + 20z = n
where x, y and z are natural numbers).
11l
V nuggets = Set(0..100)
L(s, n, t) cart_product(0 .. 100 I/ 6,
0 .. 100 I/ 9,
0 .. 100 I/ 20)
nuggets.discard(6*s + 9*n + 20*t)
print(max(nuggets))
- Output:
43
8080 Assembly
org 100h
lxi h,200h ; Zero out a page to keep nugget flags
xra a
znugs: mov m,a
inr l
jnz znugs
lxi b,101 ; B = 6 stepper, C = 101 (limit)
loopa: mov d,b ; D = 9 stepper
loopb: mov l,d ; L = 20 stepper
loopc: inr m ; Mark nugget
mvi a,20 ; 20 step
add l
mov l,a
cmp c
jc loopc
mvi a,9 ; 9 step
add d
mov d,a
cmp c
jc loopb
mvi a,6 ; 6 step
add b
mov b,a
cmp c
jc loopa
mov l,c ; Find largest number not seen
scan: dcr l
dcr m
jp scan
mov a,l
mvi b,'0'-1 ; B = high digit
digit: inr b
sui 10
jnc digit
adi '0'+10 ; A = low digit
lxi h,digits+1
mov m,a ; Store digits
dcx h
mov m,b
xchg
mvi c,9 ; CP/M print string
jmp 5
digits: db 0,0,'$' ; Placeholder for output
- Output:
43
ABC
PUT {1..100} IN non.nuggets
PUT 0 IN a
WHILE a <= 100:
PUT a IN b
WHILE b <= 100:
PUT b IN c
WHILE c <= 100:
IF c in non.nuggets:
REMOVE c FROM non.nuggets
PUT c+20 IN c
PUT b+9 IN b
PUT a+6 IN a
WRITE "Maximum non-McNuggets number:", max non.nuggets/
- Output:
Maximum non-McNuggets number: 43
Action!
PROC Main()
BYTE x,y,z,n
BYTE ARRAY nuggets(101)
FOR n=0 TO 100
DO
nuggets(n)=0
OD
FOR x=0 TO 100 STEP 6
DO
FOR y=0 TO 100 STEP 9
DO
FOR z=0 TO 100 STEP 20
DO
n=x+y+z
IF n<=100 THEN
nuggets(n)=1
FI
OD
OD
OD
n=100
DO
IF nuggets(n)=0 THEN
PrintF("The largest non McNugget number is %B%E",n)
EXIT
ELSEIF n=0 THEN
PrintE("There is no result")
EXIT
ELSE
n==-1
FI
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
The largest non McNugget number is 43
Ada
with Ada.Text_IO; use Ada.Text_IO;
procedure McNugget is
Limit : constant := 100;
List : array (0 .. Limit) of Boolean := (others => False);
N : Integer;
begin
for A in 0 .. Limit / 6 loop
for B in 0 .. Limit / 9 loop
for C in 0 .. Limit / 20 loop
N := A * 6 + B * 9 + C * 20;
if N <= 100 then
List (N) := True;
end if;
end loop;
end loop;
end loop;
for N in reverse 1 .. Limit loop
if not List (N) then
Put_Line ("The largest non McNugget number is:" & Integer'Image (N));
exit;
end if;
end loop;
end McNugget;
- Output:
The largest non McNugget number is: 43
ALGOL 68
BEGIN
# Solve the McNuggets problem: find the largest n <= 100 for which there #
# are no non-negative integers x, y, z such that 6x + 9y + 20z = n #
INT max nuggets = 100;
[ 0 : max nuggets ]BOOL sum;
FOR i FROM LWB sum TO UPB sum DO sum[ i ] := FALSE OD;
FOR x FROM 0 BY 6 TO max nuggets DO
FOR y FROM x BY 9 TO max nuggets DO
FOR z FROM y BY 20 TO max nuggets DO
sum[ z ] := TRUE
OD # z #
OD # y #
OD # x # ;
# show the highest number that cannot be formed #
INT largest := -1;
FOR i FROM UPB sum BY -1 TO LWB sum WHILE sum[ largest := i ] DO SKIP OD;
print( ( "The largest non McNugget number is: "
, whole( largest, 0 )
, newline
)
)
END
- Output:
The largest non McNugget number is: 43
APL
100 (⌈/(⍳⊣)~(⊂⊢)(+/×)¨(,⎕IO-⍨(⍳∘⌊÷))) 6 9 20
- Output:
43
AppleScript
Generalised for other set sizes, and for other triples of natural numbers. Uses NSMutableSet, through the AppleScript ObjC interface:
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
on run
set setNuggets to mcNuggetSet(100, 6, 9, 20)
script isMcNugget
on |λ|(x)
setMember(x, setNuggets)
end |λ|
end script
set xs to dropWhile(isMcNugget, enumFromThenTo(100, 99, 1))
set setNuggets to missing value -- Clear ObjC pointer value
if 0 < length of xs then
item 1 of xs
else
"No unreachable quantities in this range"
end if
end run
-- mcNuggetSet :: Int -> Int -> Int -> Int -> ObjC Set
on mcNuggetSet(n, mcx, mcy, mcz)
set upTo to enumFromTo(0)
script fx
on |λ|(x)
script fy
on |λ|(y)
script fz
on |λ|(z)
set v to sum({mcx * x, mcy * y, mcz * z})
if 101 > v then
{v}
else
{}
end if
end |λ|
end script
concatMap(fz, upTo's |λ|(n div mcz))
end |λ|
end script
concatMap(fy, upTo's |λ|(n div mcy))
end |λ|
end script
setFromList(concatMap(fx, upTo's |λ|(n div mcx)))
end mcNuggetSet
-- GENERIC FUNCTIONS ----------------------------------------------------
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
set lng to length of xs
set acc to {}
tell mReturn(f)
repeat with i from 1 to lng
set acc to acc & |λ|(item i of xs, i, xs)
end repeat
end tell
return acc
end concatMap
-- drop :: Int -> [a] -> [a]
-- drop :: Int -> String -> String
on drop(n, xs)
set c to class of xs
if c is not script then
if c is not string then
if n < length of xs then
items (1 + n) thru -1 of xs
else
{}
end if
else
if n < length of xs then
text (1 + n) thru -1 of xs
else
""
end if
end if
else
take(n, xs) -- consumed
return xs
end if
end drop
-- dropWhile :: (a -> Bool) -> [a] -> [a]
-- dropWhile :: (Char -> Bool) -> String -> String
on dropWhile(p, xs)
set lng to length of xs
set i to 1
tell mReturn(p)
repeat while i ≤ lng and |λ|(item i of xs)
set i to i + 1
end repeat
end tell
drop(i - 1, xs)
end dropWhile
-- enumFromThenTo :: Int -> Int -> Int -> [Int]
on enumFromThenTo(x1, x2, y)
set xs to {}
repeat with i from x1 to y by (x2 - x1)
set end of xs to i
end repeat
return xs
end enumFromThenTo
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m)
script
on |λ|(n)
if m ≤ n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
return lst
else
return {}
end if
end |λ|
end script
end enumFromTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- sum :: [Num] -> Num
on sum(xs)
script add
on |λ|(a, b)
a + b
end |λ|
end script
foldl(add, 0, xs)
end sum
-- NB All names of NSMutableSets should be set to *missing value*
-- before the script exits.
-- ( scpt files can not be saved if they contain ObjC pointer values )
-- setFromList :: Ord a => [a] -> Set a
on setFromList(xs)
set ca to current application
ca's NSMutableSet's ¬
setWithArray:(ca's NSArray's arrayWithArray:(xs))
end setFromList
-- setMember :: Ord a => a -> Set a -> Bool
on setMember(x, objcSet)
missing value is not (objcSet's member:(x))
end setMember
- Output:
43
Arturo
nonMcNuggets: function [lim][
result: new 0..lim
loop range.step:6 1 lim 'x [
loop range.step:9 1 lim 'y [
loop range.step:20 1 lim 'z
-> 'result -- sum @[x y z]
]
]
return result
]
print max nonMcNuggets 100
- Output:
46
Asymptote
bool[] n;
for(int i = 0; i <= 100; ++i) { n[i] = false; }
int k;
for (int a = 0; a < 100/6; ++a) {
for (int b = 0; b < 100/9; ++b) {
for (int c = 0; c < 100/20; ++c) {
k = a*6 + b*9 + c*20;
if (k <= 100) { n[k] = true; }
}
}
}
for (int k = 100; k >= 0; --k) {
if (n[k] != true) {
write("Maximum non-McNuggets number is: ", k);
break;
}
}
- Output:
Maximum non-McNuggets number is: 43
AWK
# syntax: GAWK -f MCNUGGETS_PROBLEM.AWK
# converted from Go
BEGIN {
limit = 100
for (a=0; a<=limit; a+=6) {
for (b=a; b<=limit; b+=9) {
for (c=b; c<=limit; c+=20) {
arr[c] = 1
}
}
}
for (i=limit; i>=0; i--) {
if (!arr[i]+0) {
printf("%d\n",i)
break
}
}
exit(0)
}
- Output:
43
BASIC
10 DEFINT A-Z: DIM F(100)
20 FOR A=0 TO 100 STEP 6
30 FOR B=A TO 100 STEP 9
40 FOR C=B TO 100 STEP 20
50 F(C)=-1
60 NEXT C,B,A
70 FOR A=100 TO 0 STEP -1
80 IF NOT F(A) THEN PRINT A: END
90 NEXT A
- Output:
43
Applesoft BASIC
100 dim nuggets(100)
110 for six = 0 to 100/6
120 for nine = 0 to 100/9
130 for twenty = 0 to 100/20
140 n = six*6+nine*9+twenty*20
150 if n <= 100 then nuggets(n) = 1
160 next twenty
170 next nine
180 next six
190 for n = 100 to 1 step -1
200 if nuggets(n) <> 1 then print "Maximum non-McNuggets number is: ";n : goto 250
240 next n
250 end
- Output:
Maximum non-McNuggets number is: 43
BASIC256
arraybase 1
dim nuggets(100)
for six = 0 To 100/6
for nine = 0 To 100/9
for twenty = 0 To 100/20
n = six*6 + nine*9 + twenty*20
if n <= 100 then nuggets[n] = true
next twenty
next nine
next six
for n = 100 to 1 step -1
if nuggets[n] = false then
print "Maximum non-McNuggets number is: "; n
exit for
end if
next n
- Output:
Maximum non-McNuggets number is: 43
Chipmunk Basic
100 dim nuggets(100)
110 for six = 0 to 100/6
120 for nine = 0 to 100/9
130 for twenty = 0 to 100/20
140 n = six*6+nine*9+twenty*20
150 if n <= 100 then nuggets(n) = 1
160 next twenty
170 next nine
180 next six
190 for n = 100 to 1 step -1
200 if nuggets(n) <> 1 then
210 print "Maximum non-McNuggets number is: ";n
220 end
230 endif
240 next n
250 end
- Output:
Maximum non-McNuggets number is: 43
Gambas
Public l[101] As Integer
Public Sub Main()
Dim a As Integer, b As Integer, c As Integer, n As Integer
For a = 0 To 100 / 6
For b = 0 To 100 / 9
For c = 0 To 100 / 20
n = a * 6 + b * 9 + c * 20
If n <= 100 Then l[n] = True
Next
Next
Next
For n = 100 To 1 Step -1
If Not l[n] Then
Print "Maximum non-McNuggets number is: "; n
Break
End If
Next
End
- Output:
Maximum non-McNuggets number is: 43
GW-BASIC
The Chipmunk Basic solution works without any changes.
Minimal BASIC
10 DIM N(100) : rem 10 ARRAY N for Quite BASIC
20 FOR A = 0 TO 100/6
30 FOR B = 0 TO 100/9
40 FOR C = 0 TO 100/20
50 LET K = A*6+B*9+C*20
60 IF K <= 100 THEN 80
70 GOTO 90
80 LET N(K) = 1
90 NEXT C
100 NEXT B
110 NEXT A
120 FOR K = 100 TO 1 STEP -1
130 IF N(K) <> 1 THEN 160
140 NEXT K
150 STOP
160 PRINT "Maximum non-McNuggets number is: "; K
170 END
- Output:
Maximum non-McNuggets number is: 43
MSX Basic
The Minimal BASIC solution works without any changes.
PureBasic
OpenConsole()
Define n.i
Dim nuggets.i(100)
For six.i = 0 To 100/6
For nine.i = 0 To 100/9
For twenty.i = 0 To 100/20
n = six*6 + nine*9 + twenty*20
If n <= 100
nuggets(n) = #True
EndIf
Next twenty
Next nine
Next six
For n = 100 To 1 Step -1
If nuggets(n) = #False
PrintN("Maximum non-McNuggets number is: " + Str(n))
Break
EndIf
Next n
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
- Output:
Maximum non-McNuggets number is: 43
Quite BASIC
The Minimal BASIC solution works without any changes.
Run BASIC
dim nuggets(100)
for six = 0 to 100/6
for nine = 0 to 100/9
for twenty = 0 to 100/20
n = six*6 + nine*9 + twenty*20
if n <= 100 then nuggets(n) = 1
next twenty
next nine
next six
for n = 100 to 1 step -1
if nuggets(n) <> 1 then
print "Maximum non-McNuggets number is: "; n
end
end if
next n
- Output:
Maximum non-McNuggets number is: 43
True BASIC
OPTION BASE 0
DIM nuggets(100)
FOR n = 0 TO 100
LET nuggets(n) = 0
NEXT n
FOR six = 0 TO 100/6
FOR nine = 0 TO 100/9
FOR twenty = 0 TO 100/20
LET n = six*6 + nine*9 + twenty*20
IF n <= 100 THEN LET nuggets(n) = 1
NEXT twenty
NEXT nine
NEXT six
FOR n = 100 TO 1 STEP -1
IF nuggets(n) <> 1 THEN
PRINT "Maximum non-McNuggets number is: "; n
EXIT FOR
END IF
NEXT n
END
XBasic
PROGRAM "McNuggets problem"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
DIM N[100]
FOR A = 0 TO 100/6
FOR B = 0 TO 100/9
FOR C = 0 TO 100/20
K = A*6+B*9+C*20
IF K <= 100 THEN N[K] = 1
NEXT C
NEXT B
NEXT A
FOR K = 100 TO 1 STEP -1
IF N[K] <> 1 THEN PRINT "Maximum non-McNuggets number is: "; K : EXIT FOR
NEXT K
END FUNCTION
END PROGRAM
- Output:
Maximum non-McNuggets number is: 43
Yabasic
dim nuggets(100)
for six = 0 to 100/6
for nine = 0 to 100/9
for twenty = 0 to 100/20
n = six*6 + nine*9 + twenty*20
if n <= 100 nuggets(n) = true
next twenty
next nine
next six
for n = 100 to 1 step -1
if nuggets(n) = false then
print "Maximum non-McNuggets number is: ", n
break
end if
next n
- Output:
Maximum non-McNuggets number is: 43
BCPL
get "libhdr"
manifest $( limit = 100 $)
let start() be
$( let flags = vec limit
for i = 0 to limit do flags!i := false
for a = 0 to limit by 6
for b = a to limit by 9
for c = b to limit by 20
do flags!c := true
for i = limit to 0 by -1
unless flags!i
$( writef("Maximum non-McNuggets number: %N.*N", i)
finish
$)
$)
- Output:
Maximum non-McNuggets number: 43.
BQN
100 ((↕⊣)(⌈´⊣×⊣¬∘∊⥊∘⊢)(<⊢)(+´×)¨(↕⌊∘÷)) 6‿9‿20
- Output:
43
C
#include <stdio.h>
int
main() {
int max = 0, i = 0, sixes, nines, twenties;
loopstart: while (i < 100) {
for (sixes = 0; sixes*6 < i; sixes++) {
if (sixes*6 == i) {
i++;
goto loopstart;
}
for (nines = 0; nines*9 < i; nines++) {
if (sixes*6 + nines*9 == i) {
i++;
goto loopstart;
}
for (twenties = 0; twenties*20 < i; twenties++) {
if (sixes*6 + nines*9 + twenties*20 == i) {
i++;
goto loopstart;
}
}
}
}
max = i;
i++;
}
printf("Maximum non-McNuggets number is %d\n", max);
return 0;
}
- Output:
Maximum non-McNuggets number is 43
C#
using System;
public class McNuggets
{
public static void Main()
{
bool[] isMcNuggetNumber = new bool[101];
for (int x = 0; x <= 100/6; x++)
{
for (int y = 0; y <= 100/9; y++)
{
for (int z = 0; z <= 100/20; z++)
{
int mcNuggetNumber = x*6 + y*9 + z*20;
if (mcNuggetNumber <= 100)
{
isMcNuggetNumber[mcNuggetNumber] = true;
}
}
}
}
for (int mnnCheck = isMcNuggetNumber.Length-1; mnnCheck >= 0; mnnCheck--)
{
if (!isMcNuggetNumber[mnnCheck])
{
Console.WriteLine("Largest non-McNuggett Number less than 100: " + mnnCheck.ToString());
break;
}
}
}
}
- Output:
Largest non-McNuggett Number less than 100: 43
C++
#include <cstdint>
#include <iostream>
#include <vector>
void mcnuggets(int32_t limit) {
std::vector<bool> mcnuggets_numbers(limit + 1, false);
for ( int32_t small = 0; small <= limit; small += 6 ) {
for ( int32_t medium = small; medium <= limit; medium += 9 ) {
for ( int32_t large = medium; large <= limit; large += 20 ) {
mcnuggets_numbers[large] = true;
}
}
}
for ( int32_t i = limit; i >= 0; --i ) {
if ( ! mcnuggets_numbers[i] ) {
std::cout << "Maximum non-McNuggets number is " << i << std::endl;
return;
}
}
}
int main() {
mcnuggets(100);
}
- Output:
Maximum non-McNuggets number is 43
Clojure
(defn cart [colls]
(if (empty? colls)
'(())
(for [more (cart (rest colls))
x (first colls)]
(cons x more))))
(defn nuggets [[n6 n9 n20]] (+ (* 6 n6) (* 9 n9) (* 20 n20)))
(let [possible (distinct (map nuggets (cart (map range [18 13 6]))))
mcmax (apply max (filter (fn [x] (not-any? #{x} possible)) (range 101)))]
(printf "Maximum non-McNuggets number is %d\n" mcmax))
- Output:
Maximum non-McNuggets number is 43
CLU
% Recursive nugget iterator.
% This yields all the nugget numbers of the given box sizes from start to max.
gen_nuggets = iter (start, max: int, sizes: sequence[int]) yields (int)
si = sequence[int]
if si$empty(sizes) then
yield(start)
else
for i: int in int$from_to_by(start, max, si$bottom(sizes)) do
for j: int in gen_nuggets(i, max, si$reml(sizes)) do
yield(j)
end
end
end
end gen_nuggets
start_up = proc ()
max = 100
ab = array[bool]
po: stream := stream$primary_output()
nuggets: ab := ab$fill(0,max+1,false)
for nugget: int in gen_nuggets(0, max, sequence[int]$[6,9,20]) do
nuggets[nugget] := true
end
maxn: int
for i: int in ab$indexes(nuggets) do
if ~nuggets[i] then maxn := i end
end
stream$putl(po, "Maximum non-McNuggets number: " || int$unparse(maxn))
end start_up
- Output:
Maximum non-McNuggets number: 43
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. MCNUGGETS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUGGETS.
03 NUGGET-FLAGS PIC X OCCURS 100 TIMES.
88 IS-NUGGET VALUE 'X'.
01 A PIC 999.
01 B PIC 999.
01 C PIC 999.
PROCEDURE DIVISION.
BEGIN.
MOVE SPACES TO NUGGETS.
PERFORM A-LOOP VARYING A FROM 0 BY 6
UNTIL A IS GREATER THAN 100.
MOVE 100 TO A.
FIND-LARGEST.
IF IS-NUGGET(A), SUBTRACT 1 FROM A, GO TO FIND-LARGEST.
DISPLAY 'Largest non-McNugget number: ', A.
STOP RUN.
A-LOOP.
PERFORM B-LOOP VARYING B FROM A BY 9
UNTIL B IS GREATER THAN 100.
B-LOOP.
PERFORM C-LOOP VARYING C FROM B BY 20
UNTIL C IS GREATER THAN 100.
C-LOOP.
IF C IS NOT EQUAL TO ZERO, MOVE 'X' TO NUGGET-FLAGS(C).
- Output:
Largest non-McNugget number: 043
Comal
0010 limit#:=100
0020 DIM nugget#(0:limit#)
0030 FOR a#:=0 TO limit# STEP 6 DO
0040 FOR b#:=a# TO limit# STEP 9 DO
0050 FOR c#:=b# TO limit# STEP 20 DO nugget#(c#):=TRUE
0060 ENDFOR b#
0070 ENDFOR a#
0080 FOR i#:=limit# TO 0 STEP -1 DO
0090 IF NOT nugget#(i#) THEN
0100 PRINT "Maximum non-McNuggets number: ",i#
0110 END
0120 ENDIF
0130 ENDFOR i#
- Output:
Maximum non-McNuggets number: 43
Cowgol
include "cowgol.coh";
const LIMIT := 100;
var flags: uint8[LIMIT+1];
MemZero(&flags[0], @bytesof flags);
var a: @indexof flags;
var b: @indexof flags;
var c: @indexof flags;
a := 0;
while a <= LIMIT loop
b := a;
while b <= LIMIT loop
c := b;
while c <= LIMIT loop
flags[c] := 1;
c := c + 20;
end loop;
b := b + 9;
end loop;
a := a + 6;
end loop;
a := LIMIT;
loop
if flags[a] == 0 then
print("Maximum non-McNuggets number: ");
print_i32(a as uint32);
print_nl();
break;
end if;
a := a - 1;
end loop;
- Output:
Maximum non-McNuggets number: 43
Dart
import 'dart:math';
main() {
var nuggets = List<int>.generate(101, (int index) => index);
for (int small in List<int>.generate((100 ~/ (6 + 1)), (int index) => index)) {
for (int medium in List<int>.generate((100 ~/ (9 + 1)), (int index) => index)) {
for (int large in List<int>.generate((100 ~/ (20 + 1)), (int index) => index)) {
nuggets.removeWhere((element) => element == 6 * small + 9 * medium + 20 * large);
}
}
}
print('Largest non-McNuggets number: ${nuggets.reduce(max).toString() ?? 'none'}.');
}
- Output:
Largest non-McNuggets number: 43.
Draco
proc nonrec main() void:
byte LIMIT = 100;
[LIMIT+1] bool nugget;
byte a, b, c;
for a from 0 upto LIMIT do
nugget[a] := false
od;
for a from 0 by 6 upto LIMIT do
for b from a by 9 upto LIMIT do
for c from b by 20 upto LIMIT do
nugget[c] := true
od
od
od;
a := LIMIT;
while nugget[a] do a := a - 1 od;
writeln("Maximum non-McNuggets number: ", a)
corp
- Output:
Maximum non-McNuggets number: 43
Dyalect
func mcnugget(limit) {
var sv = Array.Empty(limit + 1, false)
for s in 0^6..limit {
for n in s^9..limit {
for t in n^20..limit {
sv[t] = true
}
}
}
for i in limit^-1..0 {
if !sv[i] {
print("Maximum non-McNuggets number is \(i)")
return
}
}
}
mcnugget(100)
- Output:
Maximum non-McNuggets number is 43
EasyLang
len l[] 100
for a = 0 to 100 div 6
for b = 0 to 100 div 9
for c = 0 to 100 div 20
n = a * 6 + b * 9 + c * 20
if n >= 1 and n <= 100
l[n] = 1
.
.
.
.
for n = 100 downto 1
if l[n] = 0
print n
break 1
.
.
Elixir
Uses MapSet and Comprehension
defmodule Mcnugget do
def solve(limit) do
0..limit
|> MapSet.new()
|> MapSet.difference(
for(
x <- 0..limit,
y <- 0..limit,
z <- 0..limit,
Integer.mod(x, 6) == 0,
Integer.mod(y, 9) == 0,
Integer.mod(z, 20) == 0,
x + y + z <= limit,
into: MapSet.new(),
do: x + y + z
)
)
|> Enum.max()
end
end
Mcnugget.solve(100) |> IO.puts
- Output:
43
F#
// McNuggets. Nigel Galloway: October 28th., 2018
let fN n g = Seq.initInfinite(fun ng->ng*n+g)|>Seq.takeWhile(fun n->n<=100)
printfn "%d" (Set.maxElement(Set.difference (set[1..100]) (fN 20 0|>Seq.collect(fun n->fN 9 n)|>Seq.collect(fun n->fN 6 n)|>Set.ofSeq)))
- Output:
43
Factor
USING: backtrack kernel math.ranges prettyprint sequences sets ;
101 <iota> [ 0 6 9 20 [ 100 swap <range> amb-lazy ] tri@ ] bag-of diff last .
- Output:
43
FOCAL
01.10 F N=0,100;S T(N)=0
01.20 F A=0,6,100;F B=A,9,100;F C=B,20,100;S T(C)=-1
01.30 S N=101
01.40 S N=N-1
01.50 I (T(N))1.4
01.60 T %3,N,!
01.70 Q
- Output:
= 43
FreeBASIC
Dim As Integer l(100), a, b, c, n
For a = 0 To 100/6
For b = 0 To 100/9
For c = 0 To 100/20
n = a*6 + b*9 + c*20
If n <= 100 Then l(n) = true
Next c
Next b
Next a
For n = 100 To 1 Step -1
If l(n) = false Then Print "El mayor número que no sea McNugget es:"; n: Exit For
Next n
End
- Output:
El mayor número que no sea McNugget es: 43
Frink
This is a nice demonstration for Frink's multifor
loop which can perform arbitrarily-deeply-nested loops in a single statement. The "inner" (rightmost) loops can use values set by the "outer" (leftmost) as part of their bounds.
a = toSet[0 to 100]
multifor [z,y,x] = [0 to 100 step 20, 0 to 100-z step 9, 0 to 100-z-y step 6]
a.remove[x+y+z]
println[max[a]]
- Output:
43
FutureBasic
local fn McNuggetsProblem
BOOL l(100)
long a, b, c, i, n
for a = 0 to 100/6
for b = 0 to 100/9
for c = 0 to 100/20
n = a * 6 + b * 9 + c * 20
if n <= 100 then l(n) = YES
next
next
next
for i = 100 to 1 step -1
if l(i) == NO then print "The maximum non-McNuggets number less than 100 is: "; i: exit for
next
end fn
window 1,,( 0, 0, 450, 100 )
fn McNuggetsProblem
HandleEvents
- Output:
The maximum non-McNuggets number less than 100 is: 43
Go
package main
import "fmt"
func mcnugget(limit int) {
sv := make([]bool, limit+1) // all false by default
for s := 0; s <= limit; s += 6 {
for n := s; n <= limit; n += 9 {
for t := n; t <= limit; t += 20 {
sv[t] = true
}
}
}
for i := limit; i >= 0; i-- {
if !sv[i] {
fmt.Println("Maximum non-McNuggets number is", i)
return
}
}
}
func main() {
mcnugget(100)
}
- Output:
Maximum non-McNuggets number is 43
Haskell
import Data.Set (Set, fromList, member)
------------------------ MCNUGGETS -----------------------
mcNuggets :: Set Int
mcNuggets =
let size = enumFromTo 0 . quot 100
in fromList $
size 6
>>= \x ->
size 9
>>= \y ->
size 20
>>= \z ->
[ v
| let v = sum [6 * x, 9 * y, 20 * z],
101 > v
]
--------------------------- TEST -------------------------
main :: IO ()
main =
(putStrLn . go) $
dropWhile (`member` mcNuggets) [100, 99 .. 1]
where
go (x : _) = show x
go [] = "No unreachable quantities found ..."
Or equivalently, making use of the list comprehension notation:
import Data.Set (Set, fromList, member)
gaps :: [Int]
gaps = dropWhile (`member` mcNuggets) [100,99 .. 1]
mcNuggets :: Set Int
mcNuggets =
let size n = [0 .. quot 100 n]
in fromList
[ v
| x <- size 6
, y <- size 9
, z <- size 20
, let v = sum [6 * x, 9 * y, 20 * z]
, 101 > v ]
main :: IO ()
main =
print $
case gaps of
x:_ -> show x
[] -> "No unreachable quantities found ..."
43
J
Brute force solution: calculate all pure (just one kind of box) McNugget numbers which do not exceed 100, then compute all possible sums, and then remove those from the list of numbers up to 100 (which is obviously a McNugget number), then find the largest number remaining:
>./(i.100)-.,+/&>{(* i.@>.@%~&101)&.>6 9 20
43
Technically, we could have used 100 in place of 101 when we were finding how many pure McNugget numbers were in each series (because 100 is obviously a McNugget number), but it's not like that's a problem, either.
Java
public class McNuggets {
public static void main(String... args) {
int[] SIZES = new int[] { 6, 9, 20 };
int MAX_TOTAL = 100;
// Works like Sieve of Eratosthenes
int numSizes = SIZES.length;
int[] counts = new int[numSizes];
int maxFound = MAX_TOTAL + 1;
boolean[] found = new boolean[maxFound];
int numFound = 0;
int total = 0;
boolean advancedState = false;
do {
if (!found[total]) {
found[total] = true;
numFound++;
}
// Advance state
advancedState = false;
for (int i = 0; i < numSizes; i++) {
int curSize = SIZES[i];
if ((total + curSize) > MAX_TOTAL) {
// Reset to zero and go to the next box size
total -= counts[i] * curSize;
counts[i] = 0;
}
else {
// Adding a box of this size still keeps the total at or below the maximum
counts[i]++;
total += curSize;
advancedState = true;
break;
}
}
} while ((numFound < maxFound) && advancedState);
if (numFound < maxFound) {
// Did not find all counts within the search space
for (int i = MAX_TOTAL; i >= 0; i--) {
if (!found[i]) {
System.out.println("Largest non-McNugget number in the search space is " + i);
break;
}
}
}
else {
System.out.println("All numbers in the search space are McNugget numbers");
}
return;
}
}
- Output:
Largest non-McNugget number in the search space is 43
JavaScript
(() => {
'use strict';
// main :: IO ()
const main = () => {
const
size = n => enumFromTo(0)(
quot(100, n)
),
nuggets = new Set(
size(6).flatMap(
x => size(9).flatMap(
y => size(20).flatMap(
z => {
const v = sum([6 * x, 9 * y, 20 * z]);
return 101 > v ? (
[v]
) : [];
}
),
)
)
),
xs = dropWhile(
x => nuggets.has(x),
enumFromThenTo(100, 99, 1)
);
return 0 < xs.length ? (
xs[0]
) : 'No unreachable quantities found in this range';
};
// GENERIC FUNCTIONS ----------------------------------
// dropWhile :: (a -> Bool) -> [a] -> [a]
const dropWhile = (p, xs) => {
const lng = xs.length;
return 0 < lng ? xs.slice(
until(
i => i === lng || !p(xs[i]),
i => 1 + i,
0
)
) : [];
};
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo = (x1, x2, y) => {
const d = x2 - x1;
return Array.from({
length: Math.floor(y - x2) / d + 2
}, (_, i) => x1 + (d * i));
};
// ft :: Int -> Int -> [Int]
const enumFromTo = m => n =>
Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// quot :: Int -> Int -> Int
const quot = (n, m) => Math.floor(n / m);
// sum :: [Num] -> Num
const sum = xs => xs.reduce((a, x) => a + x, 0);
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
let v = x;
while (!p(v)) v = f(v);
return v;
};
// MAIN ---
return console.log(
main()
);
})();
- Output:
43
jq
[
[range(18) as $n6 |
range(13) as $n9 |
range(6) as $n20 |
($n6 * 6 + $n9 * 9 + $n20 * 20)] |
unique |
. as $possible |
range(101) |
. as $n |
select($possible|contains([$n])|not)
] |
max
- Output:
43
Julia
Simple brute force solution, though the BitSet would save memory considerably with larger max numbers.
function mcnuggets(max)
b = BitSet(1:max)
for i in 0:6:max, j in 0:9:max, k in 0:20:max
delete!(b, i + j + k)
end
maximum(b)
end
println(mcnuggets(100))
- Output:
43
Kotlin
// Version 1.2.71
fun mcnugget(limit: Int) {
val sv = BooleanArray(limit + 1) // all false by default
for (s in 0..limit step 6)
for (n in s..limit step 9)
for (t in n..limit step 20) sv[t] = true
for (i in limit downTo 0) {
if (!sv[i]) {
println("Maximum non-McNuggets number is $i")
return
}
}
}
fun main(args: Array<String>) {
mcnugget(100)
}
- Output:
Maximum non-McNuggets number is 43
Locomotive Basic
100 CLEAR
110 DIM a(100)
120 FOR a=0 TO 100/6
130 FOR b=0 TO 100/9
140 FOR c=0 TO 100/20
150 n=a*6+b*9+c*20
160 IF n<=100 THEN a(n)=1
170 NEXT c
180 NEXT b
190 NEXT a
200 FOR n=0 TO 100
210 IF a(n)=0 THEN l=n
220 NEXT n
230 PRINT"The Largest non McNugget number is:";l
240 END
- Output:
The largest non McNugget number is: 43
Lua
function range(A,B)
return function()
return coroutine.wrap(function()
for i = A, B do coroutine.yield(i) end
end)
end
end
function filter(stream, f)
return function()
return coroutine.wrap(function()
for i in stream() do
if f(i) then coroutine.yield(i) end
end
end)
end
end
function triple(s1, s2, s3)
return function()
return coroutine.wrap(function()
for x in s1() do
for y in s2() do
for z in s3() do
coroutine.yield{x,y,z}
end
end
end
end)
end
end
function apply(f, stream)
return function()
return coroutine.wrap(function()
for T in stream() do
coroutine.yield(f(table.unpack(T)))
end
end)
end
end
function exclude(s1, s2)
local exlusions = {} for x in s1() do exlusions[x] = true end
return function()
return coroutine.wrap(function()
for x in s2() do
if not exlusions[x] then
coroutine.yield(x)
end
end
end)
end
end
function maximum(stream)
local M = math.mininteger
for x in stream() do
M = math.max(M, x)
end
return M
end
local N = 100
local A, B, C = 6, 9, 20
local Xs = filter(range(0, N), function(x) return x % A == 0 end)
local Ys = filter(range(0, N), function(x) return x % B == 0 end)
local Zs = filter(range(0, N), function(x) return x % C == 0 end)
local sum = filter(apply(function(x, y, z) return x + y + z end, triple(Xs, Ys, Zs)), function(x) return x <= N end)
print(maximum(exclude(sum, range(1, N))))
- Output:
43
MACRO-11
.TITLE NUGGET
.MCALL .TTYOUT,.EXIT
NUGGET::MOV #^D50,R1
MOV #NUGBUF,R0
CLEAR: CLR (R0)+ ; CLEAR BUFFER
SOB R1,CLEAR
MARK: MOV #^D100,R5 ; R5 = LIMIT
CLR R0 ; R0 = 6 STEPPER
1$: MOV R0,R1 ; R1 = 9 STEPPER
2$: MOV R1,R2 ; R2 = 20 STEPPER
3$: INCB NUGBUF(R2) ; MARK
ADD #^D20,R2 ; 20 STEP
CMP R2,R5
BLT 3$
ADD #^D9,R1 ; 9 STEP
CMP R1,R5
BLT 2$
ADD #^D6,R0 ; 6 STEP
CMP R0,R5
BLT 1$
SCAN: MOV #NUGBUF+^D100,R0
1$: DEC R5
MOVB -(R0),R1
BNE 1$
DIGIT: MOV #'0-1,R0 ; SPLIT DIGITS
1$: INC R0
SUB #^D10,R5
BCC 1$
.TTYOUT ; HIGH DIGIT
MOV R5,R0
ADD #'0+^D10,R0
.TTYOUT ; LOW DIGIT
.EXIT
NUGBUF: .BLKB ^D100
.END NUGGET
- Output:
43
MAD
NORMAL MODE IS INTEGER
BOOLEAN NUGGET
DIMENSION NUGGET(101)
THROUGH CLEAR, FOR I=0, 1, I.G.100
CLEAR NUGGET(I) = 0B
THROUGH MARK, FOR A=0, 6, A.G.100
THROUGH MARK, FOR B=A, 9, B.G.100
THROUGH MARK, FOR C=B, 20, C.G.100
MARK NUGGET(C) = 1B
SEARCH THROUGH SEARCH, FOR I=100, -1, .NOT.NUGGET(I)
PRINT FORMAT F, I
VECTOR VALUES F = $29HMAXIMUM NON-MCNUGGET NUMBER: ,I2*$
END OF PROGRAM
- Output:
MAXIMUM NON-MCNUGGET NUMBER: 43
Mathematica/Wolfram Language
FrobeniusNumber[{6, 9, 20}]
- Output:
43
Modula-2
MODULE McNuggets;
FROM InOut IMPORT WriteCard, WriteString, WriteLn;
CONST Max = 100;
VAR a, b, c: CARDINAL;
nugget: ARRAY [0..Max] OF BOOLEAN;
BEGIN
FOR a := 0 TO Max DO
nugget[a] := FALSE;
END;
FOR a := 0 TO Max BY 6 DO
FOR b := a TO Max BY 9 DO
FOR c := b TO Max BY 20 DO
nugget[c] := TRUE;
END;
END;
END;
a := 100;
REPEAT DEC(a); UNTIL NOT nugget[a];
WriteString("Maximum non-McNuggets number: ");
WriteCard(a, 2);
WriteLn();
END McNuggets.
- Output:
Maximum non-McNuggets number: 43
MiniScript
n = range(0, 100)
for six in range(0, 100, 6)
for nine in range(0, 100, 9)
for twenty in range(0, 100, 20)
mcnuggets = six + nine + twenty
ix = n.indexOf(mcnuggets)
if ix != null then n.remove(ix)
end for
end for
end for
print "The largest non-McNugget number is " + n[-1]
- Output:
The largest non-McNugget number is 43
MiniZinc
%McNuggets. Nigel Galloway, August 27th., 2019
var 0..99: n;
constraint forall(x in 0..16,y in 0..11,z in 0..5)(6*x + 9*y + 20*z!=n);
solve maximize n;
output [show(n)]
- Output:
43 ---------- ==========
Nim
const Limit = 100
var mcnuggets: array[0..Limit, bool]
for a in countup(0, Limit, 6):
for b in countup(a, Limit, 9):
for c in countup(b, Limit, 20):
mcnuggets[c] = true
for n in countdown(Limit, 0):
if not mcnuggets[n]:
echo "The largest non-McNuggets number is: ", n
break
- Output:
The largest non-McNuggets number is: 43
Pascal
A console program in Free Pascal. Same idea as the Raku solution, but without generalizing. We stop once we've found 6 consecutive integers that can be represented.
program McNuggets;
{$mode objfpc}{$H+}
const
ARRAY_SIZE_STEP = 20; // small, to demonstrate extending array dynamically
var
i, nr_consec : integer;
can_do : array of boolean;
begin
SetLength( can_do, ARRAY_SIZE_STEP);
can_do[0] := true;
nr_consec := 0;
i := 0;
repeat
inc(i);
if i >= Length( can_do) then SetLength( can_do, i + ARRAY_SIZE_STEP);
can_do[i] := ((i >= 6) and can_do[i - 6])
or ((i >= 9) and can_do[i - 9])
or ((i >= 20) and can_do[i - 20]);
if can_do[i] then begin
if can_do[i - 1] then inc( nr_consec)
else nr_consec := 1;
end
else nr_consec := 0;
until nr_consec = 6;
WriteLn ('Max that can''t be represented is ', i - 6);
end.
- Output:
Max that can't be represented is 43
Perl
use ntheory qw/forperm gcd vecmin/;
sub Mcnugget_number {
my $counts = shift;
return 'No maximum' if 1 < gcd @$counts;
my $min = vecmin @$counts;
my @meals;
my @min;
my $a = -1;
while (1) {
$a++;
for my $b (0..$a) {
for my $c (0..$b) {
my @s = ($a, $b, $c);
forperm {
$meals[
$s[$_[0]] * $counts->[0]
+ $s[$_[1]] * $counts->[1]
+ $s[$_[2]] * $counts->[2]
] = 1;
} @s;
}
}
for my $i (0..$#meals) {
next unless $meals[$i];
if ($min[-1] and $i == ($min[-1] + 1)) {
push @min, $i;
last if $min == @min
} else {
@min = $i;
}
}
last if $min == @min
}
$min[0] ? $min[0] - 1 : 0
}
for my $counts ([6,9,20], [6,7,20], [1,3,20], [10,5,18], [5,17,44], [2,4,6], [3,6,15]) {
print 'Maximum non-Mcnugget number using ' . join(', ', @$counts) . ' is: ' . Mcnugget_number($counts) . "\n"
}
- Output:
Maximum non-Mcnugget number using 6, 9, 20 is: 43 Maximum non-Mcnugget number using 6, 7, 20 is: 29 Maximum non-Mcnugget number using 1, 3, 20 is: 0 Maximum non-Mcnugget number using 10, 5, 18 is: 67 Maximum non-Mcnugget number using 5, 17, 44 is: 131 Maximum non-Mcnugget number using 2, 4, 6 is: No maximum Maximum non-Mcnugget number using 3, 6, 15 is: No maximum
Perl using Regex
use strict;
use warnings;
$_ = 1 . 0 x 100;
1 while s/ (?=1) (?:.{6}|.{9}|.{20}) \K 0 /1/x;
/01*$/ and print "Maximum non-Mcnugget number is: $-[0]\n";
- Output:
Maximum non-Mcnugget number is: 43
Phix
with javascript_semantics constant limit=100 sequence nuggets = repeat(false,limit+1) for sixes=0 to limit by 6 do for nines=sixes to limit by 9 do for twenties=nines to limit by 20 do nuggets[twenties+1] = true end for end for end for printf(1,"Maximum non-McNuggets number is %d\n", rfind(false,nuggets)-1)
- Output:
Maximum non-McNuggets number is 43
Also, since it is a bit more interesting, a
with javascript_semantics function Mcnugget_number(sequence counts) if gcd(counts)>1 then return "No maximum" end if atom cmin = min(counts) sequence meals = {} sequence smin = {} integer a = -1 while true do a += 1 for b=0 to a do for c=0 to b do sequence s = {a, b, c} for i=1 to factorial(3) do sequence p = permute(i,s) integer k = sum(sq_mul(p,counts))+1 -- atom k = sum(sq_mul(p,counts))+1 if k>length(meals) then meals &= repeat(0,k-length(meals)) end if meals[k] = 1 end for end for end for for i=1 to length(meals) do if meals[i] then if length(smin) and smin[$]+1=i-1 then smin = append(smin,i-1) if length(smin)=cmin then exit end if else smin = {i-1} end if end if end for if length(smin)=cmin then exit end if end while return sprintf("%d",iff(smin[1]?smin[1]-1:0)) end function constant tests = {{6,9,20}, {6,7,20}, {1,3,20}, {10,5,18}, {5,17,44}, {2,4,6}, {3,6,15}} for i=1 to length(tests) do sequence ti = tests[i] printf(1,"Maximum non-Mcnugget number using %V is: %s\n",{ti,Mcnugget_number(ti)}) end for
- Output:
Maximum non-Mcnugget number using {6,9,20} is: 43 Maximum non-Mcnugget number using {6,7,20} is: 29 Maximum non-Mcnugget number using {1,3,20} is: 0 Maximum non-Mcnugget number using {10,5,18} is: 67 Maximum non-Mcnugget number using {5,17,44} is: 131 Maximum non-Mcnugget number using {2,4,6} is: No maximum Maximum non-Mcnugget number using {3,6,15} is: No maximum
Picat
Using constraint modelling (cp solver).
import cp.
go =>
N :: 0..100,
foreach(X in 0..16, Y in 0..11, Z in 0..5)
6*X + 9*Y + 20*Z #!= N
end,
solve($[max(N)],N),
println(n=N).
- Output:
n = 43
PicoLisp
(de nuggets1 (M)
(let Lst (range 0 M)
(for A (range 0 M 6)
(for B (range A M 9)
(for C (range B M 20)
(set (nth Lst (inc C))) ) ) )
(apply max Lst) ) )
Generator from fiber:
(de nugg (M)
(co 'nugget
(for A (range 0 M 6)
(for B (range A M 9)
(for C (range B M 20)
(yield (inc C)) ) ) ) ) )
(de nuggets2 (M)
(let Lst (range 0 M)
(while (nugg 100)
(set (nth Lst @)) )
(apply max Lst) ) )
Test versions against each other:
(test
T
(=
43
(nuggets1 100)
(nuggets2 100) ) )
PL/I
mcnugget: procedure options(main);
declare nugget(0:100) bit, (a, b, c) fixed;
do a=0 to 100; nugget(a) = '0'b; end;
do a=0 to 100 by 6;
do b=a to 100 by 9;
do c=b to 100 by 20;
nugget(c) = '1'b;
end;
end;
end;
do a=100 to 0 by -1;
if ^nugget(a) then do;
put skip list('Maximum non-McNuggets number:', a);
stop;
end;
end;
end mcnugget;
- Output:
Maximum non-McNuggets number: 43
PL/M
100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9, S); END PRINT;
PRINT$NUMBER: PROCEDURE (N);
DECLARE S (6) BYTE INITIAL ('...',13,10,'$');
DECLARE P ADDRESS, (N, C BASED P) BYTE;
P = .S(3);
DIGIT:
P = P-1;
C = N MOD 10 + '0';
N = N/10;
IF N>0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUMBER;
DECLARE (A, B, C) BYTE;
DECLARE NUGGET (101) BYTE;
DO A=0 TO 100; NUGGET(A) = 0; END;
DO A=0 TO 100 BY 6;
DO B=A TO 100 BY 9;
DO C=B TO 100 BY 20;
NUGGET(C) = -1;
END;
END;
END;
A = 100;
DO WHILE NUGGET(A); A = A-1; END;
CALL PRINT$NUMBER(A);
CALL EXIT;
EOF
- Output:
43
PowerShell
$possible = @{}
For ($i=0; $i -lt 18; $i++) {
For ($j=0; $j -lt 13; $j++) {
For ( $k=0; $k -lt 6; $k++ ) {
$possible[ $i*6 + $j*9 + $k*20 ] = $true
}
}
}
For ( $n=100; $n -gt 0; $n-- ) {
If ($possible[$n]) {
Continue
}
Else {
Break
}
}
Write-Host "Maximum non-McNuggets number is $n"
- Output:
Maximum non-McNuggets number is 43
Python
Python: REPL
It's a simple solution done on the command line:
>>> from itertools import product
>>> nuggets = set(range(101))
>>> for s, n, t in product(range(100//6+1), range(100//9+1), range(100//20+1)):
nuggets.discard(6*s + 9*n + 20*t)
>>> max(nuggets)
43
>>>
Single expression version (expect to be slower, however no noticeable difference on a Celeron B820 and haven't benchmarked):
>>> from itertools import product
>>> max(x for x in range(100+1) if x not in
... (6*s + 9*n + 20*t for s, n, t in
... product(range(100//6+1), range(100//9+1), range(100//20+1))))
43
>>>
Using Set Comprehension
#Wherein I observe that Set Comprehension is not intrinsically dysfunctional. Nigel Galloway: October 28th., 2018
n = {n for x in range(0,101,20) for y in range(x,101,9) for n in range(y,101,6)}
g = {n for n in range(101)}
print(max(g.difference(n)))
- Output:
43
List monad
A composition of pure functions, including dropwhile, which shows a more verbose and unwieldy (de-sugared) route to list comprehension, and reveals the underlying mechanics of what the (compact and elegant) built-in syntax expresses. May help to build intuition for confident use of the latter.
Note that the innermost function wraps its results in a (potentially empty) list. The resulting list of lists, some empty, is then flattened by the concatenation component of bind.
'''mcNuggets list monad'''
from itertools import (chain, dropwhile)
# mcNuggetsByListMonad :: Int -> Set Int
def mcNuggetsByListMonad(limit):
'''McNugget numbers up to limit.'''
box = size(limit)
return set(
bind(
box(6)
)(lambda x: bind(
box(9)
)(lambda y: bind(
box(20)
)(lambda z: (
lambda v=sum([x, y, z]): (
[] if v > limit else [v]
)
)())))
)
# Which, for comparison, is equivalent to:
# mcNuggetsByComprehension :: Int -> Set Int
def mcNuggetsByComprehension(limit):
'''McNuggets numbers up to limit'''
box = size(limit)
return {
v for v in (
sum([x, y, z])
for x in box(6)
for y in box(9)
for z in box(20)
) if v <= limit
}
# size :: Int -> Int -> [Int]
def size(limit):
'''Multiples of n up to limit.'''
return lambda n: enumFromThenTo(0)(n)(limit)
# -------------------------- TEST --------------------------
def main():
'''List monad and set comprehension - parallel routes'''
def test(limit):
def go(nuggets):
ys = list(dropwhile(
lambda x: x in nuggets,
enumFromThenTo(limit)(limit - 1)(1)
))
return str(ys[0]) if ys else (
'No unreachable targets in this range.'
)
return lambda nuggets: go(nuggets)
def fName(f):
return f.__name__
limit = 100
print(
fTable(main.__doc__ + ':\n')(fName)(test(limit))(
lambda f: f(limit)
)([mcNuggetsByListMonad, mcNuggetsByComprehension])
)
# ------------------------ GENERIC -------------------------
# bind (>>=) :: [a] -> (a -> [b]) -> [b]
def bind(xs):
'''List monad injection operator.
Two computations sequentially composed,
with any value produced by the first
passed as an argument to the second.
'''
return lambda f: chain.from_iterable(
map(f, xs)
)
# enumFromThenTo :: Int -> Int -> Int -> [Int]
def enumFromThenTo(m):
'''Integer values enumerated from m to n
with a step defined by nxt-m.
'''
def go(nxt, n):
d = nxt - m
return range(m, n - 1 if d < 0 else 1 + n, d)
return lambda nxt: lambda n: go(nxt, n)
# ------------------------ DISPLAY -------------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def gox(xShow):
def gofx(fxShow):
def gof(f):
def goxs(xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
def arrowed(x, y):
return y.rjust(w, ' ') + ' -> ' + fxShow(f(x))
return s + '\n' + '\n'.join(
map(arrowed, xs, ys)
)
return goxs
return gof
return gofx
return gox
# MAIN ---
if __name__ == '__main__':
main()
- Output:
List monad and set comprehension - parallel routes: mcNuggetsByListMonad -> 43 mcNuggetsByComprehension -> 43
Quackery
0 temp put
100 6 / times
[ i 6 *
100 9 / times
[ dup i 9 * +
100 20 / times
[ dup i 20 * +
dup 101 < if
[ dup bit
temp take | temp put ]
drop ]
drop ]
drop ]
-1 temp take
101 times
[ dup i bit & 0 =
if
[ nip i swap
conclude ] ]
drop dup 0 < iff
[ drop
say "There are no non-McNugget numbers below 101" ]
else
[ say "The largest non-McNugget number below 101 is "
echo ]
char . emit
Output:
The largest non-McNugget number below 101 is 43.
R
Assuming that the natural numbers start at 0.
There are two natural approaches. The first is to generate all valid x, y, and z and then apply the function:
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)
The second is to find all of the valid 6x, 9y, and 20z, and then sum them:
mcNuggets2 <- rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20)))
Either way, we get identical results, as checked by:
all(mcNuggets == mcNuggets2)
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.
results <- setdiff(0:100, mcNuggets)
cat("The non-McNuggets numbers that are no greater than 100 are:", results, "\nThe largest is", max(results), "\n")
Ultimately, this can be done in one line:
max(setdiff(0:100, rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20)))))
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:
> all(mcNuggets == mcNuggets2) [1] TRUE
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
> max(setdiff(0:100, rowSums(expand.grid(seq(0, 100, 6), seq(0, 100, 9), seq(0, 100, 20))))) [1] 43
Racket
(one of them)
#lang racket
(apply max (set->list (for*/fold ((s (list->set (range 1 101))))
((x (in-range 0 101 20))
(y (in-range x 101 9))
(n (in-range y 101 6)))
(set-remove s n))))
Raku
(formerly Perl 6)
No hard coded limits, no hard coded values. General purpose 3 value solver. Count values may be any 3 different positive integers, in any order, that are relatively prime.
Finds the smallest count value, then looks for the first run of consecutive count totals able to be generated, that is at least the length of the smallest count size. From then on, every number can be generated by simply adding multiples of the minimum count to each of the totals in that run.
sub Mcnugget-number (*@counts) {
return '∞' if 1 < [gcd] @counts;
my $min = min @counts;
my @meals;
my @min;
for ^Inf -> $a {
for 0..$a -> $b {
for 0..$b -> $c {
($a, $b, $c).permutations.map: { @meals[ sum $_ Z* @counts ] = True }
}
}
for @meals.grep: so *, :k {
if @min.tail and @min.tail + 1 == $_ {
@min.push: $_;
last if $min == +@min
} else {
@min = $_;
}
}
last if $min == +@min
}
@min[0] ?? @min[0] - 1 !! 0
}
for (6,9,20), (6,7,20), (1,3,20), (10,5,18), (5,17,44), (2,4,6), (3,6,15) -> $counts {
put "Maximum non-Mcnugget number using {$counts.join: ', '} is: ",
Mcnugget-number(|$counts)
}
- Output:
Maximum non-Mcnugget number using 6, 9, 20 is: 43 Maximum non-Mcnugget number using 6, 7, 20 is: 29 Maximum non-Mcnugget number using 1, 3, 20 is: 0 Maximum non-Mcnugget number using 10, 5, 18 is: 67 Maximum non-Mcnugget number using 5, 17, 44 is: 131 Maximum non-Mcnugget number using 2, 4, 6 is: ∞ Maximum non-Mcnugget number using 3, 6, 15 is: ∞
Refal
$ENTRY Go {
, <Iota 0 6 100>: e.A
, <Iota 0 9 100>: e.B
, <Iota 0 20 100>: e.C
, <Iota 0 1 100>: e.Nums
, <SumPairs (e.A) (<SumPairs (e.B) (e.C)>)>: e.Nuggets
, <RemoveAll (e.Nuggets) e.Nums>: e.NonNuggets
, e.NonNuggets: e.X s.Last
= <Prout 'The largest non-McNuggets number < 100 is: ' s.Last>;
};
SumPairs {
() (e.Y) = ;
(s.I e.X) (e.Y) = <SumPairs1 s.I (e.Y)> <SumPairs (e.X) (e.Y)>;
};
SumPairs1 {
s.I () = ;
s.I (s.X e.X) = <+ s.I s.X> <SumPairs1 s.I (e.X)>;
};
Remove {
s.I e.X s.I e.Y = e.X <Remove s.I e.Y>;
s.I e.X = e.X;
};
RemoveAll {
() e.X = e.X;
(s.R e.R) e.X = <RemoveAll (e.R) <Remove s.R e.X>>;
};
Iota {
s.Start s.Step s.End, <Compare s.Start s.End>: {
'+' = ;
s.X = s.Start <Iota <+ s.Start s.Step> s.Step s.End>;
};
};
- Output:
The largest non-McNuggets number < 100 is: 43
REXX
This REXX version generalizes the problem (does not depend on fixed meal sizes), and also checks for:
- a meal that doesn't include McNuggets (in other words, zero nuggets)
- a meal size that includes a double order of nuggets
- a meal size that includes a single nugget (which means, no largest McNugget number)
- excludes meals that have a multiple order of nuggets
- automatically computes the high value algebraically instead of using 100.
/*REXX pgm solves the McNuggets problem: the largest McNugget number for given meals. */
parse arg y /*obtain optional arguments from the CL*/
if y='' | y="," then y= 6 9 20 /*Not specified? Then use the defaults*/
say 'The number of McNuggets in the serving sizes of: ' space(y)
$=
#= 0 /*the Y list must be in ascending order*/
z=.
do j=1 for words(y); _= word(y, j) /*examine Y list for dups, neg, zeros*/
if _==1 then signal done /*Value ≡ 1? Then all values possible.*/
if _<1 then iterate /*ignore zero and negative # of nuggets*/
if wordpos(_, $)\==0 then iterate /*search for duplicate values. */
do k=1 for # /* " " multiple " */
if _//word($,k)==0 then iterate j /*a multiple of a previous value, skip.*/
end /*k*/
$= $ _; #= # + 1; $.#= _ /*add─►list; bump counter; assign value*/
end /*j*/
if #<2 then signal done /*not possible, go and tell bad news. */
_= gcd($) if _\==1 then signal done /* " " " " " " " */
if #==2 then z= $.1 * $.2 - $.1 - $.2 /*special case, construct the result. */
if z\==. then signal done
h= 0 /*construct a theoretical high limit H.*/
do j=2 for #-1; _= j-1; _= $._; h= max(h, _ * $.j - _ - $.j)
end /*j*/
@.=0
do j=1 for #; _= $.j /*populate the Jth + Kth summand. */
do a=_ by _ to h; @.a= 1 /*populate every multiple as possible. */
end /*s*/
do k=1 for h; if \@.k then iterate
s= k + _; @.s= 1 /*add two #s; mark as being possible.*/
end /*k*/
end /*j*/
do z=h by -1 for h until \@.z /*find largest integer not summed. */
end /*z*/
say
done: if z==. then say 'The largest McNuggets number not possible.'
else say 'The largest McNuggets number is: ' z
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gcd: procedure; $=; do j=1 for arg(); $=$ arg(j); end; $= space($)
parse var $ x $; x= abs(x);
do while $\==''; parse var $ y $; y= abs(y); if y==0 then iterate
do until y==0; parse value x//y y with y x; end
end; return x
- output when using the default inputs:
The number of McNuggets in the serving sizes of: 6 9 20 The largest McNuggets number is: 43
Ring
Nuggets = list(100)
for six = 0 To 100/6
for nine = 0 To 100/9
for twenty = 0 To 100/20
n = six*6 + nine*9 + twenty*20
If n <= 100 and not (six = 0 and nine = 0 and twenty = 0)
Nuggets[n] = true
ok
next
next
next
for n = 100 to 1 step -1
if Nuggets[n] = false
? "Maximum non-McNuggets number is: " + n
exit
ok
next
- Output:
Maximum non-McNuggets number is: 43
RPL
« → limit
« { } limit 1 + + 0 CON
0 limit FOR s
s limit FOR n
n limit FOR t
t 1 + 1 PUT
20 STEP
9 STEP
6 STEP
limit
WHILE DUP2 GET REPEAT 1 - END
1 + SWAP DROP
» » 'MCNUGTS' STO
We can tweak a little bit the above traduction, to benefit from latest efficient built-in functions:
« → limit
« 0 limit NDUPN →LIST
0 limit FOR s
s limit FOR n
n limit FOR t
limit t - 1 + 1 PUT
20 STEP
9 STEP
6 STEP
0 POS limit SWAP - 1 +
» » 'MCNUGTS' STO
100 MCNUGTS
- Output:
1: 43
Ruby
def mcnugget(limit)
sv = (0..limit).to_a
(0..limit).step(6) do |s|
(0..limit).step(9) do |n|
(0..limit).step(20) do |t|
sv.delete(s + n + t)
end
end
end
sv.max
end
puts(mcnugget 100)
- Output:
43
Generic solution, allowing for more or less then 3 portion-sizes:
limit = 100
nugget_portions = [6, 9, 20]
arrs = nugget_portions.map{|n| 0.step(limit, n).to_a }
hits = arrs.pop.product(*arrs).map(&:sum)
p ((0..limit).to_a - hits).max # => 43
Rust
No hard limits. Generalization of Rødseth’s Algorithm explained in post. Working code: Rust playground.
fn main() {
let test_cases = vec![
[6, 9, 20],
[12, 14, 17],
[12, 13, 34],
[5, 9, 21],
[10, 18, 21],
[71, 98, 99],
[7_074_047, 8_214_596, 9_098_139],
[582_795_988, 1_753_241_221, 6_814_151_015],
[4, 30, 16],
[12, 12, 13],
[6, 15, 1],
];
for case in &test_cases {
print!("g({}, {}, {}) = ", case[0], case[1], case[2]);
println!(
"{}",
match frobenius(case.to_vec()) {
Ok(g) => format!("{}", g),
Err(e) => e,
}
);
}
}
fn frobenius(unsorted_a: Vec<i64>) -> Result<i64, String> {
let mut a = unsorted_a;
a.sort();
assert!(a[0] >= 1);
if gcd(gcd(a[0], a[1]), a[2]) > 1 {
return Err("Undefined".to_string());
}
let d12 = gcd(a[0], a[1]);
let d13 = gcd(a[0] / d12, a[2]);
let d23 = gcd(a[1] / d12, a[2] / d13);
let mut a_prime = vec![a[0] / d12 / d13, a[1] / d12 / d23, a[2] / d13 / d23];
a_prime.sort();
let rod = if a_prime[0] == 1 {
-1
} else {
// Rødseth’s Algorithm
let mut a1 = a_prime[0];
let mut s0 = congruence(a_prime[1], a_prime[2], a_prime[0]);
let mut s = vec![a1];
let mut q: Vec<i64> = vec![];
while s0 != 0 {
s.push(s0);
let s1 = if s0 == 1 { 0 } else { s0 - (a1 % s0) };
let q1 = (a1 + s1) / s0;
q.push(q1);
a1 = s0;
s0 = s1;
}
let mut p = vec![0, 1];
let mut r = (s[1] * a_prime[1] - p[1] * a_prime[2]) / a_prime[0];
let mut i = 1;
while r > 0 {
let p_next = q[i - 1] * p[i] - p[i - 1];
p.push(p_next);
r = (s[i + 1] * a_prime[1] - p_next * a_prime[2]) / a_prime[0];
i += 1;
}
let v = i - 1;
-a_prime[0] + a_prime[1] * (s[v] - 1) + a_prime[2] * (p[v + 1] - 1)
- (a_prime[1] * s[v + 1]).min(a_prime[2] * p[v])
};
Ok(rod * d12 * d13 * d23 + a[0] * (d23 - 1) + a[1] * (d13 - 1) + a[2] * (d12 - 1))
}
fn gcd(a: i64, b: i64) -> i64 {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
fn congruence(a: i64, c: i64, m: i64) -> i64 {
// Solves ax ≡ c mod m
let aa = a % m;
let cc = (c + a * m) % m;
if aa == 1 {
cc
} else {
let y = congruence(m, -cc, aa);
(m * y + cc) / aa
}
}
- Output:
g(6, 9, 20) = 43 g(12, 14, 17) = 61 g(12, 13, 34) = 79 g(5, 9, 21) = 22 g(10, 18, 21) = 65 g(71, 98, 99) = 1307 g(7074047, 8214596, 9098139) = 48494282357 g(582795988, 1753241221, 6814151015) = 173685179295403 g(4, 30, 16) = Undefined g(12, 12, 13) = 131 g(6, 15, 1) = -1
SETL
program mcnuggets;
nuggets := +/+/ {{{ x + y + z
: x in [0, 6..100] }
: y in [0, 9..100] }
: z in [0, 20..100] };
print(max/{n : n in [1..100] | not n in nuggets});
end program;
- Output:
43
Swift
func maxNugget(limit: Int) -> Int {
var (max, sixes, nines, twenties, i) = (0, 0, 0, 0, 0)
mainLoop: while i < limit {
sixes = 0
while sixes * 6 < i {
if sixes * 6 == i {
i += 1
continue mainLoop
}
nines = 0
while nines * 9 < i {
if sixes * 6 + nines * 9 == i {
i += 1
continue mainLoop
}
twenties = 0
while twenties * 20 < i {
if sixes * 6 + nines * 9 + twenties * 20 == i {
i += 1
continue mainLoop
}
twenties += 1
}
nines += 1
}
sixes += 1
}
max = i
i += 1
}
return max
}
print(maxNugget(limit: 100))
- Output:
43
Tailspin
templates largestNonMcNuggetNumber
@: { largest: 0, mcNuggetNumbers: [1..$+20 -> 0] };
@.mcNuggetNumbers([6,9,20]): 1..3 -> 1;
1..$ -> #
$@.largest !
when <?($@.mcNuggetNumbers($) <=0>)> do @.largest: $;
otherwise @.mcNuggetNumbers([$ + 6, $ + 9, $ + 20]): 1..3 -> 1;
end largestNonMcNuggetNumber
100 -> largestNonMcNuggetNumber -> !OUT::write
- Output:
43
UNIX Shell
possible=()
for (( i=0; i<18; ++i )); do
for (( j=0; j<13; ++j )); do
for (( k=0; k<6; ++k )); do
(( n = i*6 + j*9 + k*20 ))
if (( n )); then
possible[n]=1
fi
done
done
done
for (( n=100; n; n-- )); do
if [[ -n ${possible[n]} ]; then
continue
fi
break
done
printf 'Maximum non-McNuggets number is %d\n' $n
- Output:
Maximum non-McNuggets number is 43
possible=
i=0
while [ $i -lt 18 ]; do
j=0
while [ $j -lt 13 ]; do
k=0
while [ $k -lt 6 ]; do
possible="${possible+$possible }"`expr $i \* 6 + $j \* 9 + $k \* 20`
k=`expr $k + 1`
done
j=`expr $j + 1`
done
i=`expr $i + 1`
done
n=100
while [ $n -gt 0 ]; do
if echo "$possible" | tr ' ' '\n' | fgrep -qx $n; then
n=`expr $n - 1`
continue
fi
break
done
echo "Maximum non-McNuggets number is $n"
- Output:
Maximum non-McNuggets number is 43
V (Vlang)
fn mcnugget(limit int) {
mut sv := []bool{len: limit+1} // all false by default
for s := 0; s <= limit; s += 6 {
for n := s; n <= limit; n += 9 {
for t := n; t <= limit; t += 20 {
sv[t] = true
}
}
}
for i := limit; i >= 0; i-- {
if !sv[i] {
println("Maximum non-McNuggets number is $i")
return
}
}
}
fn main() {
mcnugget(100)
}
- Output:
Maximum non-McNuggets number is 43
VTL-2
10 N=0
20 :N+1)=0
30 N=N+1
40 #=100>N*20
50 A=0
60 B=A
70 C=B
80 :C+1)=160
90 C=C+20
100 #=100>C*80
110 B=B+9
120 #=100>B*70
130 A=A+6
140 #=100>A*60
150 N=101
160 N=N-1
170 #=:N+1)
180 ?="Largest non-McNuggets number: ";
190 ?=N
- Output:
Largest non-McNuggets number: 43
Wren
var mcnugget = Fn.new { |limit|
var sv = List.filled(limit+1, false)
var s = 0
while (s <= limit) {
var n = s
while (n <= limit) {
var t = n
while (t <= limit) {
sv[t] = true
t = t + 20
}
n = n + 9
}
s = s + 6
}
for (i in limit..0) {
if (!sv[i]) {
System.print("Maximum non-McNuggets number is %(i)")
return
}
}
}
mcnugget.call(100)
- Output:
Maximum non-McNuggets number is 43
XPL0
int N, A(101), X, Y, Z;
[for N:= 0 to 100 do A(N):= false;
for X:= 0 to 100/6 do
for Y:= 0 to 100/9 do
for Z:= 0 to 100/20 do
[N:= 6*X + 9*Y + 20*Z;
if N <= 100 then A(N):= true;
];
for N:= 100 downto 0 do
if A(N) = false then
[IntOut(0, N);
exit;
];
]
- Output:
43
zkl
nuggets:=[0..101].pump(List()); // (0,1,2,3..101), mutable
foreach s,n,t in ([0..100/6],[0..100/9],[0..100/20])
{ nuggets[(6*s + 9*n + 20*t).min(101)]=0 }
println((0).max(nuggets));
- Output:
43
- Programming Tasks
- Solutions by Programming Task
- 11l
- 8080 Assembly
- ABC
- Action!
- Ada
- ALGOL 68
- APL
- AppleScript
- Arturo
- Asymptote
- AWK
- BASIC
- Applesoft BASIC
- BASIC256
- Chipmunk Basic
- Gambas
- GW-BASIC
- Minimal BASIC
- MSX Basic
- PureBasic
- Quite BASIC
- Run BASIC
- True BASIC
- XBasic
- Yabasic
- BCPL
- BQN
- C
- C sharp
- C++
- Clojure
- CLU
- COBOL
- Comal
- Cowgol
- Dart
- Draco
- Dyalect
- EasyLang
- Elixir
- F Sharp
- Factor
- FOCAL
- FreeBASIC
- Frink
- FutureBasic
- Go
- Haskell
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Locomotive Basic
- Lua
- MACRO-11
- MAD
- Mathematica
- Wolfram Language
- Modula-2
- MiniScript
- MiniZinc
- Nim
- Pascal
- Perl
- Ntheory
- Phix
- Picat
- PicoLisp
- PL/I
- PL/M
- PowerShell
- Python
- Quackery
- R
- Racket
- Raku
- Refal
- REXX
- Ring
- RPL
- Ruby
- Rust
- SETL
- Swift
- Tailspin
- UNIX Shell
- V (Vlang)
- VTL-2
- Wren
- XPL0
- Zkl