In: Computer Science
Chicken McNuggets come in 4, 7, and 10-piece boxes. If you want exactly N nuggets, is there a combination of boxes that you can order to get exactly ? What is a combination that will work?Write a MATLAB function that when you give it N it returns if N is a McNugget number and what combination (of the 4, 7 10 boxes) will give you that number.
I have added the code and comments for step by step explaination
function mcnugget (n) % this is the function, which takes N as input
nug = zeros(n,3); % n X 3 array, to store the combination, number of [10 7 4] boxes required
mc = zeros(n); % mc(i) stores whether i is a mcnugget number or not
comb = [10,7,4]; % the combinations
for i = 1:n
for j = 1:3
if i-comb(j) == 0 % check if i is from array [10 7 4] then mark it as a mcnugget number
mc(i) = 1;
nug(i,j) = 1;
end
end
if mc(i) == 1 % if already marked then continue
continue
end
for j = 1:3
if i-comb(j) > 0 && mc(i-comb(j)) == 1 % check if adding any unit box to previous mcnugget number gives i
mc(i) = 1; % if yes then mark it
for k = 1:3
nug(i,k) = nug(i-comb(j),k);
end
nug(i,j) = nug(i,j) + 1;
end
if mc(i) == 1 % if already marked then break
break
end
end
end
if mc(n) == 1
fprintf("%d is a McNugget number \n",n);
fprintf("A combination of [ %d - 10 boxes] , [ %d - 7 boxes] and [ %d - 4 boxes] \n",nug(n,1),nug(n,2),nug(n,3));
else
fprintf("%d is not a McNugget number \n",n);
end
end
Output: