Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Matlab program to solve this game: 1) 100 children stand in a line, and

ID: 2318564 • Letter: W

Question

Write a Matlab program to solve this game: 1) 100 children stand in a line, and the first child has a basket full of sticks. None of the others have a stick. 2) Starting with the first child, each child takes a turn as runner going down the line with the basket of sticks. The runner goes only down the line, starting with their own position. 3) The runner stops at every child (including themselves) whose positions is a multiple of their position. So child #5 stops at child 5 10 15 20 etc 4) When the runner stops at a child, he or she checks whether the child has a stick or not. * if the child has a stick, the runner takes it away * if the child has no stick, the runner gives a stick. Who has a stick at the end of the game? Print out the numbers of the children that have sticks at the end. That is, if every 10th child ends up with a stick, then print: 10 20 30 40 50 60 70 80 90 100

Explanation / Answer

children = zeros(1,100);
children(1)=100;%initializing first child with 100 sticks
for i=1:100
for j=i:100
if rem(j,i)==0
if children(j)==0%no stick
if children(i)~=0%checking weather runner have sticks to give it to the others
children(j) = children(j)+1; %giving the stick   
children(i) = children(i)-1; %decrementing his count by 1
end
else
children(j) = children(j)-1;%taking the stick away
children(i) = children(i)+1;%adding the count himself
end
end
end
end
disp(children);
for i=1:100%printing all non zero indexes
if (children(i)~=0)
fprintf('%d ',i);
end
end

result:

1 2 3 5 7 8 11 14 18 21 32 36 52 53 56 58 59 61 62 65 67 68 70 71 73 74 76 79 80 82 83 86 88 89 92 94 95 97

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote