Skip to content

Instantly share code, notes, and snippets.

@Beraliv
Last active May 25, 2018 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Beraliv/688acd77cdc9c8a18f62e16ed3f7e222 to your computer and use it in GitHub Desktop.
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.
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
`)
@Beraliv
Copy link
Author

Beraliv commented Apr 19, 2018

FORMAT

${DATE}
${RUR_AMOUNT} -> ${EUR_AMOUNT} (${HACK_RATE}) ${REAL_RATE}
PROFIT: ${PROFIT} RUR

@Beraliv
Copy link
Author

Beraliv commented Apr 19, 2018

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

@Beraliv
Copy link
Author

Beraliv commented May 17, 2018

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