# Liutos/Project-Euler

Add the solutions of problems from 1 to 10 written in Haskell

1 parent 893b22b commit b9b2b8174accfbe6a61a10aa37922882c920ea6b committed Mar 19, 2012
Showing with 84 additions and 20 deletions.
13. +1 −20 pro3.lisp
 @@ -0,0 +1,5 @@ +import qualified Data.Char as Char + +digStrProd :: String -> Int +digStrProd digStr = + product \$ map (\c -> Char.ord c - Char.ord '0') digStr
 @@ -0,0 +1,2 @@ +pro1 :: Int -> Int +pro1 sup = sum [x | x <- [1..(sup - 1)], x `mod` 3 == 0 || x `mod` 5 == 0]
 @@ -0,0 +1,10 @@ +isPrime :: Int -> Bool +isPrime number = + number /= 1 && rec 2 + where rec n + | n * n > number = True + | 0 == number `mod` n = False + | otherwise = rec (n + 1) + +pro10 = + sum [ n | n <- [1..200000], isPrime n]
 @@ -0,0 +1,6 @@ +pro2 limit = + let rec st nd sum = + if nd > limit + then sum + else rec nd (st + nd) (if even nd then sum + nd else sum) + in rec 1 2 0
 @@ -0,0 +1,15 @@ +pro3Helper :: Integer -> Integer +pro3Helper n = + let rec testNum + | testNum >= n || 0 == n `mod` testNum = testNum + | otherwise = rec (testNum + 1) + in rec 2 + +pro3 :: Integer -> Integer +pro3 n = + let rec num acc + | 1 == num = acc + | num <= 2 = num + | otherwise = rec (num `div` fac) fac + where fac = pro3Helper num + in rec n 1
 @@ -0,0 +1,7 @@ +isPalindromic :: Int -> Bool +isPalindromic number = + let str = show number + in str == reverse str + +pro4 = + maximum [x * y | x <- [100..999], y <- [100..999], isPalindromic (x * y)]
 @@ -0,0 +1,11 @@ +helper :: Int -> [Int] -> Int +helper n lst = + foldl (\n d -> if 0 == n `mod` d then n `div` d else n) n lst + +pro5 :: Int -> Int +pro5 n = + let rec m lst = + if m >= n + then product lst + else rec (m + 1) (helper m lst : lst) + in rec 2 [1]
 @@ -0,0 +1,4 @@ +pro6 :: Int -> Int +pro6 n = + let tmp = (n * (n + 1)) `div` 2 + in tmp * tmp - sum [ x * x | x <- [1..n]]
 @@ -0,0 +1,15 @@ +isPrime :: Int -> Bool +isPrime number = + number /= 1 && rec 2 + where rec n + | n * n > number = True + | 0 == number `mod` n = False + | otherwise = rec (n + 1) + +pro7 :: Int -> Int +pro7 lim = + rec 2 0 + where rec test cnt + | lim == cnt = test - 1 + | isPrime test = rec (test + 1) (cnt + 1) + | otherwise = rec (test + 1) cnt
 @@ -0,0 +1,5 @@ +import qualified Data.Char as Char + +digStrProd :: String -> Int +digStrProd digStr = + product \$ map (\c -> Char.ord c - Char.ord '0') digStr
 @@ -0,0 +1,2 @@ +pro9 = + [(a, b, c, a * b * c) | a <- [1..1000], b <- [a..1000], c <- [b..1000], 1000 == a + b + c, a * a + b * b == c * c]
 @@ -1,12 +1,3 @@ -(defun primep (n) - (labels ((rec (test-num) - (if (> (* test-num test-num) n) - t - (if (zerop (mod n test-num)) - nil - (rec (1+ test-num)))))) - (rec 2))) - (defun largest-prime-factor (n) (labels ((rec (num acc) (cond ((= 1 num) acc) @@ -15,14 +6,4 @@ ((or (>= i num) (zerop (mod num i))) i)))) (rec (/ num fac) fac)))))) - (rec n 1))) - -(defun largest-prime-factor (n) - (declare (number n)) - (let ((max 2)) - (declare (fixnum max)) - (loop for i from 3 to (- n 2) by 2 - do (if (zerop (mod n i)) - (if (primep i) - (setq max i))) - finally (return max)))) + (rec n 1)))