Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. module Q35 where
  2.  
  3. primeFactors :: Int -> [Int]
  4. primeFactors x = case (tryPrimeFactors x) of Left result -> result
  5. Right message -> error message
  6.  
  7. tryPrimeFactors :: Int -> Either [Int] String
  8. tryPrimeFactors x =
  9. let
  10. check :: Int -> Either [Int] String
  11. check x
  12. | x <= 0 = Right "Nonpositive integer is not supported"
  13. | otherwise = Left (tryPrimeFactors' x [2..(1 + floor (sqrt (fromIntegral x)))])
  14.  
  15. tryPrimeFactors' :: Int -> [Int] -> [Int]
  16. tryPrimeFactors' 1 _ = []
  17. tryPrimeFactors' p [] = [p]
  18. tryPrimeFactors' p (q:r) = if (mod p q) == 0 then q:(tryPrimeFactors' (quot p q) (q:r)) else (tryPrimeFactors' p r)
  19. in
  20. check x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement