Advertisement
banovski

Project Euler, Problem #3, Haskell

Dec 3rd, 2021 (edited)
1,281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- The prime factors of 13195 are 5, 7, 13 and 29. What is the largest
  2. -- prime factor of the number 600851475143?
  3.  
  4. divisorGenerator :: Integral a => a -> a -> a
  5. divisorGenerator a b = divisibilityCheck a (div a (div a b + 1))
  6.  
  7. divisibilityCheck :: Integral a => a -> a -> a
  8. divisibilityCheck a b = if mod a b == 0 then primeCheck a b else limitCheck a b
  9.  
  10. limitCheck :: Integral a => a -> a -> a
  11. limitCheck a b = if b > 1 then divisorGenerator a b else b
  12.  
  13. primeCheck :: Integral a => a -> a -> a
  14. primeCheck a b = if null [c | c <- [2 .. div b 2], mod b c == 0] then b else limitCheck a b
  15.  
  16. initializer :: Integral a => a -> a
  17. initializer a = limitCheck a (div a 2)
  18.  
  19. main :: IO ()
  20. main = print $ initializer 600851475143
  21.  
  22. -- 6857
  23.  
  24. -- real 0m1,211s
  25. -- user 0m1,202s
  26. -- sys  0m0,008s
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement