Last active
May 25, 2018 18:43
-
-
Save Beraliv/688acd77cdc9c8a18f62e16ed3f7e222 to your computer and use it in GitHub Desktop.
Alfa bank. Which quantity of roubles is needed to buy euros for the lowest prices at the moment.
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
function digitAt(index, amount) { | |
if (index <= 0) { | |
throw new TypeError('index must be positive') | |
} | |
return Math.floor(amount * Math.pow(10, index) % 10) | |
} | |
function restAfter(index, amount) { | |
const indent = Math.pow(10, index) | |
const rest = amount * indent % indent | |
const restFloor = Math.floor(rest) | |
return amount - Math.floor(amount) - (rest - restFloor) | |
} | |
function round(amount, index = 2) { | |
const indent = Math.pow(10, index) | |
const rest = amount * indent % indent | |
const restFloor = Math.floor(rest) | |
return Math.floor(amount) + restFloor / indent + (rest - restFloor > 0.5 ? 1 / indent : 0) | |
} | |
function fix(amount, quantity = 2) { | |
const indent = Math.pow(10, quantity) | |
return Math.floor(amount * indent) / indent | |
} | |
function currency(floor = 0, k = 0, l = 0, p = 0) { | |
return floor + k / 10 + l / 100 + p / 1000 | |
} | |
function calcRurAndEur(floor, k, l, p, rate) { | |
const eur = currency(floor, k, l, 5) | |
const rur = fix(eur * rate) | |
if (restAfter(3, rur / rate) <= 0.005) { | |
return { | |
eur: currency(floor, k, l + 1), | |
rur: round(rur + 0.01) | |
} | |
} | |
return { | |
eur: currency(floor, k, l + 1), | |
rur: round(rur) | |
} | |
} | |
function calc(rate, rurMin = 10.00) { | |
const eurMin = rurMin / rate | |
// Number = floor.klp{other digits of fractional part} | |
const floor = Math.floor(eurMin) | |
const [k, l, p] = [digitAt(1, eurMin), digitAt(2, eurMin), digitAt(3, eurMin)] | |
if (p < 5) { | |
return calcRurAndEur(floor, k, l, 5, rate) | |
} | |
return calcRurAndEur(floor, k, l + 1, 5, rate) | |
} | |
function profit(rate, { eur, rur }) { | |
return rate - fix(rur / eur) | |
} | |
// EXAMPLE | |
const rate = 76 | |
const answer = calc(rate) | |
console.log(`${new Date()} | |
${answer.rur} -> ${answer.eur} (${fix(answer.rur / answer.eur)}) ${rate} | |
PROFIT: ${fix(profit(rate, answer))} RUR | |
`) |
Thu Apr 19 2018 12:27:48 GMT+0300 (Russia TZ 2 Standard Time)
10.27 -> 0.14 (73.35) 76
PROFIT: 2.65 RUR
Thu May 17 2018 09:16:43 GMT+0300 (Russia TZ 2 Standard Time)
10.66 -> 0.15 (71.06) 73.5
PROFIT: 2.43 RUR
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FORMAT