admin 管理员组文章数量: 1086019
I am creating a simple bracket system and I need a way to check if there are a correct number of teams, OR if my program needs to pensate for bye rounds.
Right now, I am checking for "powers of two" with this function:
function validBracket(data) {
var x = data.teams.length;
return ((x != 0) && !(x & (x - 1)));
}
This works pretty well, but I am needing to know how many Bye rounds to add.
For instance, if I had 16 teams
, I would not need to add anymore teams. However, if I had 12 teams
, I would need the first 4 teams
to get a bye round.
How can I calculate number of bye rounds to add to my bracket? And would hard-coding an array of powers of two be better?
In pseudo code, something like this is what i was thinking of:
if(validateBracket(data)) {
// Valid number of teams (power of two). Keep going.
} else {
var byeRounds = calculateByeRounds();
}
NOTE: I would rather not use an array of powers of two like below:
var powersOfTwo = [2,4,8,16,32,...];
The reasoning behind this is that I would be limiting the number of teams that could be put in the system (however, I don't think a person would have over 256 teams).
I am creating a simple bracket system and I need a way to check if there are a correct number of teams, OR if my program needs to pensate for bye rounds.
Right now, I am checking for "powers of two" with this function:
function validBracket(data) {
var x = data.teams.length;
return ((x != 0) && !(x & (x - 1)));
}
This works pretty well, but I am needing to know how many Bye rounds to add.
For instance, if I had 16 teams
, I would not need to add anymore teams. However, if I had 12 teams
, I would need the first 4 teams
to get a bye round.
How can I calculate number of bye rounds to add to my bracket? And would hard-coding an array of powers of two be better?
In pseudo code, something like this is what i was thinking of:
if(validateBracket(data)) {
// Valid number of teams (power of two). Keep going.
} else {
var byeRounds = calculateByeRounds();
}
NOTE: I would rather not use an array of powers of two like below:
var powersOfTwo = [2,4,8,16,32,...];
The reasoning behind this is that I would be limiting the number of teams that could be put in the system (however, I don't think a person would have over 256 teams).
Share Improve this question asked Jun 14, 2015 at 2:37 Hunter MitchellHunter Mitchell 7,30318 gold badges75 silver badges119 bronze badges 2- 2 Calculate the next power of 2 here and subtract from your current integer – Drakes Commented Jun 14, 2015 at 2:42
- Hint: Each match eliminates one player. So a tournament of 12 players requires 11 matches. Tournaments are easy to design for powers of two, so make 12 players up to 16 by introducing 4 fictitious opponents. These are the byes. – Colonel Panic Commented Jun 16, 2015 at 10:57
2 Answers
Reset to default 11var needed = (1 << Math.ceil(Math.log2(n))) - n;
More generalized solution for extreme cases:
var needed = Math.pow(2, Math.ceil(Math.log2(n))) - n;
const n = 2;
// Convert n to binary representation as a string and count the number of '1' characters
const count = n.toString(2).split('').filter(bit => bit === '1').length;
// Return true if there is exactly one '1' bit in the binary representation of n
const result = count === 1 ? `${n} is power of two` : `${n} is NOT power of two`;
console.log(result);
本文标签: javascriptDetermining Powers of 2Stack Overflow
版权声明:本文标题:javascript - Determining Powers of 2? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743999844a2516241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论