packages feed

antiprimes (empty) → 0.1.0.0

raw patch · 5 files changed

+195/−0 lines, 5 filesdep +antiprimesdep +basedep +hspecsetup-changed

Dependencies added: antiprimes, base, hspec, primes

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Wolfgang Kittenberger (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Wolfgang Kittenberger nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ antiprimes.cabal view
@@ -0,0 +1,35 @@+name:                antiprimes+version:             0.1.0.0+synopsis:            Initial project template from stack+description:         Please see README.md+homepage:            https://github.com/wokibe/antiprimes#readme+license:             BSD3+license-file:        LICENSE+author:              Wolfgang Kittenberger+maintainer:          wolfkibe@gmail.com+copyright:           Copyright: (c) wolfkibe+category:            Learning Haskell+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     AntiPrimes+  build-depends:       base >= 4.7 && < 5+                     , primes+  default-language:    Haskell2010++test-suite antiprimes-test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Spec.hs+  build-depends:       base+                     , antiprimes+                     , hspec+  ghc-options:         -threaded -rtsopts -with-rtsopts=-N+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/wokibe/antiprimes
+ src/AntiPrimes.hs view
@@ -0,0 +1,127 @@+-- | a.k.a. highliy composite numbers+module AntiPrimes+       (Nui, Siz, Lst, size, dividers, proof, list) where++import Data.List (foldr1, group, nub, sort, tails)+import Data.Numbers.Primes (primeFactors)++-- | Number under investigation+type Nui = Int+-- | number of divisors of Nui+type Siz = Int+-- | list of tuples: numbers and their divisor sizes+type Lst = [(Nui, Siz)]++-- calculate the frequencies of the list elements+frequ :: [Int] -> [(Int, Int)]+frequ list = map (\l -> (head l, length l)) (group (sort list))++-- good old combinatorics: combine n elements of a list without repetitions+comb :: (Eq a, Num a) => a -> [a1] -> [[a1]]+comb 0 lst = [[]]+comb n lst = do+  (x:xs) <- tails lst+  rest <- comb (n-1) xs+  return $ x:rest++-- all possible combinations of a list+combAll :: (Eq a, Num a) => a -> [a1] -> [[a1]]+combAll 0 lst = []+combAll n lst = (comb n lst) ++ (combAll (n-1) lst)++-- error message+msg :: String+msg = "the number must be greater than 0"++lst1 :: Lst+lst1 = [(1,1)]++-- multiplication all elements of a list+mul :: (Num a, Foldable t) => t a -> a+mul x = foldr1 (*) x++-- generate a tuple with a number and ist size+tup :: Nui -> (Nui, Siz)+tup n = (n, size n)++-- list of (n, size) for n = [2..n]+lst :: Nui -> Lst+lst n+  | n <= 0  = error msg+  | n == 1  = lst1+  | otherwise = map tup rng+                where rng = [2..n]  -- range of interest++-- | calculate the "size" of all possible factors of a number+--+-- >>> import qualified AntiPrimes as AP+-- >>> AP.size 12+-- 6                -- as [1,2,3,4,6,12] are possible+--+size :: Nui -> Siz+size n+  | n <= 0    = error msg+  | n == 1    = 1+  | otherwise = foldr1 (*) incrs+                where facts = primeFactors n+                      frequs = frequ facts+                      counts = map snd frequs+                      incrs  = map (1 +) counts++-- | calculate all possible dividers of a number+--+-- >>> import qualified AntiPrimes as AP+-- >>> AP.dividers 12+-- [1,2,3,4,6,12]+--+dividers :: Nui -> [Int]+dividers n+  | n <= 0    = error msg+  | n == 1    = [1]+  | otherwise = sort add1+                where facts = primeFactors n+                      len   = length facts+                      combs = combAll len facts+                      multi = map mul combs+                      clean = nub multi+                      add1  = clean ++ [1]++-- | proof if a number is an antiprime+--+-- >>> import qualified AntiPrimes as AP+-- >>> AP.proof 4+-- True+-- >>> AP.proof 5+-- False+--+proof :: Nui -> Bool+proof n+  | n <= 0    = error msg+  | n == 1    = True+  | n == 2    = True+  | otherwise = length flt == 1+                where siz = size n+                      flt = filter (\(a, b) -> b >= siz) $ lst n++-- | show all (antiprimes, size) below a number+--+-- >>> import qualified AntiPrimes as AP+-- >>> AP.list 12+-- [(1,1),(2,2),(4,3),(6,4)]+--+list :: Int -> Lst+list 0 = error msg+list 1 = lst1+list n+  | n <= 0    = error msg+  | n == 1    = lst1+  | otherwise = list' lst1 (lst n)++-- helper function to clean out excluded entries+list' :: Lst -> Lst -> Lst+list' aps [] = aps+list' aps rest = list' nxt est+  where top = head rest+        rst = tail rest+        nxt = aps ++ [top]+        est = filter (\(a, b) -> b > (snd top)) $ rst
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}