Skip to content

Instantly share code, notes, and snippets.

@qinlili23333
Last active October 14, 2021 14:09
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 qinlili23333/d682555ac132b83a823638ae65cabb5f to your computer and use it in GitHub Desktop.
Save qinlili23333/d682555ac132b83a823638ae65cabb5f to your computer and use it in GitHub Desktop.
同一天生日
function getBirthday() {
return Math.floor(Math.random() * 366)
}
function getBirthdayArray(num) {
var array = []
for (var i = 0; i < num; i++) {
array[i] = getBirthday()
}
return array
}
function isArrayRepeat3(array) {
var isOver3 = false;
var data = array.reduce(function (prev, next) {
prev[next] = (prev[next] + 1) || 1;
if (prev[next] >= 3) { isOver3 = true; }
return prev;
}, {});
return isOver3
}
function doVerify(people, repeat) {
var yes = 0
for (var i = 0; i < repeat; i++) {
if (isArrayRepeat3(getBirthdayArray(people))) {
yes++
}
}
return Number(yes) / Number(repeat)
}
function autoVerify(accuracy) {
var stopVerify = false
for (people = 3; !stopVerify; people++) {
var percent = doVerify(people, accuracy)
console.log("people:"+people+", percent:"+percent)
if (percent > 0.5) {
stopVerify = true
return people
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment