From aaaab5c4f40e7503d1fc0f92b942e40aba6011c5 Mon Sep 17 00:00:00 2001 From: Vitalis Salis Date: Fri, 26 Feb 2016 12:41:48 +0200 Subject: [PATCH] Add solutions to problems 1, 4, 5, 6, 9 in haskell --- prob-1/prob1.hs | 1 + prob-4/prob4.hs | 10 ++++++++++ prob-5/prob5.hs | 12 ++++++++++++ prob-6/prob6.hs | 3 +++ prob-9/prob9.hs | 1 + 5 files changed, 27 insertions(+) create mode 100644 prob-1/prob1.hs create mode 100644 prob-4/prob4.hs create mode 100644 prob-5/prob5.hs create mode 100644 prob-6/prob6.hs create mode 100644 prob-9/prob9.hs diff --git a/prob-1/prob1.hs b/prob-1/prob1.hs new file mode 100644 index 0000000..2038b6e --- /dev/null +++ b/prob-1/prob1.hs @@ -0,0 +1 @@ +mysum = sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0] diff --git a/prob-4/prob4.hs b/prob-4/prob4.hs new file mode 100644 index 0000000..5baf6a4 --- /dev/null +++ b/prob-4/prob4.hs @@ -0,0 +1,10 @@ +isPalindrome :: String -> Bool +isPalindrome [] = True +isPalindrome [x] = True +isPalindrome xs + | x == y = isPalindrome (tail (init xs)) + | otherwise = False + where y = last xs + x = head xs + +maxPal = maximum [x * y | x <- [1..999], y <- [1..999], isPalindrome (show (x * y))] diff --git a/prob-5/prob5.hs b/prob-5/prob5.hs new file mode 100644 index 0000000..bc2ae12 --- /dev/null +++ b/prob-5/prob5.hs @@ -0,0 +1,12 @@ +isDivisible :: Integer -> Integer -> Bool +isDivisible _ 1 = True +isDivisible n x + | n `mod` x == 0 = isDivisible n (x - 1) + | otherwise = False + +findNum :: Integer -> Integer +findNum n + | isDivisible n 20 = n + | otherwise = findNum (n + 20) + +ans = findNum 20 diff --git a/prob-6/prob6.hs b/prob-6/prob6.hs new file mode 100644 index 0000000..ea106cc --- /dev/null +++ b/prob-6/prob6.hs @@ -0,0 +1,3 @@ +small_sum = sum [x ^ 2 | x <- [1..100]] +big_sum = (sum [x | x <- [1..100]]) ^ 2 +ans = big_sum - small_sum diff --git a/prob-9/prob9.hs b/prob-9/prob9.hs new file mode 100644 index 0000000..b6f833b --- /dev/null +++ b/prob-9/prob9.hs @@ -0,0 +1 @@ +ans = head [a * b * c | c <- [1..500], b <- [1..c], a <- [1..b], a ^ 2 + b ^ 2 == c ^ 2 && a + b + c == 1000]