-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
calculate total bonus and cost if buy all numbers
- Loading branch information
Showing
4 changed files
with
71 additions
and
22 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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
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) | ||
}`); | ||
}()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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