Recent

Author Topic: Monkeys and coconuts: a challenge  (Read 9909 times)

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Monkeys and coconuts: a challenge
« on: April 30, 2015, 08:41:05 pm »
Hi,

First of all. This is not a school assignment.
(See my wiki page to see what I'm doing in real life.)

For those who have nothing better to do, or just like the challenge.

Here's the situation.

After a huge storm 5 sailors are stranded on a tropical island.
On the island there is no other human being.
There are trees and 1 monkey (maybe other monkeys too, but the don't show themselves).

The sailors gather as much coconuts as they can and then go to sleep.
During the night one after each other the sailors wake up.
Each sailor in turn, when awake, sees the pile of coconuts and divides it into 5 equal shares.
After dividing there is exactly 1 coconut left, and he gives that to the monkey.
He hides his share of coconuts and then puts the remaining back onto 1 pile.

When the day breaks, all of the sailors wake up again and see a rather smaller pile.
(Nobody of course is willing to tell what he's donduring the night).
The sailors then divide this final pile into 5 equal shares, and this time no coconut is leftover (so the monkey gets none).

Q1: How many coconuts where there at the beginning (it can be proven that there is an infinite amount of solutions, so what is the smallest amount)?

Q2: Can you write an equation that calculates the smallest possible solution?

Q3: Can you write a search function that finds the smallest possible solution, with the condition that the input of that function must be the amount of coconuts that each sailor gets in the final division? (Starting with the total amount is way to easy).
(A prototype could be: function IsPossibleSolution(FinalShare: Integer; out N: Integer): Boolean;)

Q4: Can you generalize the function from Q3 so that it accepts 6 arbitrary remainders (0..4) for each step in the division? It can be assumed that after each division, the remainder is given to the monkey.

Q5: Given the same situation, but now in the final division there is again 1 coconut remaining (which goes to the monkey). How many coconuts where there at the beginning?

Q6: Given the function from Q5: what is the solution if the remainders are (from first division to last): 4, 3, 2, 1, 0, 1?

Bart

lainz

  • Hero Member
  • *****
  • Posts: 4725
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Monkeys and coconuts: a challenge
« Reply #1 on: April 30, 2015, 09:06:17 pm »
Nice. It keep my thinking... I can't solve it right now. My mind is more artistic than nothing.

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Monkeys and coconuts: a challenge
« Reply #2 on: April 30, 2015, 09:20:07 pm »
ya got some ambiguity in the part above Q1.

6 <> 0..4

'He shares...' which sailor? last one? each one?
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Monkeys and coconuts: a challenge
« Reply #3 on: April 30, 2015, 10:05:47 pm »
add Q1: My function says 3121 coconuts. Am I right?  :)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: Monkeys and coconuts: a challenge
« Reply #4 on: April 30, 2015, 10:08:51 pm »
6 <> 0..4

In total there are 6 divisions.
Each of the 5 sailors does one, and in the morning there is the final division.

Since each division is a division by 5 (there are 5 sailors), the remainder can only be in the range of 0..4.

'He shares...'

I never said "He shares".
Each sailor divides the pile into 5 equal amounts, and the remainig 1 cocounut from this action goes to the monkey.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: Monkeys and coconuts: a challenge
« Reply #5 on: April 30, 2015, 10:10:44 pm »
add Q1: My function says 3121 coconuts. Am I right?  :)

I don't want to comment on that in public yet.
I'll pm you.

Bart

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Monkeys and coconuts: a challenge
« Reply #6 on: April 30, 2015, 10:19:48 pm »
So should we send you PMs with answers and later you will announce winner ?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Monkeys and coconuts: a challenge
« Reply #7 on: April 30, 2015, 10:23:56 pm »
6 <> 0..4

In total there are 6 divisions.
Each of the 5 sailors does one, and in the morning there is the final division.

Since each division is a division by 5 (there are 5 sailors), the remainder can only be in the range of 0..4.

'He shares...'

I never said "He shares".
Each sailor divides the pile into 5 equal amounts, and the remainig 1 cocounut from this action goes to the monkey.

Bart

true. it was 'He hides his share...'
very interesting problem.

seems to me to be a task that calls for recursion and working backwards...
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Monkeys and coconuts: a challenge
« Reply #8 on: April 30, 2015, 10:33:00 pm »
seems to me to be a task that calls for recursion and working backwards...
Nope. it's just a loop.

Each "iteration result" should be dividable by 4 without modulus (since a sailor ...puts the remaining back onto 1 pile.)
But since we're going backwards, we need to calculate the "previous" state, which is reversive of and divides it into 5 equal shares.
After dividing there is exactly 1 coconut left, and he gives that to the monkey.


Now for Q6, 1 coconut should be replaceable by a value passed to a function.

anyway, it's all too easy, and i think it's Bart's school assignment. :)

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Monkeys and coconuts: a challenge
« Reply #9 on: April 30, 2015, 10:48:21 pm »
it does seem like an assignment - but i'll not google.

the sequence in Q6 looks suspicious - when the remainder is 0, the amount divided must have been a multiple of 5. given that, the last division can't result in a remainder of 1.


Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

eny

  • Hero Member
  • *****
  • Posts: 1647
Re: Monkeys and coconuts: a challenge
« Reply #10 on: April 30, 2015, 11:10:22 pm »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Monkeys and coconuts: a challenge
« Reply #11 on: April 30, 2015, 11:19:19 pm »
Step 1 define the 6 equations one for each devision something along the lines of
1)X = 5A+1
2)A = 5B+1
3)B = 5C+1
4)C = 5D+1
5)D = 5E+1
6)E = 5F+1

Replace all unknowns with their equations starting from the bottom up. we have something like
5) D = 5E+1 => D = 5(5F+1)+1 => D = 25F+6
4) C = 5D+1 => C = 5(25F+6)+1 => C = 125F+30+1 => C = 125F+31
3) B = 5C+1 => B = 5(125F+31)+1 => B = 625F+155+1 => B = 625F+156
2) A = 5B+1 => A = 5(625F+156)+1 => A= 3125F+780+1 => A = 3125F+781
1) X = 5A+1 => X = 5(3125F+781) + 1 =>  X = 15625F+3905+1 => X = 15625F+3906 smaller possible integer number is 1 so starting number is 19.531

of course the above assume that the last devision gave a coconut to the monkey as well which makes it the solution for Q5. Calculations  are made while typing the solution mostly in my head so it probably has errors. It is simple a description for the methodology for calculating the final equation to create a function for it ee

Code: [Select]
Function HowManyCoconuts( EachSailorGot:Byte)Qword;
begin
  Result := 15625*EachSailorGot+3906;
end;

the sequence in Q6 looks suspicious - when the remainder is 0, the amount divided must have been a multiple of 5. given that, the last division can't result in a remainder of 1.
basically neither can the second division. If the first division by 5 gives one reminder which is given away then the second division by 5 should have no reminder at all. As an exercise in logic and task subdivision is nice but (in the famous words of Mr spock) it is illogical.
« Last Edit: April 30, 2015, 11:39:23 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: Monkeys and coconuts: a challenge
« Reply #12 on: May 01, 2015, 02:10:21 am »
Code: [Select]
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
  { TForm1 }
  TForm1 = class(TForm)
    B1: TButton;
    E1: TEdit; { e1.text = 1 }
    E2: TEdit; { e2.text = 1000 }
    L1: TLabel; { result: after 5 sailors }
    L2: TLabel; { result: start }
    procedure B1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
var
  Form1: TForm1;
  i: integer;
  T: array [ 0..5 ] of real;
Implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.B1Click(Sender: TObject);
begin
  L1.Caption:= '?';
  L2.Caption:= '?';
  for i:= StrToInt( E1.Text ) to StrToInt( E2.Text ) do
  begin
    T[5]:= i * 5; { min T[5] mod 5 = 0 } { After 5 sailors }
    T[4]:= ( T[5] * 5/4 ) + 1;  { After 4 sailors }
    if frac( T[4] / 4 ) > 0 then
      continue;
    T[3]:= ( T[4] * 5/4 ) + 1;  { After 3 sailors }
    if frac( T[3] / 4 ) > 0 then
      continue;
    T[2]:= ( T[3] * 5/4 ) + 1;  { After 2 sailors }
    if frac( T[2] / 4 ) > 0 then
      continue;
    T[1]:= ( T[2] * 5/4 ) + 1;  { After 1 sailor }
    if frac( T[1] / 4 ) > 0 then
      continue;
    T[0]:= ( T[1] * 5/4 ) + 1;  { Start }
    if frac(( T[0] - 1 ) / 5 ) = 0 then
    begin
      L1.Caption:= IntToStr( i );
      L2.Caption:= IntToStr( trunc( T[ 0 ] ));
      ShowMessage( 'Solution found for this interval.' );
      break;
    end;
  end;
  if L1.Caption = '?' then
    ShowMessage( 'No solution for this interval.' );
end;
end. 
Lazarus 2.0.10 Windows 10 64bits

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Monkeys and coconuts: a challenge
« Reply #13 on: May 01, 2015, 04:21:36 am »
Step 1 define the 6 equations one for each devision something along the lines of
1)X = 5A+1
2)A = 5B+1
...
According to the task, it should go like this:
1) X = A + 4*A + 1
2) 4 * A = B + 4* B + 1
3) 4 * B = C + 4 *C + 1
... keep in mind the condition He hides his share of coconuts and then puts the remaining back onto 1 pile

Thus the next division takes place only on the 4-parts of 5N



Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: Monkeys and coconuts: a challenge
« Reply #14 on: May 01, 2015, 09:24:48 am »
I don't remember the exact formulation of this problem for fish and fishers but Witgenstein or Dirac got answer 'minus 5 fishes'.

As for this problem:

Amount of coconuts = Q

Q = 5*N1 +1;
4*N1 = 5*N2 + 1;
4*N2 = 5*N3 + 1;
4*N3 = 5*N4 + 1;
4*N4 = 5*N5 + 1;
N5 = 5*N; for Q1 and N5 = 5*N+1; for Q5

then:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var n:integer;
    Q,n1,n2,n3,n4,n5:integer;
    solution:boolean;
begin
  memo1.clear;
  n:=0;
  solution:=false;
  repeat
    inc(n);
    n5:=5*n{+1};
    begin
    n4:=5*n5+1;
    if n4 mod 4 = 0 then begin
      n3:=5*n4 div 4+1;
      if n3 mod 4 = 0 then begin
        n2:=5*n3 div 4+1;
        if n2 mod 4 = 0 then begin
          n1:=5*n2 div 4+1;
          if n1 mod 4 = 0 then begin
            Q:=5*n1 div 4+1;
            memo1.lines.add(inttostr(Q));
            solution:=true;
          end;
        end;
      end;
    end;
   end;
  until n>10000;
end;

Q1 -------- 3121
Q2 -------- I was trying to but got stuck finally :) too unexperienced in digital math
Q3...Q4 --------- no sense
Q5 --------- 6246
Q6 --------- didn't understand the question

----------------------------------- (UPD) ------------------------------------

a next step towards Q2: Solved the equations system with multiplication and no division:
4*4*4*4*Q = 5*5*5*5*5*N5 + 5*5*5*(5 + 4) + 5*5*4*4 + 5*4*4*4 +4*4*4*4;
N5 = 5*N; for Q1 and N5 = 5*N+1; for Q5
i.e.
256*Q = 15625*N + 1161 {+3125};
(hopefully no mistakes here)
Its equation but it doesn't calculate anything actually yet :) and "15625*N + 1161 {+3125} mod 256 = 0" is not a 'solvable' equation.
« Last Edit: May 01, 2015, 01:03:23 pm by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

 

TinyPortal © 2005-2018