Sailors and coconuts problem

Started by bplus, February 13, 2021, 06:40:51

Previous topic - Next topic

bplus

Hey I noticed this added to On-line Samples recently.

Yeah we played with this at JB when bluatigro presented the problem in his very broken English.

Cute! I called it, "And one for the Monkey Problem"

Here is my interpretation from my SmallBASIC inspired Interpreter allot of the descriptions are quotes from the blu:

' and 1 for the monkey problem SB.txt rev for SB.exe
'from
' and 1 for the monkey problem.bas for JB 2.0 b 2018-02-11
' test string new string array functions > and <

n start 100
.   i (bluatigro) fount this puzle in a book:
.     there are 5 person's + 1 monkey
.     they shipwrek on a Island
.     the fisrst day they colect kokonut's
.
. day 1:"
. I (bplus) am starting with a number of coconuts.
n cc start
dim pile(5) this is why we need string array! for 5 piles
. Say the coconuts collected by the people is start .
.
.     in the night happens this:
.     (each) person goes to the pile
.     divides it in 5 hides 1/5
.     and gives 1 to the monkey
.
. I (bplus) am paying the monkey first.
. I (bplus) am not cutting any coconuts into parts of coconuts.
.
. So Coconut inventory after first night:
[
n persn persn + 1
i persn > 5
x
f
n cc cc - 1
n monk monk + 1
> piel persn int(cc/5)
n cc cc - int(cc/5)
< p piel persn
. For person persn , their pile is p
]
.             and monkey has monk
.
. day 2"
.      the 2e day"
.      thay divide the remaining pile in 5"
.      and give 1 to the monkey"
.      now calc the size of every pile"
.
. Coconuts starting day 2, before final divvy is cc
n cc cc - 1
n monk monk + 1     
n divee int(cc/5)
. After monkey gets one more coconut the divvy is divee
.
n persn 1
[
< p piel persn
n p p + divee
> piel persn p
n persn persn +1
i persn > 5
x
f
]
n Remain start
n persn 1
[
< p piel persn
. For person persn , their pile is p
n Remain Remain - p
n persn persn + 1
i persn > 5
x
f
]
. The monkey has monk coconuts.
n Remain Remain - monk
.
. Coconuts remaining are Remain for the monkey who programmed this inventory.



n means you are going to set a number varName varValueExpression.

You might figure out the rest :)


1 person likes this

twgonder

#1
My first attempt in SmallBasic.

I am having some funny display problems. When I run this on ver 12.14 and 12.20 SDL
at the end a line seems to always come up missing in my tests.
Trial 3897 (the left most number) is always missing.

Is there a way to make sure that all print lines are displayed?
Is there a way to scroll up/down in the output (the mouse and page keys don't seem to work)?

Thanks

round157

Quote from: twgonder on June 30, 2021, 23:03:50
My first attempt in SmallBasic.



Hello, new user?

The author(chrisw) of SmallBASIC does not come to the forum daily. However, there are several experienced users. They may solve the problem.

(Welcome to this forum!)

bplus

Quote from: twgonder on June 30, 2021, 23:03:50
My first attempt in SmallBasic.

I am having some funny display problems. When I run this on ver 12.14 and 12.20 SDL
at the end a line seems to always come up missing in my tests.
Trial 3897 (the left most number) is always missing.

Is there a way to make sure that all print lines are displayed?
Is there a way to scroll up/down in the output (the mouse and page keys don't seem to work)?

Thanks

If you get the print lines saved into an array you can make a scroller for array and show a page section at a time with arrow keys and page up/down keys, key home to go to start  and key end to last lines...

1 person likes this

bplus

In SB sample, the question asks what is minimum coconuts the sailors had to pick?

What is minimum picked to achieve what goal or specification?

The way I learned this riddle is: given they picked x amount of coconuts what is the amount of coconuts each sailor has after the final divy (and how many did the monkey get).
1 person likes this

Aurel [banned]

what i have is a flying monkey
so i am wondering maybe you can made something similar in SB
(Y)

twgonder

The original problem that I gave coding applicants was simple and needed about six lines of actual code. Some poor guys wrote pages of code. Others snuck out of the testing room. The problem was something like this:

Five explorers go into the jungle to pick coconuts all day long. Back at camp they put them into a big pile and agreed to divide the pile up evenly in the morning. Explorer 1, awoke in the middle of the night, and not being a trusting sole, went to the pile, divided it into 5 smaller and equal piles, with one extra that he gave to a monkey that was watching. He hid his pile in the jungle, pushed the remaining four piles into one pile and went back to sleep. During the rest of the night, the other four explorers did the same pile division. In the morning all awoke to a much smaller pile than the evening before, but being unable to admit what they had each done, they divided the remaining pile into exactly five piles and went their separate ways. What is the minimum number of coconuts the explorers had to pick the day before?
(The first trick is to realize that you can't split a coconut five ways.)

From what I understand, this was a problem presented in an early 1900s university mathematics journal, long before computers were available to solve it. As I remember, there is a weird solution, but it results in a negative number of coconuts (not exactly a solution in my book).

I wrote this program to be more versatile and esthetic.

bplus

QuoteWhat is the minimum number of coconuts the explorers had to pick the day before?
(The first trick is to realize that you can't split a coconut five ways.)

Again I ask, "What is the minimum number of coconuts the explorers had to pick the day before?"
to do what? Maybe for each explorer to leave with at least 1 coconut (total picked around 10) each explorer to have at least 10 coconuts? (Then it's closer to 100).

I agree you can't split coconuts so each time it is: take = INT(pile/numberOfExplorers).

I also agree that negative or imaginary numbers would not be satisfactory solutions for this problem. :)

1 person likes this

bplus

#8
Quote from: Aurel on July 02, 2021, 17:05:07
what i have is a flying monkey
so i am wondering maybe you can made something similar in SB

Ehhhh Aurel showing off his new graphics image for Micro A LOL! But can it do String Literals without quotes? LOL

Flying monkey phffff... simple shooter???
1 person likes this

Aurel [banned]

Yo ....you hit me with coconut in head.. :P
:D

String literals..wahat is that ?  8)
You mean string variable ?
yes you can  ;D
(Y)

Aurel [banned]

just for B+
  ;D
(Y)

twgonder

#11
Quote from: bplus on July 03, 2021, 12:25:12

Again I ask, "What is the minimum number of coconuts the explorers had to pick the day before?"
to do what? Maybe for each explorer to leave with at least 1 coconut (total picked around 10) each explorer to have at least 10 coconuts? (Then it's closer to 100).

Let's start with your 10. Divide it into 5 piles of two coconuts. Oooops, there isn't one for the monkey.
Let's try a pile of 11. We get 5 piles of two and one for the monkey. So far, so good. Push four piles remaining (after hiding one) together for one pile of 8. The next explorer divides 8 by 5, Oooops, no 5 equal piles.
Now you can try again with 12 coconuts. Keep trying till you find a solution to the problem as stated.
(There is no other "to do what?")

bplus

#12
I have each explorer pay the monkey a coconut first so it doesn't raise hell while they take an integer amount of 1/5th the pile for 5 explorers.

So I need at least 17 coconuts for everyone to walk away with at least one coconut.

From a JB run coded 2018 (the following assumes 5 explorers/sailors):
Quote
   i (bluatigro) fount this puzle in a book :
     there are 5 person's + 1 monkey
     they shipwrek on a Island
     the fisrst day they colect kokonut's

day 1:
I (bplus) am starting with a number of coconuts.
Say the coconuts collected by the people is 17.

     in the night happens this :
     eats (each) person goes to the pile
     divides it in 5 hides 1/5
     and gives 1 to the monkey

I (bplus) am paying the monkey first.
I (bplus) am not cutting any coconuts into parts of coconuts.

So Coconut inventory after first night:
For person 1, their pile is 3
For person 2, their pile is 2
For person 3, their pile is 1
For person 4, their pile is 1
For person 5, their pile is 1
             and monkey has 5

day 2
      the 2e day
      thay divide the remaining pile in 5
      and give 1 to the monkey
      now calc the size of every pile

Coconuts starting day 2, before final divy is 4
after monkey gets one more coconut the divy is 0

For person 1, their pile is 3
For person 2, their pile is 2
For person 3, their pile is 1
For person 4, their pile is 1
For person 5, their pile is 1
The monkey has 6 coconuts.

Coconuts remaining are 3 for the monkey who programmed this inventory.



So @twgonder how many coconuts do you think is needed to be collected so that everyone walks away with at least 1 coconut?
1 person likes this

bplus

I need 22 coconuts before none get wasted at the end everyone walks away with at least 2:
Quote
   i (bluatigro) fount this puzle in a book :
     there are 5 person's + 1 monkey
     they shipwrek on a Island
     the fisrst day they colect kokonut's

day 1:
I (bplus) am starting with a number of coconuts.
Say the coconuts collected by the people is 22.

     in the night happens this :
     eats (each) person goes to the pile
     divides it in 5 hides 1/5
     and gives 1 to the monkey

I (bplus) am paying the monkey first.
I (bplus) am not cutting any coconuts into parts of coconuts.

So Coconut inventory after first night:
For person 1, their pile is 4
For person 2, their pile is 3
For person 3, their pile is 2
For person 4, their pile is 1
For person 5, their pile is 1
             and monkey has 5

day 2
      the 2e day
      thay divide the remaining pile in 5
      and give 1 to the monkey
      now calc the size of every pile

Coconuts starting day 2, before final divy is 6
after monkey gets one more coconut the divy is 1

For person 1, their pile is 5
For person 2, their pile is 4
For person 3, their pile is 3
For person 4, their pile is 2
For person 5, their pile is 2
The monkey has 6 coconuts.

Coconuts remaining are 0 for the monkey who programmed this inventory.


1 person likes this

bplus

#14
Quote from: Aurel on July 03, 2021, 15:52:25
Yo ....you hit me with coconut in head.. :P
:D

String literals..wahat is that ?  8)
You mean string variable ?
yes you can  ;D

Yo = "This is a string literal."

Yo is a string variable set to the string literal right of = sign.

In my Interpreter you can do this without double quotes:
Yo = This is a string literal.

This is print Yo code:
. Yo

Output is:
This is a string literal.

8)
1 person likes this