fork download
  1. module Main
  2. where
  3.  
  4. import Data.List
  5.  
  6. divides :: Integer -> Integer -> Bool
  7. divides d n = rem n d == 0
  8.  
  9. ld :: Integer -> Integer
  10. ld x = ldf 2 x
  11.  
  12. ldf :: Integer -> Integer -> Integer
  13. ldf m n | divides m n = m
  14. | m^2 > n = n
  15. | otherwise = ldf (m + 1) n
  16.  
  17. prime0 :: Integer -> Bool
  18. prime0 x | x < 1 = error "Not a positive integer!"
  19. | x == 1 = False
  20. | otherwise = ld x == x
  21.  
  22. factors :: Integer -> [Integer]
  23. factors n | n < 1 = error "Not a positive integer!"
  24. | n == 1 = []
  25. | otherwise = p : factors (div n p) where p = ld n
  26.  
  27. main :: IO()
  28. main = do
  29. putStrLn "Hello! This is a short program that aims at prime generation!"
  30. putStrLn "Test it yourself, here are some samples:"
  31. print $ factors 10
  32. print $ factors 15
  33. print $ "What about numbers and number of their divisors?"
  34. print $ [(x, factors x, length $ factors x) | x <- [2..100]]
  35. print "That above is messy, let's filter primes!"
  36. print $ filter prime0 [2..100]
  37. print "All fine and dandy, but what about really large numbers?"
  38. print $ factors 10001011107101
  39. print $ factors 11111111111111111
  40. print $ factors (2^20 - 1)
  41. print $ factors (2^31 - 1) -- Marsene Prime! This is a comment by the way.
  42. print "Yeah, that easy and fast!"
  43. print "This language and weird, quirky and I can totally get why people think"
  44. print "that it was made by sadists to masochists."
  45. print "But damn me if I am going to deny that it can approach C in terms of"
  46. print "efficiency. In right hand of course."
  47. print "As a bonus, here is a solution to first problem on Project Euler"
  48. print $ sum $ union [3,6..999] [5,10..999]
  49.  
Success #stdin #stdout 0.56s 4680KB
stdin
Standard input is empty
stdout
Hello! This is a short program that aims at prime generation!
Test it yourself, here are some samples:
[2,5]
[3,5]
"What about numbers and number of their divisors?"
[(2,[2],1),(3,[3],1),(4,[2,2],2),(5,[5],1),(6,[2,3],2),(7,[7],1),(8,[2,2,2],3),(9,[3,3],2),(10,[2,5],2),(11,[11],1),(12,[2,2,3],3),(13,[13],1),(14,[2,7],2),(15,[3,5],2),(16,[2,2,2,2],4),(17,[17],1),(18,[2,3,3],3),(19,[19],1),(20,[2,2,5],3),(21,[3,7],2),(22,[2,11],2),(23,[23],1),(24,[2,2,2,3],4),(25,[5,5],2),(26,[2,13],2),(27,[3,3,3],3),(28,[2,2,7],3),(29,[29],1),(30,[2,3,5],3),(31,[31],1),(32,[2,2,2,2,2],5),(33,[3,11],2),(34,[2,17],2),(35,[5,7],2),(36,[2,2,3,3],4),(37,[37],1),(38,[2,19],2),(39,[3,13],2),(40,[2,2,2,5],4),(41,[41],1),(42,[2,3,7],3),(43,[43],1),(44,[2,2,11],3),(45,[3,3,5],3),(46,[2,23],2),(47,[47],1),(48,[2,2,2,2,3],5),(49,[7,7],2),(50,[2,5,5],3),(51,[3,17],2),(52,[2,2,13],3),(53,[53],1),(54,[2,3,3,3],4),(55,[5,11],2),(56,[2,2,2,7],4),(57,[3,19],2),(58,[2,29],2),(59,[59],1),(60,[2,2,3,5],4),(61,[61],1),(62,[2,31],2),(63,[3,3,7],3),(64,[2,2,2,2,2,2],6),(65,[5,13],2),(66,[2,3,11],3),(67,[67],1),(68,[2,2,17],3),(69,[3,23],2),(70,[2,5,7],3),(71,[71],1),(72,[2,2,2,3,3],5),(73,[73],1),(74,[2,37],2),(75,[3,5,5],3),(76,[2,2,19],3),(77,[7,11],2),(78,[2,3,13],3),(79,[79],1),(80,[2,2,2,2,5],5),(81,[3,3,3,3],4),(82,[2,41],2),(83,[83],1),(84,[2,2,3,7],4),(85,[5,17],2),(86,[2,43],2),(87,[3,29],2),(88,[2,2,2,11],4),(89,[89],1),(90,[2,3,3,5],4),(91,[7,13],2),(92,[2,2,23],3),(93,[3,31],2),(94,[2,47],2),(95,[5,19],2),(96,[2,2,2,2,2,3],6),(97,[97],1),(98,[2,7,7],3),(99,[3,3,11],3),(100,[2,2,5,5],4)]
"That above is messy, let's filter primes!"
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
"All fine and dandy, but what about really large numbers?"
[7,29,49266064567]
[2071723,5363222357]
[3,5,5,11,31,41]
[2147483647]
"Yeah, that easy and fast!"
"This language and weird, quirky and I can totally get why people think"
"that it was made by sadists to masochists."
"But damn me if I am going to deny that it can approach C in terms of"
"efficiency. In right hand of course."
"As a bonus, here is a solution to first problem on Project Euler"
233168