Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
24
Task/McNuggets-problem/Ada/mcnuggets-problem.adb
Normal file
24
Task/McNuggets-problem/Ada/mcnuggets-problem.adb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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;
|
||||
19
Task/McNuggets-problem/Agena/mcnuggets-problem.agena
Normal file
19
Task/McNuggets-problem/Agena/mcnuggets-problem.agena
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
scope # 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
|
||||
|
||||
local constant maxNuggets := 100;
|
||||
local sum := [];
|
||||
for i from 0 to maxNuggets do sum[ i ] := false od;
|
||||
for x from 0 to maxNuggets by 6 do
|
||||
for y from x to maxNuggets by 9 do
|
||||
for z from y to maxNuggets by 20 do
|
||||
sum[ z ] := true
|
||||
od
|
||||
od
|
||||
od;
|
||||
# show the highest number that cannot be formed
|
||||
local largest := maxNuggets;
|
||||
while sum[ largest ] do largest -:= 1 od;
|
||||
printf( "The largest non McNugget number is: %d\n", largest )
|
||||
|
||||
epocs
|
||||
35
Task/McNuggets-problem/COBOL/mcnuggets-problem.cob
Normal file
35
Task/McNuggets-problem/COBOL/mcnuggets-problem.cob
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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).
|
||||
21
Task/McNuggets-problem/Fennel/mcnuggets-problem.fennel
Normal file
21
Task/McNuggets-problem/Fennel/mcnuggets-problem.fennel
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(do ;;; 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
|
||||
|
||||
(local maxNuggets 100)
|
||||
(var sum {})
|
||||
(for [i 0 maxNuggets] (tset sum i false))
|
||||
(for [x 0 maxNuggets 6]
|
||||
(for [y x maxNuggets 9]
|
||||
(for [z y maxNuggets 20]
|
||||
(tset sum z true)
|
||||
)
|
||||
)
|
||||
)
|
||||
; show the highest number that cannot be formed
|
||||
(var largest maxNuggets)
|
||||
(while (. sum largest)
|
||||
(set largest (- largest 1))
|
||||
)
|
||||
(print (.. "The largest non McNugget number is " largest))
|
||||
|
||||
)
|
||||
21
Task/McNuggets-problem/Oberon-07/mcnuggets-problem.oberon
Normal file
21
Task/McNuggets-problem/Oberon-07/mcnuggets-problem.oberon
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MODULE McNuggetsProblem;
|
||||
(* 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 *)
|
||||
IMPORT Out;
|
||||
|
||||
CONST maxNuggets = 100;
|
||||
VAR isSum : ARRAY maxNuggets + 1 OF BOOLEAN;
|
||||
x, y, z, largest : INTEGER;
|
||||
|
||||
BEGIN
|
||||
(* find the numbers that can be formed *)
|
||||
FOR x := 0 TO maxNuggets BY 6 DO
|
||||
FOR y := x TO maxNuggets BY 9 DO
|
||||
FOR z := y TO maxNuggets BY 20 DO isSum[ z ] := TRUE END
|
||||
END
|
||||
END;
|
||||
(* show the highest number that cannot be formed *)
|
||||
largest := maxNuggets;
|
||||
WHILE isSum[ largest ] DO DEC( largest ) END;
|
||||
Out.String( "The largest non-McNugget number is: " );Out.Int( largest, 0 );Out.Ln
|
||||
END McNuggetsProblem.
|
||||
|
|
@ -1,13 +1,15 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">100</span>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<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>
|
||||
<!--
|
||||
with javascript_semantics
|
||||
function mnmnn(sequence counts, integer limit=2000)
|
||||
if gcd(counts)>1 then return "No maximum" end if
|
||||
sequence nuggets = repeat(false,limit+1)
|
||||
integer {i,j,k} = counts
|
||||
for a=0 to limit by i do
|
||||
for b=a to limit by j do
|
||||
for c=b to limit by k do
|
||||
nuggets[c+1] = true
|
||||
end for
|
||||
end for
|
||||
end for
|
||||
return sprintf("%d",rfind(false,nuggets)-1)
|
||||
end function
|
||||
printf(1,"Maximum non-McNuggets number is %s\n", mnmnn({6,9,20},100))
|
||||
|
|
|
|||
|
|
@ -1,46 +1,45 @@
|
|||
(phixonline)-->
|
||||
<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>
|
||||
with javascript_semantics
|
||||
function Mcnugget_number(sequence counts)
|
||||
|
||||
<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>
|
||||
if gcd(counts)>1 then return "No maximum" end if
|
||||
|
||||
<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>
|
||||
<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 cmin = min(counts), klim = 0, a = -1
|
||||
sequence meals = {}
|
||||
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))
|
||||
if k and (klim=0 or k<=klim) then
|
||||
if k>length(meals) then
|
||||
meals &= repeat(0,k-length(meals))
|
||||
end if
|
||||
meals[k] = 1
|
||||
end if
|
||||
end for
|
||||
end for
|
||||
end for
|
||||
integer r = 0, res = 0
|
||||
for i=1 to length(meals) do
|
||||
if meals[i] then
|
||||
r += 1
|
||||
if r=cmin then klim = res exit end if
|
||||
else
|
||||
r = 0
|
||||
res = i
|
||||
end if
|
||||
end for
|
||||
if r=cmin and a*cmin>res then
|
||||
return sprintf("%d",res)
|
||||
end if
|
||||
end while
|
||||
end function
|
||||
|
||||
<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>
|
||||
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">a</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<span style="color: #000080;font-style:italic;">-- atom k = sum(sq_mul(p,counts))+1</span>
|
||||
<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>
|
||||
<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>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<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>
|
||||
<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>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<!--
|
||||
constant tests = {{6,9,20}, {6,7,20}, {1,3,20}, {10,5,18}, {5,17,44}, {2,4,6}, {3,6,15},
|
||||
{12,14,17},{12,13,34},{5,9,21},{10,18,21},{71,98,99},{4,30,16},{12,12,13},{6,15,1}}
|
||||
for t in tests do
|
||||
printf(1,"Maximum non-Mcnugget number using %v is: %s\n",{t,Mcnugget_number(t)})
|
||||
end for
|
||||
|
|
|
|||
59
Task/McNuggets-problem/Phix/mcnuggets-problem-3.phix
Normal file
59
Task/McNuggets-problem/Phix/mcnuggets-problem-3.phix
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
requires(64,true)
|
||||
|
||||
function congruence(atom a, c, m)
|
||||
// Solves ax = c mod m
|
||||
atom aa = mod(a,m),
|
||||
cc = mod(c+a*m,m)
|
||||
if aa == 1 then
|
||||
return cc
|
||||
end if
|
||||
atom y = congruence(m, -cc, aa);
|
||||
return (m*y+cc)/aa
|
||||
end function
|
||||
|
||||
function frobenius(sequence unsorted_a)
|
||||
sequence a = sort(deep_copy(unsorted_a))
|
||||
assert(a[1]>=1);
|
||||
if gcd(a)>1 then return "Undefined" end if
|
||||
atom d12 = gcd(a[1],a[2]),
|
||||
d13 = gcd(a[1]/d12,a[3]),
|
||||
d23 = gcd(a[2]/d12,a[3]/d13),
|
||||
{a1,a2,a3} = sort({a[1]/d12/d13,
|
||||
a[2]/d12/d23,
|
||||
a[3]/d13/d23})
|
||||
atom rod = -1
|
||||
if a1!=1 then
|
||||
// Rødseth’s Algorithm
|
||||
atom w1 = a1,
|
||||
s0 = congruence(a2,a3,a1);
|
||||
sequence s = {a1},
|
||||
q = {}
|
||||
while s0 != 0 do
|
||||
s &= s0
|
||||
atom s1 = iff(s0==1 ? 0 : s0-mod(w1,s0)),
|
||||
q1 = (w1+s1)/s0;
|
||||
q &= q1
|
||||
w1 = s0
|
||||
s0 = s1
|
||||
end while
|
||||
sequence p = {0, 1}
|
||||
atom r = (s[2]*a2-p[2]*a3)/a1;
|
||||
integer i = 1;
|
||||
while r > 0 do
|
||||
atom p_next = q[i]*p[i+1]-p[i];
|
||||
p &= p_next
|
||||
r = (s[i+2]*a2-p_next*a3)/a1;
|
||||
i += 1;
|
||||
end while
|
||||
rod = -a1+a2*(s[i]-1)+a3*(p[i+1]-1)
|
||||
- min(a2*s[i+1],a3*p[i])
|
||||
end if
|
||||
return sprintf("%d",rod*d12*d13*d23+a[1]*(d23-1)+a[2]*(d13-1)+a[3]*(d12-1))
|
||||
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},
|
||||
{12,14,17},{12,13,34},{5,9,21},{10,18,21},{71,98,99},{4,30,16},{12,12,13},
|
||||
{7074047,8214596,9098139},{582795988,1753241221,6814151015},{6,15,1}}
|
||||
for t in tests do
|
||||
printf(1,"Maximum non-Mcnugget number using %v is: %s\n",{t,frobenius(t)})
|
||||
end for
|
||||
19
Task/McNuggets-problem/Pluto/mcnuggets-problem.pluto
Normal file
19
Task/McNuggets-problem/Pluto/mcnuggets-problem.pluto
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
do -- 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
|
||||
|
||||
local maxNuggets <const> = 100
|
||||
local sum = {}
|
||||
for i = 0, maxNuggets do sum[ i ] = false end
|
||||
for x = 0, maxNuggets, 6 do
|
||||
for y = x, maxNuggets, 9 do
|
||||
for z = y, maxNuggets, 20 do
|
||||
sum[ z ] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
-- show the highest number that cannot be formed
|
||||
local largest = maxNuggets
|
||||
while sum[ largest ] do largest -= 1 end
|
||||
print( $"The largest non McNugget number is {largest}" )
|
||||
|
||||
end
|
||||
18
Task/McNuggets-problem/PowerShell/mcnuggets-problem.ps1
Normal file
18
Task/McNuggets-problem/PowerShell/mcnuggets-problem.ps1
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
$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"
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
# Get s/m/l ranges to (100/6, 100/9, 100/20) and multipiy out.
|
||||
⟜(◴≡/+פ⟜(☇1⇡⌊÷)6_9_20) 100
|
||||
# Compare against 0-100 for missing, return highest.
|
||||
/↥▽:⟜(¬∊)⇡⟜(▽⊸≤)
|
||||
# Get s/m/l ranges to (100/6, 100/9, 100/20) and multiply out.
|
||||
# Then compare against 0-100 for missing, return highest.
|
||||
/↥⊚¬∊▽⤚≥◴≡/+×⊃(¤|♭₂⇡⌊÷|⋅∘|⋅⇡)6_9_20 100
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue