% c is a counting variable
% r is a dummy variable to check if modulus = 0
% num is the number of sailors
% monkey is the number of cocounts given to the monkey
% x is the number of starting coconuts
num = 10;
monkey = 4;
x = 1; %set to one, will output as final answer
c = 1;
r = 1;
Message1 = ['When you have ', num2str(num), ' sailors and give the monkey ', num2str(monkey), ' coconuts per time, '];
while r ~= 0
p = x;
c = 1;
while c <= num
p = ((num -1)/num).*(p- monkey);
m = p - monkey;
r = mod(m,num);
if r == 0
c = c + 1;
end
if r ~=0
x = x + 1;
c = num + 1;
end
end
end
Message2 = ['you will need ', num2str(x) , ' starting coconuts.'];
disp(Message1)
disp(Message2)