Created
October 31, 2015 00:52
-
-
Save RHavar/bcd39c2cca2a09c6d2eb to your computer and use it in GitHub Desktop.
This file contains 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
// let payouts be the array payouts details (like from /bets/custom) | |
// let details.outcome be the final outcome of the bet | |
// Given the outcome, find out how many times it went right | |
var rights = payouts.findIndex(function(p) { | |
return details.outcome >= p.from && details.outcome < p.to; | |
}); | |
assert(rights >= 0); | |
var directionCombinations = binom(choices, rights); | |
// Now we will convert from where details.outcome lies in the range | |
var payoutsRange = payouts[rights].to - payouts[rights].from; | |
var ith = ((details.outcome - payouts[rights].from) / payoutsRange) * (directionCombinations-1); | |
assert(ith >= 0, ith <= directionCombinations-1); | |
ith = Math.round(ith); | |
// path is the path! | |
var path = bitseq(choices, rights, ith); | |
function binom(n,k) { | |
k = Math.min(k, n - k); | |
assert(k >= 0); | |
var r = 1; | |
for (var i = 0; i < k; ++i) | |
r = (r * (n - i)) / (i + 1); | |
return r; | |
} | |
// n is how many layers | |
// k is how many rights it made | |
// i it the ith value | |
function bitseq(n, k, i) { | |
if (k > n || k < 0) return null; | |
if (n === k && i == 0) return Array.apply(null, new Array(n)).map(String.prototype.valueOf, 'R').join(''); | |
if (n === k) return null; | |
var m = binom(n-1, k); | |
if (i < m) { | |
var seq = bitseq(n-1, k, i); | |
if (seq === null) return null; | |
return 'L' + seq; | |
} | |
var seq = bitseq(n-1, k-1, i-m); | |
if (seq === null) return null; | |
return 'R' + seq; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment