While working on Rosetta Code challenges I was reminded of this one and could not find my QB64 code for what I had started.
So I went back to Syntax Bomb and copied a couple of posts, and now I found the generalized one here in this thread and refining the presentation report for 2 to 9 sailors:
_Title "Sailors, coconuts and a monkey - Rosetta Code" 'b+ discussed at Syntax Bomb 2021-07-04 ' ref https://www.syntaxbomb.com/smallbasic/sailors-and-coconuts-problem/msg347051139/#msg347051139
'OK here is the actual problem presented at Rosetta Code:
' ref 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!
'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
' ref https://www.syntaxbomb.com/smallbasic/sailors-and-coconuts-problem/msg347051141/#msg347051141
'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:
'Input "Please enter number of sailors that collect coconuts "; s
'If s = 6 Then limit = 250000
'If s < 6 Then limit = 4000
'For i = 1 To limit
' 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: Exit For
' 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.
' Now Sunday, 2021-09-05 refined the generalized version to show what each sailor takes at night
' and then the final divy.
monkey = 1
For TotalSailors
= 2 To 9 For coconuts
= 1 To 500000000 n = coconuts
report$
= Chr$(10) + Str$(TotalSailors
) + " sailors require a minimum of " + _Trim$(Str$(coconuts
)) + " coconuts." + Chr$(10) For sailor
= 1 To TotalSailors
take
= Int(n
/ TotalSailors
) n = n - take - monkey
report$
= report$
+ Space$(5) + "Sailor" + Str$(sailor
) + " takes" + Str$(take
) + " and 1 goes to monkey." + Chr$(10) divy = n / TotalSailors
report$
= report$
+ Space$(5) + "The final divy for each sailor is" + Str$(divy
) + "." + Chr$(10) ' print whole report for first good coconuts number
continue:
Print "Press any to continue...";
'Sleep 'comment out when writing the file
Here is the output report:
2 sailors require a minimum of 11 coconuts.
Sailor 1 takes 5 and 1 goes to monkey.
Sailor 2 takes 2 and 1 goes to monkey.
The final divy for each sailor is 1.
3 sailors require a minimum of 25 coconuts.
Sailor 1 takes 8 and 1 goes to monkey.
Sailor 2 takes 5 and 1 goes to monkey.
Sailor 3 takes 3 and 1 goes to monkey.
The final divy for each sailor is 2.
4 sailors require a minimum of 765 coconuts.
Sailor 1 takes 191 and 1 goes to monkey.
Sailor 2 takes 143 and 1 goes to monkey.
Sailor 3 takes 107 and 1 goes to monkey.
Sailor 4 takes 80 and 1 goes to monkey.
The final divy for each sailor is 60.
5 sailors require a minimum of 3121 coconuts.
Sailor 1 takes 624 and 1 goes to monkey.
Sailor 2 takes 499 and 1 goes to monkey.
Sailor 3 takes 399 and 1 goes to monkey.
Sailor 4 takes 319 and 1 goes to monkey.
Sailor 5 takes 255 and 1 goes to monkey.
The final divy for each sailor is 204.
6 sailors require a minimum of 233275 coconuts.
Sailor 1 takes 38879 and 1 goes to monkey.
Sailor 2 takes 32399 and 1 goes to monkey.
Sailor 3 takes 26999 and 1 goes to monkey.
Sailor 4 takes 22499 and 1 goes to monkey.
Sailor 5 takes 18749 and 1 goes to monkey.
Sailor 6 takes 15624 and 1 goes to monkey.
The final divy for each sailor is 13020.
7 sailors require a minimum of 823537 coconuts.
Sailor 1 takes 117648 and 1 goes to monkey.
Sailor 2 takes 100841 and 1 goes to monkey.
Sailor 3 takes 86435 and 1 goes to monkey.
Sailor 4 takes 74087 and 1 goes to monkey.
Sailor 5 takes 63503 and 1 goes to monkey.
Sailor 6 takes 54431 and 1 goes to monkey.
Sailor 7 takes 46655 and 1 goes to monkey.
The final divy for each sailor is 39990.
8 sailors require a minimum of 117440505 coconuts.
Sailor 1 takes 14680063 and 1 goes to monkey.
Sailor 2 takes 12845055 and 1 goes to monkey.
Sailor 3 takes 11239423 and 1 goes to monkey.
Sailor 4 takes 9834495 and 1 goes to monkey.
Sailor 5 takes 8605183 and 1 goes to monkey.
Sailor 6 takes 7529535 and 1 goes to monkey.
Sailor 7 takes 6588343 and 1 goes to monkey.
Sailor 8 takes 5764800 and 1 goes to monkey.
The final divy for each sailor is 5044200.
9 sailors require a minimum of 387420481 coconuts.
Sailor 1 takes 43046720 and 1 goes to monkey.
Sailor 2 takes 38263751 and 1 goes to monkey.
Sailor 3 takes 34012223 and 1 goes to monkey.
Sailor 4 takes 30233087 and 1 goes to monkey.
Sailor 5 takes 26873855 and 1 goes to monkey.
Sailor 6 takes 23887871 and 1 goes to monkey.
Sailor 7 takes 21233663 and 1 goes to monkey.
Sailor 8 takes 18874367 and 1 goes to monkey.
Sailor 9 takes 16777215 and 1 goes to monkey.
The final divy for each sailor is 14913080.
Now I see that david_uwi has offered an interesting new angle of approach. Sure could use a speedup for 8 and 9 sailors! ;-))
387420481 coconuts minimum in one day collected by 9 sailors, ha!, this is a theoretical math problem isn't it?!