有效地生成所有的合数小于N(用自己的因式分解)自己的、因式分解、合数、有的

由网友(呆萌ω小甜心)分享简介:我想建立一个高效的Python迭代器/发电机收益率:I'd like to build an efficient Python iterator/generator that yields:小于N的所有合数随着他们的主要因子我称之为composites_with_factors()I'll call it "co...

我想建立一个高效的Python迭代器/发电机收益率:

I'd like to build an efficient Python iterator/generator that yields:

小于N的所有合数 随着他们的主要因子

我称之为composites_with_factors()

I'll call it "composites_with_factors()"

假设我们已经有了质数的名单小于N,或素数生成器,可以做同样的。

Assume we already have a list of primes less than N, or a primes generator that can do the same.

请注意,我:

在不需要数以数字顺序产生 请不要介意1产生的开头或不 请不要介意素数产生了,太

我想这可以用一个巧妙的递归生成器来完成...

I figure this can be done with a clever recursive generator...

因此​​,例如,调用composites_with_factors(16)可以得到:

So, for example, a call to composites_with_factors(16) may yield:

# yields values in form of "composite_value, (factor_tuple)"
2, (2)
4, (2, 2)
8, (2, 2, 2)
6, (2, 3)
12, (2, 2, 3)
10, (2, 5)
14, (2, 7)
3, (3)
9, (3, 3)
15, (3, 5)
5, (5)
7, (7)
11, (11)
13, (13)

正如你可以从我的输出顺序看,我由开始与现有的素数发生器的最小素数,并输出的主要少于N的一切权力想象这个工作的,然后再次尝试通过该权力黄金,但在每个阶段看,如果我可以申请额外的素数的权力(和仍比N为少)。当与黄金组合都做了,放弃它,并重复使用的素数生成下一个最低的素数。

As you can see from the order of my output, I conceive of this working by starting with the smallest prime on the available primes generator, and outputting all powers of that prime less than N, then try again through the powers of that prime but at each stage seeing if I can apply powers of additional primes (and still be less than N). When all combinations with THAT prime are done, drop it, and repeat with the next lowest prime number available on the primes generator.

我试图用递归生成做到这一点已经得到了我的时弹出与收益递归的,或抛出StopIteration异常很困惑,或回报,或干脆掉出递归函数

My attempts to do this with "recursive generators" have gotten me very confused on when to pop out of the recursion with "yield ", or "raise StopIteration", or "return", or simply fall out of the recursed function.

感谢您的智慧!

附加说明:

我的执行的现在要做这样一个办法:我写了一个功能因子的数字,这样我就可以因子他们到素数,而产生的结果。没问题。我把这种极快的依靠的是什么数N最低的首要因素......对于N高达10万元的缓存。

I do have one way to do this now: I have written a function to factor numbers, so I can factor them down to primes, and yield the results. No problem. I keep this blazingly fast by relying on a cache of "what is the lowest prime factor of number N"... for N up to 10 million.

但是,一旦我出了缓存,我们会,它转予为幼稚保理业务。 (呸)

However, once I'm out of the cache, we'll, it devolves to "naive" factoring. (Yuck.)

这篇文章的观点是:

在我假设,从他们的因素,产生了大量的复合材料会比大数复合材料快......尤其是因为我不关心顺序和 您怎么能有一个Python生成器递归自称,并产生所产生的东西一个流?

推荐答案

假设 primesiter(N)创建一个迭代器的所有素数高达ñ (1不应该被包含在 primesiter 或以下code以及输入INF。循环)

Assuming primesiter(n) creates an iterator over all primes up to n (1 should NOT be included in primesiter, or following code well enter inf. loop)

def composite_value(n, min_p = 0):
    for p in primesiter(n):
        # avoid double solutions such as (6, [2,3]), and (6, [3,2])
        if p < min_p: continue
        yield (p, [p])
        for t, r in composite_value(n//p, min_p = p): # uses integer division
            yield (t*p, [p] + r)

输出的

>> list(composite_value(16))
[(2, [2]),
 (4, [2, 2]),
 (8, [2, 2, 2]),
 (16, [2, 2, 2, 2]),
 (12, [2, 2, 3]),
 (6, [2, 3]),
 (10, [2, 5]),
 (14, [2, 7]),
 (3, [3]),
 (9, [3, 3]),
 (15, [3, 5]),
 (5, [5]),
 (7, [7]),
 (11, [11]),
 (13, [13])]

请注意:它包括N(= 16),以及,我用的元组的列表,而不是。如果需要,它们都可以很容易解决,但我会离开,作为一个练习。

NOTE: it includes n (= 16) as well, and I used list instead of tuples. Both can easily be resolved if needed, but I will leave that as an exercise.

阅读全文

相关推荐

最新文章