diff --git a/hs-euler/ChangeLog.md b/hs-euler/ChangeLog.md index ef481df..9251a9b 100644 --- a/hs-euler/ChangeLog.md +++ b/hs-euler/ChangeLog.md @@ -1,3 +1,3 @@ -# Changelog for euler072 +# Changelog for euler ## Unreleased changes diff --git a/hs-euler/README.md b/hs-euler/README.md index 2a5eb1e..8abf6cb 100644 --- a/hs-euler/README.md +++ b/hs-euler/README.md @@ -1 +1 @@ -# euler072 +# euler diff --git a/hs-euler/app/Main.hs b/hs-euler/app/Main.hs index 76c81cf..0ba5bd8 100644 --- a/hs-euler/app/Main.hs +++ b/hs-euler/app/Main.hs @@ -5,57 +5,34 @@ --package arithmoi -} -{- -Consider the fraction, n/d, where n and d are positive integers. If n [Integer] -reducedProper d = do - let pf = map fst $ factorise d - x <- [1..d] - guard (not (any (\p -> x `rem` p == 0) pf)) - return x +import qualified Euler072 --- Naive solution, took ages -nbFractions :: Integer -> Int -nbFractions d = foldl' (+) 0 (map (traceShowId . length . reducedProper . traceShowId) [2..d]) +solution :: Int -> Maybe (IO ()) +solution 72 = Just $ do putStr ("072: " :: Text); print Euler072.solution +solution _ = Nothing --- Write a list of all factors -factors = concatMap (\(x,n) -> replicate n x) . factorise +solutions = [72] --- phi, Euler's totient function --- https://en.wikipedia.org/wiki/Euler%27s_totient_function -phi n - | isPrime n = n-1 - | otherwise = prodPhi (factors n) - where - prodPhi [a] = a-1 - prodPhi (a:b:t) - | a == b = a * prodPhi (b:t) - | otherwise = (a - 1) * prodPhi (b:t) - --- less than 5s -solution n = sum $ map phi [2..n] +readNumber :: [[Char]] -> Maybe Int +readNumber [] = Nothing +readNumber (s:args) = reads s & head & fmap fst main :: IO () main = do - let n = 1000000 - -- print $ nbFractions n -- 303963552391 - print $ solution n + nbParsed <- fmap readNumber getArgs + case nbParsed of + Just nb -> fromMaybe + (putErrText "I don't know the solution of this problem yet.") + (solution nb) + Nothing -> solutions + & map solution + & map (fromMaybe (pure ())) + & sequence_ diff --git a/hs-euler/euler.cabal b/hs-euler/euler.cabal index 18bdab6..5e886c4 100644 --- a/hs-euler/euler.cabal +++ b/hs-euler/euler.cabal @@ -2,13 +2,13 @@ -- -- see: https://github.com/sol/hpack -- --- hash: 859091034e520db697fc23b2aeb6b27cc63dfe46541d371c8bd861b7ff922014 +-- hash: fa22c4d5b58b46bb0a1b31feef800edb37c1724fa165ed9b529637ec8b56ddc5 -name: euler072 +name: euler version: 0.1.0.0 -description: Please see the README on GitHub at -homepage: https://github.com/githubuser/euler072#readme -bug-reports: https://github.com/githubuser/euler072/issues +description: Please see the README on GitHub at +homepage: https://github.com/githubuser/euler#readme +bug-reports: https://github.com/githubuser/euler/issues author: Author name here maintainer: example@example.com copyright: 2019 Author name here @@ -22,13 +22,14 @@ extra-source-files: source-repository head type: git - location: https://github.com/githubuser/euler072 + location: https://github.com/githubuser/euler library exposed-modules: + Euler072 Lib other-modules: - Paths_euler072 + Paths_euler hs-source-dirs: src build-depends: @@ -37,31 +38,31 @@ library , protolude default-language: Haskell2010 -executable euler072-exe +executable euler main-is: Main.hs other-modules: - Paths_euler072 + Paths_euler hs-source-dirs: app ghc-options: -O -threaded -rtsopts -with-rtsopts=-N build-depends: arithmoi , base >=4.7 && <5 - , euler072 + , euler , protolude default-language: Haskell2010 -test-suite euler072-test +test-suite euler-test type: exitcode-stdio-1.0 main-is: Spec.hs other-modules: - Paths_euler072 + Paths_euler hs-source-dirs: test ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: arithmoi , base >=4.7 && <5 - , euler072 + , euler , protolude default-language: Haskell2010 diff --git a/hs-euler/package.yaml b/hs-euler/package.yaml index 751e89d..a07a902 100644 --- a/hs-euler/package.yaml +++ b/hs-euler/package.yaml @@ -1,6 +1,6 @@ -name: euler072 +name: euler version: 0.1.0.0 -github: "githubuser/euler072" +github: "githubuser/euler" license: BSD3 author: "Author name here" maintainer: "example@example.com" @@ -17,7 +17,7 @@ extra-source-files: # To avoid duplicated efforts in documentation and dealing with the # complications of embedding Haddock markup inside cabal files, it is # common to point users to the README.md file. -description: Please see the README on GitHub at +description: Please see the README on GitHub at dependencies: - base >= 4.7 && < 5 @@ -28,7 +28,7 @@ library: source-dirs: src executables: - euler072-exe: + euler: main: Main.hs source-dirs: app ghc-options: @@ -37,10 +37,10 @@ executables: - -rtsopts - -with-rtsopts=-N dependencies: - - euler072 + - euler tests: - euler072-test: + euler-test: main: Spec.hs source-dirs: test ghc-options: @@ -48,4 +48,4 @@ tests: - -rtsopts - -with-rtsopts=-N dependencies: - - euler072 + - euler diff --git a/hs-euler/src/Euler072.hs b/hs-euler/src/Euler072.hs index 76c81cf..f2e2e69 100644 --- a/hs-euler/src/Euler072.hs +++ b/hs-euler/src/Euler072.hs @@ -20,7 +20,7 @@ How many elements would be contained in the set of reduced proper fractions for d ≤ 1,000,000? -} {-# LANGUAGE NoImplicitPrelude #-} -module Main where +module Euler072 where import Protolude @@ -37,6 +37,8 @@ reducedProper d = do nbFractions :: Integer -> Int nbFractions d = foldl' (+) 0 (map (traceShowId . length . reducedProper . traceShowId) [2..d]) +solutionSlow = nbFractions 1000000 + -- Write a list of all factors factors = concatMap (\(x,n) -> replicate n x) . factorise @@ -52,10 +54,4 @@ phi n | otherwise = (a - 1) * prodPhi (b:t) -- less than 5s -solution n = sum $ map phi [2..n] - -main :: IO () -main = do - let n = 1000000 - -- print $ nbFractions n -- 303963552391 - print $ solution n +solution = let n = 1000000 in sum $ map phi [2..n]