Haskell中的主要分解要返回赋予数字和功率的元素列表
我一直在尝试通过尝试一些简单的问题来学习Haskell。
;
目前,我正在尝试实现函数 primeFactorization :: Integer - &gt [(整数,整数)]
,使输出是包含质量因子及其在数字中提高功率的元组列表。
示例输出
> PrimeFactorization 120
[(2,3),(3,1),(5,1)]
自120 = 2^3 * 3 * 3^1 * 5^1
我的(部分)解决方案
primeFactorization :: Integer -> [Integer]
primeFactorization n =
let
factors :: Integer -> [Integer]
factors n = [x | x <- [2..n-1], n `mod` x == 0]
isPrime :: Integer -> Bool
isPrime n
| n `elem` [0, 1] = False
| n == 2 = True
| n > 2 = null [ x | x <- [2..(ceiling . sqrt . fromIntegral) n], n `mod` x == 0]
| otherwise = False
in
filter isPrime $ (factors n)
这是一个工作实现获得数字的主要因素。但是,如下所见,它仅输出主要因素。我不确定如何将次数存储在Haskell中。另外,考虑到在Haskell中进行迭代是不合时宜的,我不知道如何实施该解决方案。在Python中,我会这样做:
def pf(number):
factors=[]
d=2
while(number>1):
while(number%d==0):
factors.append(d)
number=number/d
d+=1
return factors
因此,问题:如何实现主要因素的功能?
注意:
- 我已经看到了:阶乘分解,但这并没有回答我的问题。
- 这不是家庭作业问题,我正在独立学习。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以用递归替换命令式循环(只要它们不介入任何全球状态)。这可能不是最优雅的方法,但是在这种情况
下辅助功能并在累加器变量上向前计算,但这更尴尬,我认为在这种情况下没有记忆/性能的好处可以证明它是合理的。)
您可以将其与您的Haskell代码(对于您已经找到的每个因素,检查一次发生的频率)或扩展它,以便整个事情都像Python解决方案一样工作(实际上更有效,因为它避免了每个数字检查是否检查是否检查这是PRIME)。为此,您只需要在结果中回馈最终
n
。让我们使用一个where
块来处理模式匹配,还可以使rem
and:然后:与您的python代码相等的是,
这实际上为您提供了,例如在Python中,列表还包括非各个人(具有功率0)。为了避免这种情况,将列表构建更改为
You can always replace imperative-language loops (as long as they don't meddle with any global state) with recursion. That may not be the most elegant approach, but in this case it seems perfectly appropriate to imitate your inner Python loop with a recursive function:
(This counts “backwards” compared to the Python loop. You could also make it tail-recursive with a helper function and count forwards over an accumulator variable, but that's more awkward and I don't think there's a memory/performance benefit that would justify it in this case.)
You can either use that together with your Haskell code (for each of the factors you've already found, check how often it occurs), or extend it so the whole thing works like the Python solution (which is actually a lot more efficient, because it avoids for every number checking whether it's prime). For that you just need to give back the final
n
in the result. Let's use awhere
block for handling the pattern matching, and also make therem
and:Then the equivalent to your Python code is
This actually gives you, like in Python, the list including also non-dividers (with power 0). To avoid that, change the list-building to