SyntaxBomb - Indie Coders

Languages & Coding => SmallBASIC => Topic started by: bplus on February 13, 2021, 06:40:51

Title: Sailors and coconuts problem
Post by: bplus on February 13, 2021, 06:40:51
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:
Code: [Select]
' 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 :)


Title: Re: Sailors and coconuts problem
Post by: 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
Title: Re: Sailors and coconuts problem
Post by: round157 on July 02, 2021, 10:26:06
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!)
Title: Re: Sailors and coconuts problem
Post by: bplus on July 02, 2021, 11:35:02
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...

Title: Re: Sailors and coconuts problem
Post by: bplus on July 02, 2021, 11:45:58
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).
Title: Re: Sailors and coconuts problem
Post by: 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
Title: Re: Sailors and coconuts problem
Post by: twgonder on July 02, 2021, 20:33:18
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.
Title: Re: Sailors and coconuts problem
Post by: bplus on July 03, 2021, 12:25:12
Quote
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.)

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. :)

Title: Re: Sailors and coconuts problem
Post by: bplus on July 03, 2021, 12:27:14
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???
Title: Re: Sailors and coconuts problem
Post by: 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
Title: Re: Sailors and coconuts problem
Post by: Aurel on July 03, 2021, 16:47:09
just for B+
  ;D
Title: Re: Sailors and coconuts problem
Post by: twgonder on July 03, 2021, 16:48:23

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?")
Title: Re: Sailors and coconuts problem
Post by: bplus on July 03, 2021, 21:37:15
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?
Title: Re: Sailors and coconuts problem
Post by: bplus on July 03, 2021, 22:03:02
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.


Title: Re: Sailors and coconuts problem
Post by: bplus on July 03, 2021, 22:10:35
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)
Title: Re: Sailors and coconuts problem
Post by: twgonder 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.
Title: Re: Sailors and coconuts problem
Post by: bplus 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!
Title: Re: Sailors and coconuts problem
Post by: twgonder 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.
Title: Re: Sailors and coconuts problem
Post by: bplus 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 If
Next

So the next problem is to generalize this for s sailors.


Title: Re: Sailors and coconuts problem
Post by: bplus 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.
Title: Re: Sailors and coconuts problem
Post by: bplus 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 "; s
For 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 skip
Next

PS might have to put a higher limit for i in first For loop when sailors s exceed 5.
Title: Re: Sailors and coconuts problem
Post by: bplus 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
Title: Re: Sailors and coconuts problem
Post by: bplus on July 04, 2021, 03:22:09
Here is screen shot on 6 sailors which matches answers at Rosetta Code:
Title: Re: Sailors and coconuts problem
Post by: twgonder 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?
Title: Re: Sailors and coconuts problem
Post by: bplus 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 ;-))
Title: Re: Sailors and coconuts problem
Post by: twgonder 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.)
Title: Re: Sailors and coconuts problem
Post by: bplus 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,
SimplePortal 2.3.6 © 2008-2014, SimplePortal