-2

I needed some help with a problem I'd been assigned in class. It's our introduction to for loops. Here is the problem:

Consider the following riddle.

This is all I have so far:

function pile = IslandBananas(numpeople, numbears)
for pilesize=1:10000000


end

I would really appreciate your input. Thank you!

  • Why did you delete most of your question? Just show some more effort and the SO community will definitely be ready to help! – voxeloctree Jul 25 '13 at 02:08
  • Deleting the purpose of the exercise and leaving only a meaningless code fragment isn't really an improvement to this question. – tmpearce Jul 25 '13 at 02:08

1 Answers1

2

I will help you, but you need to try harder than that. And also, you only need one for loop. First, think about how you would construct this algorithm. Well you know you have to use a for loop so that is a start. So let's think about what is going on in the problem.

1) You have a pile.

2) First night someone takes the pile and divides it into 3 and finds that one is left over, this means mod(pile,3) = 1.

3) But he discards the extra banana. This means (pile-1).

4) He takes a third of it, leaving two-thirds left. This means (2/3)*(pile-1).

5) In the morning they take the pile and divide it into 3 and find again that one is left over, so this means mod((2/3)*(pile-1),3) = 1.

6) But they discard the extra banana. This means (2/3)*(pile-1)-1.

7) Finally, they have to each have at least one banana if it is to be the smallest pile possible. Thus, the smallest pile must be such that (1/3)*((2/3)*(pile-1)-1) = 1.

I have essentially given you the answer, the rest you can write with the formula (1/3)*((2/3)*(pile-1)-1) and a simple if statement to test for the smallest possible integer which is 1. This can be done in four lines inside of your for loop.

Now, expanding this to any number of people and any number of bears requires two simple substitutions in that formula! If your teacher demands it, this can easily be split into two nested for loops.

voxeloctree
  • 839
  • 6
  • 13
  • Not quite. First of all, your assignments are confusing. As is this MATLAB code will not even run. You can break up the code into steps if you like, that might be good for you to understand the process, but really all you need to do is say in your for loop, `endpile = formulaIgaveyou` and then right after that have an appropriate test to verify that `endpile` is an integer, if you include a break statement, once that is satisfied you are done. – voxeloctree Jul 24 '13 at 23:39
  • 1
    Knowing how to use if and for loops are not MATLAB specific, but fundamental precepts of programming. You need to review these concepts and seriously try. Unless you do, I cannot help you any further. I have already supplied the entire answer. You need to think about this problem and code it for yourself, clearly you need the practice. As for references, they are readily available on the internet. Please put serious effort into your own work. – voxeloctree Jul 25 '13 at 01:54