Ooops
September 28, 2021, 06:09:00

### Author Topic: Sailors and coconuts problem  (Read 1057 times)

#### twgonder

• Jr. Member
• Posts: 29
##### Re: Sailors and coconuts problem
« Reply #15 on: July 04, 2021, 00:28:14 »
Lets try 17 first. 17 - 1 = 16; 16 / 5 = 3.2 coconuts. Fail, has to be a whole number of coconuts.
Lets try 21 next. 21 -1 = 20; 20 / 5 = 4, hide one pile of 4, 4 x 4 = 16; 16 - 1 = 15; 15 / 5 = 3; 4 x 3 =12; 12 - 1 = 11; 11 / 5 = 2.2. Fail for explorer #3
Neither 17 nor 21 are a solution.
Below is a png for just 2 sailors/explorers.

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #16 on: July 04, 2021, 00:30:34 »
16/5 is 5 piles of 3 with 1 left over so first guy takes 3 in my coding world.

You are just not desperate enough for coconuts I think! LOL wait until you get stranded on that island!
« Last Edit: July 04, 2021, 00:32:09 by bplus »

#### twgonder

• Jr. Member
• Posts: 29
##### Re: Sailors and coconuts problem
« Reply #17 on: July 04, 2021, 01:08:26 »
That (16) works for the first explorer that wakes up. But the night has four more explorers/sailors doing the same pile division.
Before a programmer can begin to solve a problem, s/he first must understand it.
Once they understand the problem, then they can make a plan of attack with code.

I included the code above. I still have more work to do on it.
The first solution that works for 5 explorers/sailors is 3,121 coconuts.

What started me on this thread was the problem where trial (the first column) is missing 3,897
and it always turns up missing in each of my runs, so I don't think it's just a buffer problem.
I want to get into the debugger and see what's going on, to do that it would be nice to have a "show output" page that allowed for variable inspection.  If I remember correctly, didn't most old interpreted BASICs do that?
Also, a statement like this would be a nice new(?) feature:
nn If trial = 3897 then debug 'and it would trap into the debugger mode in the show output page

Also, if it wouldn't be too hard, here's an additional request from another BASIC I used for operators:
eq = =
ne = <>
ge = >=
le = <=
With these Boolean operators it's easy to differentiate math operations from comparisons.
This might not be compatible with old programs as these would become reserved and not available as variable names.
« Last Edit: July 04, 2021, 01:17:18 by twgonder »

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #18 on: July 04, 2021, 01:49:46 »
OK here is the actual problem presented at Rosetta Code:
http://rosettacode.org/wiki/Sailors,_coconuts_and_a_monkey_problem

So we want a special number that keeps dividing by 5 and getting 1 left over (for the monkey 5 times at night) subtract the 5th + the 1 leftover 5 times but not in the morning of the final divy monkey gets none because 5 divides final number.

Turns out to be 3121.

Before you can understand the problem it must be presented to you correctly, I didn't get the problem presented correctly.

Here's a case where QB64 and SmallBASIC work the same!
Code: [Select]
`For i = 1 To 4000    If i Mod 5 = 1 Then        i2 = i - Int(i / 5) - 1        If i2 Mod 5 = 1 Then            i3 = i2 - Int(i2 / 5) - 1            If i3 Mod 5 = 1 Then                i4 = i3 - Int(i3 / 5) - 1                If i4 Mod 5 = 1 Then                    i5 = i4 - Int(i4 / 5) - 1                    If i5 Mod 5 = 1 Then                        i6 = i5 - Int(i5 / 5) - 1                        If i6 Mod 5 = 0 Then                            Print i                        End If                    End If                End If            End If        End If    End IfNext`
So the next problem is to generalize this for s sailors.

« Last Edit: July 04, 2021, 02:25:21 by bplus »

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #19 on: July 04, 2021, 02:31:37 »
3897 is showing up on my screen, maybe for some screwy reason your screen has a little jump in scrolling, the code is good it seems to me. Try a different sized screen?

Here is a debug test: tell the code to stop right after it prints line for 3897.
« Last Edit: July 04, 2021, 03:16:07 by bplus »

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #20 on: July 04, 2021, 02:54:21 »
OK I have it generalized for s sailors, at least this code works correctly for 5.

A slight difference between QB64 and SmallBASIC for label makers:
Code: [Select]
`Input "Please enter number of sailors that collect coconuts "; sFor i = 1 To 4000    n = i    For j = 1 To s        If n Mod s = 1 Then            n = n - Int(n / s) - 1        Else            GoTo skip        End If    Next    If n Mod s = 0 Then Print i: End    skip:  ' <<< for SmallBASIC change this line to label skipNext`
PS might have to put a higher limit for i in first For loop when sailors s exceed 5.
« Last Edit: July 04, 2021, 02:59:19 by bplus »

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #21 on: July 04, 2021, 03:10:16 »
OK it worked for 4 sailors saying 765 coconuts:
Quote
765 for 4 sailors

765 / 4 = 191.25
765 - 192 = 573   thats one

573 / 4 = 143.25
573 - 144 = 429   thats 2

429 / 4 = 107.25
429 - 108 = 321   thats 3

321 / 4 = 80.25
321 - 81 = 240    thats 4

and 240 mod 4 = 0 no remainder

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #22 on: July 04, 2021, 03:22:09 »
Here is screen shot on 6 sailors which matches answers at Rosetta Code:

#### twgonder

• Jr. Member
• Posts: 29
##### Re: Sailors and coconuts problem
« Reply #23 on: July 04, 2021, 03:26:20 »
Before you can understand the problem it must be presented to you correctly, I didn't get the problem presented correctly.

So what do you object to in the presentation of the problem?
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?

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #24 on: July 04, 2021, 03:48:29 »
Not your presentation, I thought I knew the problem from what was presented at JB in 2018.

I did not know the pile always had a mod s = 1 for as many sailors as there were and then a mod s = 0 for final divy. I thought it was an inventory problem not a number with special properties problem.

I think I can match Ruby's 100 sailors (at Rosetta Code) with my Interpreter that can do math for thousands of digits.

Wait... that would mean count up to that number, nope we dont have all century ;-))
« Last Edit: July 04, 2021, 04:00:51 by bplus »

#### twgonder

• Jr. Member
• Posts: 29
##### Re: Sailors and coconuts problem
« Reply #25 on: July 04, 2021, 04:13:23 »
3897 is showing up on my screen, maybe for some screwy reason your screen has a little jump in scrolling, the code is good it seems to me. Try a different sized screen?

Here is a debug test: tell the code to stop right after it prints line for 3897.

Very good suggestion. A couple of things to note that may be bugs.
1. I did a pause when trial reaches 3897 and indeed row 3897shows, after pressing a key the program continues and the line disappears (or is overwritten). This happens in a maximized window. The problem goes away with a minimized window.
2. The program runs very fast in minimized window (like immediate for 5 sailors, but for several seconds in maximized window. Hmmm
3. Changing between maximize and minimize window size does some very strange things, like changing the size of the window and other creepy things that may be different in different computers.

(Please forgive if I use the wrong terms for the windows, as mine is a Spanish version of Windows 10, so I'm not sure what Windows calls them in English.)

#### bplus

• Full Member
• Posts: 221
##### Re: Sailors and coconuts problem
« Reply #26 on: July 04, 2021, 11:37:02 »
3897 is showing up on my screen, maybe for some screwy reason your screen has a little jump in scrolling, the code is good it seems to me. Try a different sized screen?

Here is a debug test: tell the code to stop right after it prints line for 3897.

Very good suggestion. A couple of things to note that may be bugs.
1. I did a pause when trial reaches 3897 and indeed row 3897shows, after pressing a key the program continues and the line disappears (or is overwritten). This happens in a maximized window. The problem goes away with a minimized window.
2. The program runs very fast in minimized window (like immediate for 5 sailors, but for several seconds in maximized window. Hmmm
3. Changing between maximize and minimize window size does some very strange things, like changing the size of the window and other creepy things that may be different in different computers.

(Please forgive if I use the wrong terms for the windows, as mine is a Spanish version of Windows 10, so I'm not sure what Windows calls them in English.)

Yes I've experienced same in earlier versions. My solution is to get the maximized Window/SmallBASIC screen the size you like and ALWAYS run SmallBASIC maximized, XMAX and YMAX (Screen height and width constants) will be constant and dependable,