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
|
||||
|
|
|
@ -1 +1 @@
|
|||
# euler072
|
||||
# euler
|
||||
|
|
|
@ -5,57 +5,34 @@
|
|||
--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 OverloadedStrings #-}
|
||||
module Main where
|
||||
|
||||
import Protolude
|
||||
|
||||
import Math.NumberTheory.Primes
|
||||
import System.Environment
|
||||
|
||||
reducedProper :: Integer -> [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_
|
||||
|
|
|
@ -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 <https://github.com/githubuser/euler072#readme>
|
||||
homepage: https://github.com/githubuser/euler072#readme
|
||||
bug-reports: https://github.com/githubuser/euler072/issues
|
||||
description: Please see the README on GitHub at <https://github.com/githubuser/euler#readme>
|
||||
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
|
||||
|
|
|
@ -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 <https://github.com/githubuser/euler072#readme>
|
||||
description: Please see the README on GitHub at <https://github.com/githubuser/euler#readme>
|
||||
|
||||
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
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue