Write a Matlab program to solve this game 1 100 children sta

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

Solution

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

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)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site