List haskell中的素因子Zacion
我在做一个程序,对于给定的整数n,返回一对整数的列表,其中第一个元素是n的素数分解的素数,第二个元素是该素数的对应指数。例如,对于n=50,它将输出[(2,1),(5,2)],因为50=(2^1)*(5^2) 总之,这是我的代码:List haskell中的素因子Zacion,list,haskell,list-comprehension,primes,List,Haskell,List Comprehension,Primes,我在做一个程序,对于给定的整数n,返回一对整数的列表,其中第一个元素是n的素数分解的素数,第二个元素是该素数的对应指数。例如,对于n=50,它将输出[(2,1),(5,2)],因为50=(2^1)*(5^2) 总之,这是我的代码: --returns all numbers that divide x divis :: Integer -> [Integer] divis 1 = [] divis x = [n | n<-[2..(x-1)], mod x n == 0] --ch
--returns all numbers that divide x
divis :: Integer -> [Integer]
divis 1 = []
divis x = [n | n<-[2..(x-1)], mod x n == 0]
--checks if a number is prime
isprime :: Integer -> Bool
isprime 1 = False
isprime n = if divis n == [] then True else False
--list of prime numbers that divide x
facto :: Integer -> [Integer]
facto 1 = []
facto x = [n | n <- (divis x), isprime n == True]
--finds the biggest exponent of a number m that divides another number n
potencia :: Integer -> Integer -> Integer
potencia _ 0 = error "error"
potencia _ 1 = error "error"
potencia n m = (head [x | x <- [0..], not(mod n (m^x) == 0)]) - 1
--返回所有除以x的数字
除数::整数->[整数]
除数1=[]
除数x=[n | n布尔
iPrime 1=错误
isprime n=如果除数n=[],则为True,否则为False
--除以x的素数列表
事实::整数->[整数]
事实1=[]
事实x=[n | n整数->整数
效价u0=错误“错误”
效价u1=错误“错误”
效价n m=(头[x | x[(整数,整数)]
factorzar 0=错误“否”
factorzar 1=[(1,1)]--这不准确,但我稍后会更改它
factorzar n=[(x,y)| x简单的答案是
y == potencia n x
你真的应该读书吗
let y = potencia n x
你不需要检查mod n(x^y)=0
——我认为根据potentia
的定义,这是正确的
还有其他一些事情你可以做得不同,但它们是整理。风格说明:如果除数n=[]不要写,如果为真,那么为假
,只写divis n=[]
甚至null(divis n)
。同样,不要写isprime n==True
,只写isprime n
。
let y = potencia n x