Commit 068758f 1 parent f13630a commit 068758f Copy full SHA for 068758f
File tree 6 files changed +14
-22
lines changed
6 files changed +14
-22
lines changed Original file line number Diff line number Diff line change 7
7
-}
8
8
9
9
module EulerLib
10
- (digitSum , sqrt )
10
+ (digitSum , sqrt , isPrime )
11
11
where
12
12
13
13
import Prelude hiding (sqrt )
@@ -28,3 +28,7 @@ sqrt n = sqrtAlpha 1 where
28
28
| otherwise = sqrtAlpha (Data.Bits. shiftL i 1 )
29
29
sqrtBeta 0 acc = acc
30
30
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 ]
Original file line number Diff line number Diff line change 9
9
import EulerLib
10
10
11
11
12
+ limit = 2000000 :: Int
12
13
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 )])
Original file line number Diff line number Diff line change 6
6
- https://github.com/nayuki/Project-Euler-solutions
7
7
-}
8
8
9
+ import EulerLib
10
+
9
11
10
12
target = 5000
11
13
main = putStrLn (show ans)
@@ -25,8 +27,5 @@ ans = head $ dropWhile (\n -> partitions n n <= target) [0..]
25
27
partitions :: Int -> Int -> Int
26
28
partitions _ 0 = 1
27
29
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)
29
31
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 ]
Original file line number Diff line number Diff line change @@ -175,15 +175,12 @@ ans = find 2 (target - 2) -- Already found 2 because n = 1 and 2 satisfy PD(n)
175
175
176
176
find :: Integer -> Integer -> Integer
177
177
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 ]
180
180
remain' = remain - (if a then 1 else 0 )
181
181
remain'' = remain' - (if b then 1 else 0 )
182
182
in if remain' == 0
183
183
then (ring * (ring - 1 ) * 3 + 2 )
184
184
else if remain'' == 0
185
185
then (ring * (ring + 1 ) * 3 + 1 )
186
186
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 ]
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import EulerLib
10
10
11
11
12
12
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
14
14
ans = sum (take 25 (filter cond [7 ,9 .. ]))
15
15
16
16
findLeastDivisibleRepunit :: Integer -> Integer
@@ -20,6 +20,3 @@ findLeastDivisibleRepunit n = if (mod n 2) == 0 || (mod n 5) == 0
20
20
func 0 _ k = k
21
21
func s p k = func (mod (s + p * 10 ) n) (mod (p * 10 ) n) (k + 1 )
22
22
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 ]
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import EulerLib
10
10
11
11
12
12
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
14
14
ans = sum (take 40 (filter cond [2 .. ]))
15
15
16
16
repunitMod :: Integer -> Integer -> Integer
@@ -20,6 +20,3 @@ powMod :: Integer -> Integer -> Integer -> Integer
20
20
powMod _ 0 _ = 1
21
21
powMod x 1 m = mod x m
22
22
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 ]
You can’t perform that action at this time.
0 commit comments