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