Skip to content

Commit 5564942

Browse files
committed
EulerLib, P16, P20, P56, P65, P164: Refactored copies of Haskell digitSum function into new library module.
1 parent fdb2b53 commit 5564942

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

haskell/EulerLib.hs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-
2+
- Shared code for solutions to Project Euler problems
3+
- Copyright (c) Project Nayuki. All rights reserved.
4+
-
5+
- https://www.nayuki.io/page/project-euler-solutions
6+
- https://github.com/nayuki/Project-Euler-solutions
7+
-}
8+
9+
module EulerLib
10+
(digitSum)
11+
where
12+
13+
14+
digitSum :: Integral a => a -> a
15+
digitSum 0 = 0
16+
digitSum n = (mod n 10) + (digitSum (div n 10))

haskell/p016.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
- https://github.com/nayuki/Project-Euler-solutions
77
-}
88

9+
import EulerLib
10+
911

1012
-- We implement this solution in a straightforward way thanks to Haskell's built-in arbitrary precision Integer type.
1113
main = putStrLn (show ans)
12-
ans = digitSum (2^1000 :: Integer)
13-
14-
digitSum 0 = 0
15-
digitSum n = (mod n 10) + (digitSum (div n 10))
14+
ans = EulerLib.digitSum (2^1000 :: Integer)

haskell/p020.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
- https://github.com/nayuki/Project-Euler-solutions
77
-}
88

9+
import EulerLib
10+
911

1012
{-
1113
- We do a straightforward product thanks to Haskell's built-in arbitrary precision Integer type.
1214
-}
1315

1416
main = putStrLn (show ans)
15-
ans = digitSum (factorial 100 :: Integer)
16-
17-
digitSum 0 = 0
18-
digitSum n = (mod n 10) + (digitSum (div n 10))
17+
ans = EulerLib.digitSum (factorial 100 :: Integer)
1918

2019
factorial n = product [1..n]

haskell/p056.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
- https://github.com/nayuki/Project-Euler-solutions
77
-}
88

9+
import EulerLib
910

10-
main = putStrLn (show ans)
11-
ans = foldl1 max [digitsum (a^b) | a <- [0..99], b <- [0..99]]
1211

13-
digitsum 0 = 0
14-
digitsum n = (mod n 10) + (digitsum (div n 10))
12+
main = putStrLn (show ans)
13+
ans = foldl1 max [EulerLib.digitSum (a^b) | a <- [0..99], b <- [0..99]]

haskell/p065.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- https://github.com/nayuki/Project-Euler-solutions
77
-}
88

9+
import EulerLib
10+
911

1012
limit = 100
1113
main = putStrLn (show ans)
@@ -15,12 +17,9 @@ ans = let
1517
| otherwise = let (n, d) = func (i+1) end
1618
in ((eContFracTerm i) * n + d, n)
1719
(numer, _) = func 0 limit
18-
in digitSum numer
20+
in EulerLib.digitSum numer
1921

2022
eContFracTerm 0 = 2
2123
eContFracTerm i
2224
| mod i 3 == 2 = (div i 3) * 2 + 2
2325
| otherwise = 1
24-
25-
digitSum 0 = 0
26-
digitSum n = (mod n 10) + (digitSum (div n 10))

haskell/p164.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- https://github.com/nayuki/Project-Euler-solutions
77
-}
88

9+
import EulerLib
10+
911

1012
base = 10
1113
digits = 20
@@ -41,9 +43,6 @@ ans = (ways (digits + consecutive) 0) - (ways (digits + consecutive - 1) 0)
4143
ways :: Int -> Int -> Integer
4244
ways 0 0 = 1
4345
ways 0 _ = 0
44-
ways d p = if (digitSum p) > maxSum then 0
46+
ways d p = if (EulerLib.digitSum p) > maxSum then 0
4547
else sum [waysMemo !! (d - 1) !! ((mod p (base^(consecutive - 1))) * base + i) | i <- [0..9]]
4648
waysMemo = [[ways d p | p <- [0..]] | d <- [0..]]
47-
48-
digitSum 0 = 0
49-
digitSum n = (mod n 10) + (digitSum (div n 10))

0 commit comments

Comments
 (0)