Five sailors are shipwrecked on an island and collect a large pile of coconuts during the day. That night the first sailor wakes up and decides to take his first share early so tries to divide the pile of coconuts equally into five piles but finds that there is one coconut left over, so he tosses it to a monkey and then hides "his" one of the five equally sized piles of coconuts and pushes the other four piles together to form a single visible pile of coconuts again and goes to bed. To cut a long story short, each of the sailors in turn gets up once during the night and performs the same actions of dividing the coconut pile into five, finding that one coconut is left over and giving that single remainder coconut to the monkey. In the morning (after the surreptitious and separate action of each of the five sailors during the night), the remaining coconuts are divided into five equal piles for each of the sailors, whereupon it is found that the pile of coconuts divides equally amongst the sailors with no remainder. (Nothing for the monkey in the morning.) http://rosettacode.org/wiki/Sailors,_coconuts_and_a_monkey_problem
Last active
December 10, 2015 15:44
-
-
Save sean-nicholas/9cd007de066bc6aa3ee5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Solver(startCoconuts, sailors) { | |
| this.coconuts = startCoconuts; | |
| this.sailors = sailors; | |
| this.calls = 0; | |
| } | |
| Solver.prototype.canBeDividedBySailorsMinusOne = function() { | |
| if ((this.coconuts / (this.sailors - 1)) % 1 === 0) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| Solver.prototype.start = function() { | |
| while (this.calls < this.sailors - 1) { | |
| if (!this.canBeDividedBySailorsMinusOne()) { | |
| return false; | |
| } | |
| this.calls++; | |
| this.coconuts = this.coconuts / (this.sailors - 1) * this.sailors + 1; | |
| } | |
| return this.coconuts; | |
| } | |
| var sailors = process.argv[2]; | |
| var coconuts = 0; | |
| var iteration = 0; | |
| var found = false; | |
| while (!found) { | |
| var solver = new Solver(iteration * sailors + 1, sailors); | |
| var solution = solver.start(); | |
| if (solution === false) { | |
| iteration++; | |
| } else { | |
| found = true; | |
| console.log(solution); | |
| } | |
| } | |
| //Usage | |
| //node better_coconut.js [sailors] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| coconut.js https://gist.github.com/rechenberger/3a173eaa83ad8812a203 | |
| ----------------------------------- | |
| 5: 3121 - Dauer: 18ms | |
| 6: 46651 - Dauer: 29ms | |
| 7: 823537 - Dauer: 142ms | |
| 8: 16777209 - Dauer: 2571ms | |
| 9: 387420481 - Dauer: 63113ms | |
| 10: ?????????? - Dauer: Aborted after 11 mins | |
| better_coconut.js | |
| ----------------------------------- | |
| 5: 3121 - Dauer: 17ms | |
| 6: 46651 - Dauer: 17ms | |
| 7: 823537 - Dauer: 21ms | |
| 8: 16777209 - Dauer: 64ms | |
| 9: 387420481 - Dauer: 865ms | |
| 10: 9999999991 - Dauer: 20986ms | |
| best_coconut.js | |
| ----------------------------------- | |
| 5: 3121 - Dauer: 20ms | |
| 6: 46651 - Dauer: 20ms | |
| 7: 823537 - Dauer: 10ms | |
| 8: 16777209 - Dauer: 20ms | |
| 9: 387420481 - Dauer: 20ms | |
| 10: 9999999991 - Dauer: 20ms | |
| 11: 285311670601 - Dauer: 20ms | |
| 12: 8916100448245 - Dauer: 20ms | |
| 13: 302875106592241 - Dauer: 20ms | |
| 14: ???????????????? - Integer overflow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Solver(startCoconuts, sailors) { | |
| this.coconuts = startCoconuts; | |
| this.sailors = sailors; | |
| this.calls = 0; | |
| } | |
| Solver.prototype.canBeDividedBySailorsMinusOne = function() { | |
| if ((this.coconuts / (this.sailors - 1)) % 1 === 0) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| Solver.prototype.start = function() { | |
| while (this.calls < this.sailors - 1) { | |
| if (!this.canBeDividedBySailorsMinusOne()) { | |
| return false; | |
| } | |
| this.calls++; | |
| this.coconuts = this.coconuts / (this.sailors - 1) * this.sailors + 1; | |
| } | |
| return this.coconuts; | |
| } | |
| var sailors = process.argv[2]; | |
| var iterations = 0; | |
| (function() { | |
| if (sailors == 1) { | |
| console.log(1); | |
| return; | |
| } | |
| var solution = 3; | |
| for (var i = 3; i <= sailors; i++) { | |
| var solver = new Solver((solution + i - 3) * i + 1, i); | |
| solution = solver.start(); | |
| } | |
| console.log(solution); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment