Posts: 597
Threads: 110
Joined: Apr 2022
Reputation:
34
You'll find my solution for this challenge (coded with BAM) here.
The gist of the challenge:
Quote:Once upon a time, 3 friends bought apples and carried them on a donkey.
When night came, they decided to sleep in the forest.
The first friend woke up hungry at midnight, so he divided the apples into three equal shares.
The one extra remaining apple, he gave it to the donkey. He then ate his share and went back to sleep.
A while later, the second friend woke up hungry, so he divided the remaining apples into three equal shares.
The one extra remaining apple, he gave it to the donkey. He then ate his share and went back to sleep.
A while later, the third friend woke up hungry, so he divided the remaining apples into three equal shares.
The one extra remaining apple, he gave it to the donkey. He then ate his share and went back to sleep.
In the morning, the 3 friends woke up hungry, so they divided the remaining apples into three equal shares.
The one extra remaining apple, they gave it to the donkey. They then ate each of their share.
How many apples did they initially buy, and how many apples did each of them eat?
Posts: 3,982
Threads: 178
Joined: Apr 2022
Reputation:
220
08-04-2024, 03:35 AM
(This post was last modified: 08-04-2024, 03:43 AM by bplus.)
Code: (Select All) For i = 1 To 4000
If i Mod 3 = 1 Then
i2 = i - Int(i / 3) - 1 '1st
If i2 Mod 3 = 1 Then
i3 = i2 - Int(i2 / 3) - 1 '2nd
If i3 Mod 3 = 1 Then
i4 = i3 - Int(i3 / 3) - 1 ' 3rd
If i4 Mod 3 = 1 Then
i5 = i4 - Int(i4 / 3) - 1 '4th
If i5 Mod 3 = 0 Then
Print i; "="; i2 / 2 + i5 / 2; "+"; i3 / 2 + i5 / 2; "+"; i4 / 2 + i5 / 2; "+"; 4
Exit For
End If
End If
End If
End If
End If
Next
This problem is similar to 5 Sailors, Coconuts and a Monkey at Rosetta Code. Find the minimum number to satisfy the conditions set by problem, I learned later at Syntax Bomb.
b = b + ...
Posts: 597
Threads: 110
Joined: Apr 2022
Reputation:
34
08-04-2024, 04:15 AM
(This post was last modified: 08-04-2024, 04:23 AM by CharlieJV.)
With the code I wrote, the first possible solution for number of apples bought: 79
Friend 3 ate a total of 18 apples.
Friend 2 ate a total of 24 apples.
Friend 1 ate a total of 33 apples.
The donkey ate 4 apples.
Friend 1 is a pig.
I'm not entirely convinced I got it right, so I look forward to confirmation one way or the other via other approaches.
Posts: 200
Threads: 5
Joined: Apr 2022
Reputation:
22
08-04-2024, 05:20 AM
(This post was last modified: 08-05-2024, 08:33 AM by JRace.
Edit Reason: (initialize DONKEYEATEN properly)
)
Okay try this.
My first version was a compact, concise little thing similar to @bplus's, but then I reread your challenge and realized I wasn't giving all the information required.
D'oh!! Chuck it in the #%@&-it bucket and start with fresh ingredients:
(unusually for me, there is no stunt programming or early optimization on this one... just keepin' it simple)
(this program makes no assumptions e.g. about how many apples the donkey eats; it just performs the calculations implied by the puzzle)
Code: (Select All)
Code: (Select All)
Dim C, T, DONKEYEATEN, F1EATEN, F2EATEN, F3EATEN, ALLEAT As Integer
Print "Total", "FatBoy", "Friend2", "Friend3", "Donkey"
For C = 1 To 1000
T = C
DONKEYEATEN = 1 'give the donkey an apple so he won't rat us out
T = T - 1
If (T Mod 3) = 0 Then
F1EATEN = T / 3 'friend #1 aka "fatboy".
T = T - F1EATEN
DONKEYEATEN = DONKEYEATEN + 1 'give the donkey an apple so he won't rat us out
T = T - 1
If (T Mod 3) = 0 Then
F2EATEN = T / 3
T = T - F2EATEN
DONKEYEATEN = DONKEYEATEN + 1 'give the donkey an apple so he won't rat us out
T = T - 1
If (T Mod 3) = 0 Then
F3EATEN = T / 3
T = T - F3EATEN
DONKEYEATEN = DONKEYEATEN + 1 'the donkey is sick of apples by now
T = T - 1
If (T Mod 3) = 0 Then
ALLEAT = T / 3
F3EATEN = F3EATEN + ALLEAT
F2EATEN = F2EATEN + ALLEAT
F1EATEN = F1EATEN + ALLEAT
Print C, F1EATEN, F2EATEN, F3EATEN, DONKEYEATEN
End If
End If
End If
End If
Next
Possible solutions up to 1000:
Posts: 2,183
Threads: 222
Joined: Apr 2022
Reputation:
104
While it appears the donkey got the short end of the stick that night, write a program, for the next day, showing how many apples came out of their _ _ _.
Pete
Shoot first and shoot people who ask questions, later.
Posts: 3,982
Threads: 178
Joined: Apr 2022
Reputation:
220
08-04-2024, 10:47 AM
(This post was last modified: 08-04-2024, 11:24 AM by bplus.)
Testing 79
79 / 3 = 26 R 1 79 - 26 - 1 = 52
52 / 3 = 17 R 1 52 - 17 - 1 = 34
34 / 3 = 11 R 1 34 - 11 - 1 = 22
22 / 3 = 7 R 1 so 7 more for each friend and donkey gets the 4 Remainders.
26 + 7 = 33 1st
17 + 7 = 24 2nd
11 + 7 = 18 3rd
+4 Donkey
= 79 It works alright I stand corrected
Where did I go wrong?
Code: (Select All) For i = 1 To 4000
If i Mod 3 = 1 Then
i2 = i - Int(i / 3) - 1 '1st
If i2 Mod 3 = 1 Then
i3 = i2 - Int(i2 / 3) - 1 '2nd
If i3 Mod 3 = 1 Then
i4 = i3 - Int(i3 / 3) - 1 ' 3rd
If i4 Mod 3 = 1 Then
i5 = i4 - Int(i4 / 3) - 1 '4th
'If i5 Mod 3 = 0 Then
Print i; "="; i2 / 2 + i5 / 2; "+"; i3 / 2 + i5 / 2; "+"; i4 / 2 + i5 / 2; "+"; 4
Exit For
'End If
End If
End If
End If
End If
Next
Aha! i5 mod 3 = 0 was not required!
Well it wouldn't be right to be first AND be correct ;-))
Quote:Friend 1 is a pig.
ha! Friend 1 was the earliest bird for the worm laden apples
b = b + ...
Posts: 3,982
Threads: 178
Joined: Apr 2022
Reputation:
220
(08-04-2024, 08:54 AM)Pete Wrote: While it appears the donkey got the short end of the stick that night, write a program, for the next day, showing how many apples came out of their _ _ _.
Pete
Code: (Select All) print 0; "apples came out that any reasonable, well fed human might eat."
b = b + ...
Posts: 597
Threads: 110
Joined: Apr 2022
Reputation:
34
08-04-2024, 12:47 PM
(This post was last modified: 08-04-2024, 12:55 PM by CharlieJV.)
(08-04-2024, 05:20 AM)JRace Wrote: Okay try this.
[...snip...]
Man, I love code that has comedic Easter eggs in 'em !!!
When I graduated from university eons ago, I got hired and immediately thrown into a project that was long over budget and late delivering.
Four months later, the project was delivered (but buggy), and I was the only team member kept on to eliminate the bugs. The other two Programmer's, the two analysis, the technical architect and the project manager all left.
Not long after, I find this gem of a comment in the code: "Completely non-standard way of designing a form. See my analyst."
Extra funny to me because of the friction between the 4GL Windows Programmer's and the COBOL Mainframe background analysts.
All of that aside: thanks! Nice to compare output results to validate one's own program with somebody else's work.
(08-04-2024, 08:54 AM)Pete Wrote: While it appears the donkey got the short end of the stick that night, write a program, for the next day, showing how many apples came out of their _ _ _.
Pete
Insta-thought: When one comes from a place where the men are men and the donkeys are nervous, the donkeys can only hope to get the short end of any stick late at night ...
Posts: 597
Threads: 110
Joined: Apr 2022
Reputation:
34
(08-04-2024, 10:47 AM)bplus Wrote: ha! Friend 1 was the earliest bird for the worm laden apples
The earliest bird may get the worm, but it is the second mouse that gets the cheese.
Posts: 3,982
Threads: 178
Joined: Apr 2022
Reputation:
220
LOL! oh man I had to think about that for more than second, then all the funnier!
b = b + ...
|