Skip to content

Commit 068758f

Browse files
committed
EulerLib, P10, P77, P128, P130, P132: Refactored copies of Haskell isPrime function into library module.
P10: Tweaked Haskell solution to fix error-causing ambiguities.
1 parent f13630a commit 068758f

File tree

6 files changed

+14
-22
lines changed

6 files changed

+14
-22
lines changed

haskell/EulerLib.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-}
88

99
module EulerLib
10-
(digitSum, sqrt)
10+
(digitSum, sqrt, isPrime)
1111
where
1212

1313
import Prelude hiding (sqrt)
@@ -28,3 +28,7 @@ sqrt n = sqrtAlpha 1 where
2828
| otherwise = sqrtAlpha (Data.Bits.shiftL i 1)
2929
sqrtBeta 0 acc = acc
3030
sqrtBeta i acc = sqrtBeta (div i 2) (if (i + acc)^2 <= n then i + acc else acc)
31+
32+
33+
isPrime :: (Integral a, Data.Bits.Bits a) => a -> Bool
34+
isPrime n = n > 1 && null [() | k <- [2 .. (sqrt n)], mod n k == 0]

haskell/p010.hs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import EulerLib
1010

1111

12+
limit = 2000000 :: Int
1213
main = putStrLn (show ans)
13-
ans = sum (filter isPrime [2 .. 2000000-1])
14-
15-
isPrime :: Int -> Bool
16-
isPrime n = n > 1 && null [() | k <- [2 .. (EulerLib.sqrt n)], mod n k == 0]
14+
ans = sum (filter EulerLib.isPrime [2 .. (limit - 1)])

haskell/p077.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
target = 5000
1113
main = putStrLn (show ans)
@@ -25,8 +27,5 @@ ans = head $ dropWhile (\n -> partitions n n <= target) [0..]
2527
partitions :: Int -> Int -> Int
2628
partitions _ 0 = 1
2729
partitions 0 _ = 0
28-
partitions i n = (if ((isPrime i) && i <= n) then (partitionsMemo !! i !! (n - i)) else 0) + (partitionsMemo !! (i - 1) !! n)
30+
partitions i n = (if ((EulerLib.isPrime i) && i <= n) then (partitionsMemo !! i !! (n - i)) else 0) + (partitionsMemo !! (i - 1) !! n)
2931
partitionsMemo = [[partitions i n | n <- [0..]] | i <- [0..]]
30-
31-
isPrime :: Int -> Bool
32-
isPrime n = n > 1 && null [() | k <- [2 .. n-1], mod n k == 0]

haskell/p128.hs

+2-5
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,12 @@ ans = find 2 (target - 2) -- Already found 2 because n = 1 and 2 satisfy PD(n)
175175

176176
find :: Integer -> Integer -> Integer
177177
find ring remain = let
178-
a = all isPrime [ring * 6 - 1, ring * 6 + 1, ring * 12 + 5]
179-
b = all isPrime [ring * 6 - 1, ring * 6 + 5, ring * 12 - 7]
178+
a = all EulerLib.isPrime [ring * 6 - 1, ring * 6 + 1, ring * 12 + 5]
179+
b = all EulerLib.isPrime [ring * 6 - 1, ring * 6 + 5, ring * 12 - 7]
180180
remain' = remain - (if a then 1 else 0)
181181
remain'' = remain' - (if b then 1 else 0)
182182
in if remain' == 0
183183
then (ring * (ring - 1) * 3 + 2)
184184
else if remain'' == 0
185185
then (ring * (ring + 1) * 3 + 1)
186186
else find (ring + 1) remain''
187-
188-
isPrime :: Integer -> Bool
189-
isPrime n = n > 1 && null [() | k <- [2 .. (EulerLib.sqrt n)], mod n k == 0]

haskell/p130.hs

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import EulerLib
1010

1111

1212
main = putStrLn (show ans)
13-
cond i = (mod i 5 /= 0) && (not (isPrime i)) && mod (i - 1) (findLeastDivisibleRepunit i) == 0
13+
cond i = (mod i 5 /= 0) && (not (EulerLib.isPrime i)) && mod (i - 1) (findLeastDivisibleRepunit i) == 0
1414
ans = sum (take 25 (filter cond [7,9..]))
1515

1616
findLeastDivisibleRepunit :: Integer -> Integer
@@ -20,6 +20,3 @@ findLeastDivisibleRepunit n = if (mod n 2) == 0 || (mod n 5) == 0
2020
func 0 _ k = k
2121
func s p k = func (mod (s + p * 10) n) (mod (p * 10) n) (k + 1)
2222
in func 1 1 1
23-
24-
isPrime :: Integer -> Bool
25-
isPrime n = n > 1 && null [() | k <- [2 .. (EulerLib.sqrt n)], mod n k == 0]

haskell/p132.hs

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import EulerLib
1010

1111

1212
main = putStrLn (show ans)
13-
cond i = isPrime i && (repunitMod (10^9) i) == 0
13+
cond i = EulerLib.isPrime i && (repunitMod (10^9) i) == 0
1414
ans = sum (take 40 (filter cond [2..]))
1515

1616
repunitMod :: Integer -> Integer -> Integer
@@ -20,6 +20,3 @@ powMod :: Integer -> Integer -> Integer -> Integer
2020
powMod _ 0 _ = 1
2121
powMod x 1 m = mod x m
2222
powMod x y m = mod ((powMod x (div y 2) m)^2 * (if ((mod y 2) == 0) then 1 else x)) m
23-
24-
isPrime :: Integer -> Bool
25-
isPrime n = n > 1 && null [() | k <- [2 .. (EulerLib.sqrt n)], mod n k == 0]

0 commit comments

Comments
 (0)