Prime factors in Haskell
问题 I'm new to Haskell. How to generate a list of lists which contains prime factors of next integers? Currently, I only know how to generate prime numbers: primes = map head $ iterate (\(x:xs) -> [y | y<-xs, y `mod` x /= 0 ]) [2..] 回答1: A simple approach to determine the prime factors of n is to search for the first divisor d in [2..n-1] if D exists: return d : primeFactors(div n d) otherwise return n (since n is prime) Code: prime_factors :: Int -> [Int] prime_factors 1 = [] prime_factors n |