Javascript 中的组合算法返回数字的所有可能组合

发布于 2024-12-19 05:44:40 字数 456 浏览 2 评论 0 原文

编辑:

我需要在Javascript中实现组合算法,其结果与维基百科右侧的数字。使用给定的数字(n),该函数将能够返回所有可能的分隔,例如

2: [1,1], [2] (2 sets)
3: [1,1,1], [1,2], [2,1], [3] (4 sets)
4: [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [3,1], [1,3], [4] (8 sets)

理想情况下应该是一个函数接受并执行回调2^(n-1)次。我会接受任何我能理解(并重写)的语言的答案。谢谢!

Edit:

I need to implement composition algorithm in Javascript, where the result would be the same as the figure on the right in Wikipedia.With a given number (n), the function would be able to return all possible separations, e.g.

2: [1,1], [2] (2 sets)
3: [1,1,1], [1,2], [2,1], [3] (4 sets)
4: [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [3,1], [1,3], [4] (8 sets)

Ideally it should be a function accept and execute callback 2^(n-1) times. I will accept answers in any language I could understand (and rewrite from). Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七月上 2024-12-26 05:44:40

Prusswan 可能已经给出了最好的答案(算法的名称),但我忍不住写了一些 JavaScript:

function separate(n, callback) {
    for (var i=1; i<n; i++) {
        separate(n-i, function(ret) {
            ret.push(i);
            callback(ret);
        });
    }
    callback([n]);
}

Prusswan might have already given the best answer (the name of the algorithm), but I couldn't resist writing some javascript:

function separate(n, callback) {
    for (var i=1; i<n; i++) {
        separate(n-i, function(ret) {
            ret.push(i);
            callback(ret);
        });
    }
    callback([n]);
}
辞别 2024-12-26 05:44:40

刚刚自己搞定了这个功能:

/* Return all possibile compositions of a given natural number
* callback will be called 2^n-1 times.
*
* ref: http://en.wikipedia.org/wiki/Composition_(number_theory)
*/

function compositionsOf(n, callback) {
    var x, a, j;
    x = 1 << n-1;
    while (x--) {
        a = [1];
        j = 0;
        while (n-1 > j) {
            if (x & (1 << j)) {
                a[a.length-1]++;
            } else {
                a.push(1);
            }
            j++;
        }
        callback.call(this, a);
    }
};

Just worked out the function on my own:

/* Return all possibile compositions of a given natural number
* callback will be called 2^n-1 times.
*
* ref: http://en.wikipedia.org/wiki/Composition_(number_theory)
*/

function compositionsOf(n, callback) {
    var x, a, j;
    x = 1 << n-1;
    while (x--) {
        a = [1];
        j = 0;
        while (n-1 > j) {
            if (x & (1 << j)) {
                a[a.length-1]++;
            } else {
                a.push(1);
            }
            j++;
        }
        callback.call(this, a);
    }
};
微凉 2024-12-26 05:44:40
def partition(i):
    lst = [1]
    while i:
        if i & 1:
            lst.append(1)
        else:
            lst[-1] += 1
        i >>= 1
    del lst[-1]
    return lst

要求 2**(n-1) <= i < 2**n。

def partition(i):
    lst = [1]
    while i:
        if i & 1:
            lst.append(1)
        else:
            lst[-1] += 1
        i >>= 1
    del lst[-1]
    return lst

Call for 2**(n-1) <= i < 2**n.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文