McNuggets problem: Difference between revisions

Add ABC
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Add ABC)
(15 intermediate revisions by 8 users not shown)
Line 31:
</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()
Line 341 ⟶ 405:
 
<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}}==
Line 381 ⟶ 468:
{{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}}==
Line 491 ⟶ 828:
<pre>
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>
 
Line 708 ⟶ 1,078:
 
<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}}==
Line 1,236 ⟶ 1,629:
43
</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .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</syntaxhighlight>
{{out}}
<pre>43</pre>
 
=={{header|MAD}}==
Line 1,292 ⟶ 1,724:
{{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}}
<pre>
The largest non-McNugget number is 43</pre>
 
=={{header|MiniZinc}}==
Line 1,325 ⟶ 1,776:
{{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>
 
=={{header|Perl}}==
Line 1,967 ⟶ 2,455:
<pre>
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>
 
Line 2,105 ⟶ 2,628:
g(6, 15, 1) = -1
</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}}==
Line 2,283 ⟶ 2,818:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var mcnugget = Fn.new { |limit|
var sv = List.filled(limit+1, false)
var s = 0
2,093

edits