Skip to content

Commit

Permalink
calculate total bonus and cost if buy all numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgao0 committed Sep 5, 2023
1 parent 11c4929 commit c723339
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
60 changes: 40 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
var numeral = require('numeral');
var Combinatorics = require('js-combinatorics');
const { Combination } = require('js-combinatorics');

function printProgress(progress) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(progress);
}

(function () {
'use strict';
var winnings = []; // NTD
winnings.push({m: 6, s: null, bouns: 39299711});
winnings.push({m: 5, s: 1, bouns: 3230113});
winnings.push({m: 5, s: null, bouns: 49011});
winnings.push({m: 4, s: 1, bouns: 11777});
winnings.push({m: 4, s: null, bouns: 1655});
winnings.push({m: 3, s: 1, bouns: 1000});
winnings.push({m: 2, s: 1, bouns: 400});
winnings.push({m: 3, s: null, bouns: 400});
const costPerTicket = 50; // NTD
const randomNumbers = [1, 2, 3, 4, 5, 6];
const randomSpecialNumber = 7;
const prizes = {}; // NTD
prizes['6:0'] = { name: '頭獎', m: 6, s: null, bonus: 104538797, acc: 0 };
prizes['5:1'] = { name: '貳獎', m: 5, s: 1, bonus: 10089281, acc: 0 };
prizes['5:0'] = { name: '參獎', m: 5, s: null, bonus: 120323, acc: 0 };
prizes['4:1'] = { name: '肆獎', m: 4, s: 1, bonus: 18129, acc: 0 };
prizes['4:0'] = { name: '伍獎', m: 4, s: null, bonus: 2000, acc: 0 };
prizes['3:1'] = { name: '陸獎', m: 3, s: 1, bonus: 1000, acc: 0 };
prizes['2:1'] = { name: '柒獎', m: 2, s: 1, bonus: 400, acc: 0 };
prizes['3:0'] = { name: '普獎', m: 3, s: null, bonus: 400, acc: 0 };

const allNumbers = new Array(49).fill(1).map(function (v, i) {return v + i;});
const allCombinations = new Combination(allNumbers, 6);

var allNumbers = new Array(49).fill(1).map(function (v, i) {return v + i;}),
allCombinations = Combinatorics.bigCombination(allNumbers, 6);
console.log(`Cost for buying all combinations: NTD ${
new Intl.NumberFormat().format(allCombinations.length * BigInt(costPerTicket))
}`);

function hasBalls(nums, matchs) {
return matchs.every(function (match) {
return nums.indexOf(match) > -1;
});
let totalBonus = 0n;
for(let i = 0n; i < allCombinations.length; i++) {
const comb = allCombinations.nth(i);
const matchedNumbers = comb.filter(n => randomNumbers.includes(n)).length;
const matchedSpecialNumber = comb.includes(randomSpecialNumber) ? 1 : 0;
const prizeKey = `${matchedNumbers}:${matchedSpecialNumber}`;
const matchedPrize = prizes[prizeKey];
if (matchedPrize) {
matchedPrize.acc += matchedPrize.bonus;
totalBonus += BigInt(matchedPrize.bonus);
printProgress(`Progress: ${i + 1n}/${allCombinations.length}`);
}
}

console.log(allCombinations.filter(function (nums) {
return hasBalls(nums, [1, 2, 3, 4, 5, 6]);
}));
console.log(`Bonus if buying all tickets: NTD ${
new Intl.NumberFormat().format(totalBonus)
}`);
}());
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "try to analyze the winnings of lottery",
"main": "index.js",
"dependencies": {
"js-combinatorics": "^0.5.2",
"numeral": "^2.0.6"
"js-combinatorics": "2.1.1"
},
"devDependencies": {},
"scripts": {
Expand Down

0 comments on commit c723339

Please sign in to comment.