在 Haskell 中实现因式分解方法
我正在 Project Euler 上做 问题 266 并经过一些搜索,发现这个方法快速找出一个数的因数。你要做的就是找到一个数的质因数的所有排列:这些就是它的因数。
我已经有一个模块来查找数字的素数功率因数,例如:
Main> primePowerFactors 196
[(2,2),(7,2)]
这基本上表明:2^2 * 7^2 == 196
。从这里我想找到这些幂的所有排列,从而给出 196 的因子:
- (2^0)(7^0) = 1
- (2^1)(7^0) = 2
- (2^2) (7^0) = 4
- (2^0)(7^1) = 7
- (2^1)(7^1) = 14
- (2^2)(7^1) = 28
- (2^0)(7 ^2) = 49
- (2^1)(7^2) = 98
我想出了以下结果:
factors n = [a|a<-map facs (primePowerFactors n), y <- [0..(snd $ last $ primePowerFactors n)]]
where
facs (x,y) = (x,y)
rSq n = toInteger $ round $ sqrt (fromInteger n)
psr n = last $ dropWhile (<= rSq n) $ factors n
p = foldl' (*) 1 $ takeWhile (< 190) primes
answer = (psr p) `mod` (10^16)
但我的问题是 factors
无法正常工作。我希望它对每个质因数的所有可能的指数值进行排列,然后找到给出该因数的乘积。如何修改它以返回 n 的因子?
I am doing question 266 at Project Euler and after a bit of searching, found this method of quickly finding the factors of a number. What you do is find all the permutations of the prime factors of a number: these are its factors.
I already have a module to find the prime power factors of a number, eg:
Main> primePowerFactors 196
[(2,2),(7,2)]
This is basically showing that: 2^2 * 7^2 == 196
. From here I want to find all the permutations of those powers, to give the factors of 196 thusly:
- (2^0)(7^0) = 1
- (2^1)(7^0) = 2
- (2^2)(7^0) = 4
- (2^0)(7^1) = 7
- (2^1)(7^1) = 14
- (2^2)(7^1) = 28
- (2^0)(7^2) = 49
- (2^1)(7^2) = 98
I came up with the following:
factors n = [a|a<-map facs (primePowerFactors n), y <- [0..(snd $ last $ primePowerFactors n)]]
where
facs (x,y) = (x,y)
rSq n = toInteger $ round $ sqrt (fromInteger n)
psr n = last $ dropWhile (<= rSq n) $ factors n
p = foldl' (*) 1 $ takeWhile (< 190) primes
answer = (psr p) `mod` (10^16)
But my problem is that factors
doesn't work properly. I want it to permute through all the possible values of the exponent for each prime factor and then find the product to give the factor. How can it be modified to return the just the factors of n?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于一些高尔夫代码,我编写了一个非常简单的很好的幂集函数。
该算法的一个缺陷是它不包括原始集,但这对您来说是完美的,因为它看起来不像您想要的。
将此与将您的primePowerFactors n 转换为整数列表的方法相结合,假设
使用这些助手,可以使用数字n 生成因子列表
这种算法可能有点难用列表理解的方式表达。
for some code golf i wrote a nice power set function that is pretty simple.
A deficiency of this algorithm is that it doesn't include the original set, however that is perfect for you since it doesn't look like you want it.
combine this with a way to convert your
primePowerFactors n
into a list of ints, lets sayusing these helpers, a list of factors from a number n is generated with
This sort of algorithm is probably a bit harder to express in terms of a list comprehension.
首先,
facs
是恒等函数:y
绑定在左侧的模式匹配中,而您可能希望它是y< /code> 来自列表理解。列表推导式中绑定的变量名称是该推导式的本地变量名称,不能在不同的范围(如
where
子句)中使用。此外,列表推导式中的 y 仅根据最后一个因子的指数计算:
对于每个因子,应考虑其自身的指数,而不仅仅是最后一个因子的指数。
一个更基本的问题是,factors 函数的返回类型似乎与它的意图不符:
这返回质因数的幂列表,而它应该生成这些构造的列表,例如this:
需要对所有可能的因子进行素因数分解,但该函数似乎只返回一个素因数分解。
First of all,
facs
is the identity function:The
y
is bound in the pattern match on the left hand side while you probably intended it to be they
from the list comprehension. Variable names bound in a list comprehension are local to that comprehension and can not be used in a different scope like thewhere
clause.Also, the
y
in the list comprehension is only computed from the last factors exponent:For each factor it's own exponent should be considered, not just always the exponent of the last factor.
A more fundamental problem is, that the return type of the
factors
function doesn't seem to match it's intention:This returns a list of powers of prime factors while it should produce a list of these constructs, like this:
The prime factorization of all possible factors is needed, but the function seems to return just one prime factorization.