Advertisement
Guest User

gaff

a guest
Mar 31st, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE ScopedTypeVariables #-}
  2.  
  3. -- | this code solves (fast) the primacity problem in facebook hackathon 2015.
  4. --
  5. --   basically, given 3 positive integers -- A, B, K -- output number of items
  6. --   in [A, B] with primacity K.  For example, if A=5, B=15, K=2, the program
  7. --   will output 5 -- i.e., 5 numbers in [5, 15] have primacity 2.
  8. --
  9. --   NOTE: we must always have 2 <= A <= B, K >= 1.
  10. --
  11. --   for details, see ~/software-development/notes/facebook-hackathon-2015.pages
  12. --
  13. --   author & source: /u/ Siddhanathan @ https://goo.gl/Yv4XMh (stackoverflow).
  14.  
  15. -- NOTE:
  16. --  with base-4.8, Prelude re-exports a few more entities from modules such as
  17. --  Data.Monoid and Control.Applicative, which makes below 2 imports in the
  18. --  original code redundant.
  19. --  REF: see https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/7.10
  20. --  import Control.Applicative ( pure, (<$>) )
  21. --  import Data.Monoid         ( (<>) )
  22.  
  23. module Primacity where
  24.  
  25. import Data.List           ( nub )
  26.  
  27. isPrime :: forall i. Integral i => i -> Bool
  28. isPrime n = isPrime_ n primes
  29.   where isPrime_ :: i -> [i] -> Bool
  30.         isPrime_ m [] = isPrime_ m primes   -- this case should never happen
  31.         isPrime_ m (p:ps)
  32.             | p * p > m      = True
  33.             | m `mod` p == 0 = False
  34.             | otherwise      = isPrime_ m ps
  35.  
  36. primes :: (Integral i) => [i]
  37. primes = 2 : filter isPrime [3,5..]
  38.  
  39. primeFactors :: forall i. Integral i => i -> [i]
  40. primeFactors n = factors n primes
  41.   where factors :: i -> [i] -> [i]
  42.         factors m [] = factors m primes     -- this case should never happen
  43.         factors m (x:xs)
  44.             | x * x > m      = [m]
  45.             | m `mod` x == 0 = x : factors (m `div` x) (x:xs)
  46.             | otherwise      = factors m xs
  47.  
  48. primacity :: (Integral i) => i -> Int
  49. primacity = length . nub . primeFactors
  50.  
  51. primacityCount :: (Integral i) => [i] -> Int -> Int
  52. primacityCount x k = length . filter (== k) . fmap primacity $ x
  53.  
  54. df :: IO ()
  55. df = do
  56.   let a :: Int = 2
  57.       b :: Int = 10000000
  58.       k :: Int = 1
  59.       n = primacityCount [a..b] k
  60.   putStrLn $ show n
  61.  
  62.  
  63. {-# LANGUAGE ScopedTypeVariables #-}
  64.  
  65. module Main (main) where
  66.  
  67. import Primacity
  68.  
  69. dff :: IO ()
  70. dff = do
  71.   let a :: Int = 2
  72.       b :: Int = 10000000
  73.       k :: Int = 1
  74.       n = primacityCount [a..b] k
  75.   putStrLn $ show n
  76.  
  77. -- using GHC 8.10.4
  78. -- ghc options used:
  79.   --  '-O2 -fforce-recomp -Wall -Werror'
  80.   --  '-fforce-recomp -Wall -Werror'
  81. main :: IO ()
  82. main = do
  83.   df    -- this call works just fine.
  84.   -- but below call to `dff`, which is identical to `df` in `Primacity.hs`,
  85.   -- hangs.  however, if input `b` in `dff` is small, say 100, it works.
  86.   dff
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement