File tree Expand file tree Collapse file tree 3 files changed +52
-1
lines changed
teams/mjg-haskell/BrisFunctional Expand file tree Collapse file tree 3 files changed +52
-1
lines changed Original file line number Diff line number Diff line change
1
+ module BrisFunctional.Problem3
2
+ ( problem3
3
+ , testProblem3 )
4
+ where
5
+
6
+ import Test.HUnit
7
+
8
+ -- Problem 3
9
+ -- What is the largest prime factor of the number 600851475143 ?
10
+ problem3 :: (Integral a ) => a -> a
11
+ problem3 a = recurse a 2
12
+
13
+ recurse :: (Integral a ) => a -> a -> a
14
+ recurse num divisor
15
+ | num == divisor = num
16
+ | num `mod` divisor == 0 = recurse (num `div` divisor) divisor
17
+ | otherwise = recurse num (divisor+ 1 )
18
+
19
+
20
+ -- test case
21
+ testProblem3 = TestCase $ assertEqual
22
+ " Problem 3 answer = 6857"
23
+ 6857
24
+ ( problem3 600851475143 )
25
+
Original file line number Diff line number Diff line change
1
+ module BrisFunctional.Problem5
2
+ ( problem5
3
+ , testProblem5 )
4
+ where
5
+
6
+ import Test.HUnit
7
+
8
+ -- Problem 5
9
+ -- What is the smallest positive number that is evenly divisible
10
+ -- by all of the numbers from 1 to 20?
11
+ -- nb 6840 = 20*19*18
12
+ problem5 :: Integer -> Integer
13
+ problem5 a
14
+ | divides1_20 a = a
15
+ | otherwise = problem5 (a+ 6840 )
16
+ where divides1_20 = (\ s -> (all (== True ) (map (\ t -> s `mod` t == 0 ) [1 .. 20 ])))
17
+
18
+
19
+ -- test case
20
+ testProblem5 = TestCase $ assertEqual
21
+ " Problem 5 answer = 232792560"
22
+ 232792560
23
+ ( problem5 6840 )
24
+
Original file line number Diff line number Diff line change @@ -3,11 +3,13 @@ module BrisFunctional.ProblemTests where
3
3
import BrisFunctional.Problem1
4
4
import BrisFunctional.Problem2
5
5
import BrisFunctional.Problem3
6
+ import BrisFunctional.Problem5
6
7
7
8
import Test.HUnit
8
9
9
10
main = runTestTT $ TestList
10
11
[ testProblem1
11
12
, testProblem2
12
- , testProblem3 ]
13
+ , testProblem3
14
+ , testProblem5 ]
13
15
You can’t perform that action at this time.
0 commit comments