【问题标题】:Why is my rubik's cube calculation off by 4000?为什么我的魔方计算值减少了 4000?
【发布时间】:2021-11-10 17:07:25
【问题描述】:

我正在尝试将一些快速而肮脏的 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');

我对核心结果算法遵循了几种不同的安排,它们都给了我这个最终结果。我错过了什么?

【问题讨论】:

    标签: javascript rubiks-cube


    【解决方案1】:

    您可以使用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');

    【讨论】:

    • 修好了!我有一些阅读要做,非常感谢你
    【解决方案2】:

    双精度浮点数的精度为 53 位,几乎是 16 位精度。[1] 您期望得到 17 位精度的结果。有可能这个数字甚至不能用双精度表示。

    简单的解决方案是使用BigInt 数字而不是浮点数。


    1. log10( 253 ) = 15.95...

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 2012-10-18
      • 2010-12-21
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      相关资源
      最近更新 更多