为什么我的魔方计算偏差了 4000?
Why is my rubik's cube calculation off by 4000?
我正在尝试将一些快速而肮脏的 javascript 代码放在一起,以便在给定约束条件(例如“已解决 1 个边缘块”)的情况下给出魔方上可能的块排列数。 (为简单起见,坚持使用 3x3)当我 运行 正常的 12 个边缘和 8 个角通过我的函数时,它给了我一个比我能够找到的答案大 4000 的数字。 (函数给了我 43,252,003,274,489,860,000 但 https://www.youtube.com/watch?v=z2-d0x_qxSM 说它应该是 43,252,003,274,489,856,000)
我的代码:
// 3x3x3 Rubik's Cube
edgepieces = 12;
cornerpieces = 8;
centerpieces = 6;
// total possible permutations in general
function numCombos(edges, corners) {
result = ((factorial(edges) * (factorial(corners)) / 2) * (2 ** (edges - 1)) * (3 ** (corners - 1)));
return result;
}
// n!
function factorial(x) {
if (x == 0) {
return 1;
} else {
return x * factorial(x - 1);
}
}
console.log(numCombos(edgepieces, cornerpieces) + '\n');
对于核心结果算法,我遵循了几种不同的安排,它们都给了我这个最终结果。我错过了什么?
双精度浮点数有 53 位精度,差不多是 16 位精度。[1] 您期望得到 17 位精度的结果。有可能这个数字甚至不能用双精度表示。
简单的解决方案是使用 BigInt 数字而不是浮点数。
- log10( 253 ) = 15.95...
您可以使用 BigInt
values 来避免浮点精度问题:
// 3x3x3 Rubik's Cube
edgepieces = 12n;
cornerpieces = 8n;
centerpieces = 6n;
// total possible permutations in general
function numCombos(edges, corners) {
result = ((factorial(edges) * (factorial(corners)) / 2n) * (2n ** (edges - 1n)) * (3n ** (corners - 1n)));
return result;
}
// n!
function factorial(x) {
if (x == 0) {
return 1n;
} else {
return x * factorial(x - 1n);
}
}
console.log(numCombos(edgepieces, cornerpieces) + '\n');
我正在尝试将一些快速而肮脏的 javascript 代码放在一起,以便在给定约束条件(例如“已解决 1 个边缘块”)的情况下给出魔方上可能的块排列数。 (为简单起见,坚持使用 3x3)当我 运行 正常的 12 个边缘和 8 个角通过我的函数时,它给了我一个比我能够找到的答案大 4000 的数字。 (函数给了我 43,252,003,274,489,860,000 但 https://www.youtube.com/watch?v=z2-d0x_qxSM 说它应该是 43,252,003,274,489,856,000)
我的代码:
// 3x3x3 Rubik's Cube
edgepieces = 12;
cornerpieces = 8;
centerpieces = 6;
// total possible permutations in general
function numCombos(edges, corners) {
result = ((factorial(edges) * (factorial(corners)) / 2) * (2 ** (edges - 1)) * (3 ** (corners - 1)));
return result;
}
// n!
function factorial(x) {
if (x == 0) {
return 1;
} else {
return x * factorial(x - 1);
}
}
console.log(numCombos(edgepieces, cornerpieces) + '\n');
对于核心结果算法,我遵循了几种不同的安排,它们都给了我这个最终结果。我错过了什么?
双精度浮点数有 53 位精度,差不多是 16 位精度。[1] 您期望得到 17 位精度的结果。有可能这个数字甚至不能用双精度表示。
简单的解决方案是使用 BigInt 数字而不是浮点数。
- log10( 253 ) = 15.95...
您可以使用 BigInt
values 来避免浮点精度问题:
// 3x3x3 Rubik's Cube
edgepieces = 12n;
cornerpieces = 8n;
centerpieces = 6n;
// total possible permutations in general
function numCombos(edges, corners) {
result = ((factorial(edges) * (factorial(corners)) / 2n) * (2n ** (edges - 1n)) * (3n ** (corners - 1n)));
return result;
}
// n!
function factorial(x) {
if (x == 0) {
return 1n;
} else {
return x * factorial(x - 1n);
}
}
console.log(numCombos(edgepieces, cornerpieces) + '\n');