Skip to content

Commit

Permalink
Add the solutions of problems from 1 to 10 written in Haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
Liutos committed Mar 19, 2012
1 parent 893b22b commit b9b2b81
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 20 deletions.
5 changes: 5 additions & 0 deletions haskell/#pro8.hs#
Original file line number Original file line Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions haskell/.#pro8.hs
2 changes: 2 additions & 0 deletions haskell/pro1.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
pro1 :: Int -> Int
pro1 sup = sum [x | x <- [1..(sup - 1)], x `mod` 3 == 0 || x `mod` 5 == 0]
10 changes: 10 additions & 0 deletions haskell/pro10.hs
Original file line number Original file line Diff line number Diff line change
@@ -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]
6 changes: 6 additions & 0 deletions haskell/pro2.hs
Original file line number Original file line Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions haskell/pro3.hs
Original file line number Original file line Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions haskell/pro4.hs
Original file line number Original file line Diff line number Diff line change
@@ -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)]
11 changes: 11 additions & 0 deletions haskell/pro5.hs
Original file line number Original file line Diff line number Diff line change
@@ -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]
4 changes: 4 additions & 0 deletions haskell/pro6.hs
Original file line number Original file line Diff line number Diff line change
@@ -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]]
15 changes: 15 additions & 0 deletions haskell/pro7.hs
Original file line number Original file line Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions haskell/pro8.hs
Original file line number Original file line Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions haskell/pro9.hs
Original file line number Original file line Diff line number Diff line change
@@ -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]
21 changes: 1 addition & 20 deletions pro3.lisp
Original file line number Original file line Diff line number Diff line change
@@ -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) (defun largest-prime-factor (n)
(labels ((rec (num acc) (labels ((rec (num acc)
(cond ((= 1 num) acc) (cond ((= 1 num) acc)
Expand All @@ -15,14 +6,4 @@
((or (>= i num) ((or (>= i num)
(zerop (mod num i))) i)))) (zerop (mod num i))) i))))
(rec (/ num fac) fac)))))) (rec (/ num fac) fac))))))
(rec n 1))) (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))))

0 comments on commit b9b2b81

Please sign in to comment.