Skip to content

Commit 1eb9303

Browse files
author
Matthew Gilliard
committed
added haskell solution for problem #5
1 parent beadb9b commit 1eb9303

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+

teams/mjg-haskell/BrisFunctional/ProblemTests.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ module BrisFunctional.ProblemTests where
33
import BrisFunctional.Problem1
44
import BrisFunctional.Problem2
55
import BrisFunctional.Problem3
6+
import BrisFunctional.Problem5
67

78
import Test.HUnit
89

910
main = runTestTT $ TestList
1011
[ testProblem1
1112
, testProblem2
12-
, testProblem3 ]
13+
, testProblem3
14+
, testProblem5 ]
1315

0 commit comments

Comments
 (0)