Permalink
Browse files

Nothing special. A usual commit.

  • Loading branch information...
Liutos committed Mar 24, 2012
1 parent 01fec53 commit f67f426f07a4f261477d617cc820504e01dc8053
View
@@ -1,4 +1,4 @@
words.txt
-pro8.hi
-pro8.o
-simpletext.pdf
+haskell/pro8.hi
+haskell/pro8.o
+haskell/simpletext.pdf
View
@@ -0,0 +1,18 @@
+collatzLength :: Int -> Int
+collatzLength n =
+ let rec m times
+ | 1 == m = times
+ | even m = rec (m `div` 2) (1 + times)
+ | otherwise = rec (1 + 3 * m) (1 + times)
+ in rec n 1
+
+pro14Helper :: (Int, Int) -> Int -> (Int, Int)
+pro14Helper (max, win) i =
+ let len = collatzLength i
+ in if len > win
+ then (i, len)
+ else (max, win)
+
+pro14 :: Int -> (Int, Int)
+pro14 n =
+ foldl pro14Helper (0, 0) [2..n]
View
@@ -0,0 +1,7 @@
+pro16 :: Int -> Int
+pro16 n =
+ let rec num sum =
+ if 0 == num
+ then sum
+ else rec (num `div` 10) (sum + num `mod` 10)
+ in rec n 0
View
@@ -0,0 +1,13 @@
+import Data.Char
+
+fact n =
+ let rec m prod =
+ if 1 == m
+ then prod
+ else rec (m - 1) (prod * m)
+ in rec n 1
+
+pro20 n =
+ if n < 10
+ then n
+ else (n `mod` 10) + (pro20 $ n `div` 10)
View
@@ -0,0 +1,22 @@
+fib :: Integer -> Integer
+fib n
+ | 1 == n = 1
+ | 2 == n = 1
+ | otherwise = rec 1 1 n
+ where rec a b m =
+ if 2 == m then b else rec b (a + b) (m - 1)
+
+numLength :: Integer -> Integer
+numLength n =
+ let rec num len =
+ if 0 == num then len
+ else rec (num `div` 10) (len + 1)
+ in rec n 0
+
+pro25 :: Integer -> Integer
+pro25 len =
+ let rec term =
+ if numLength (fib term) >= len
+ then term
+ else rec $ term + 1
+ in rec 13
View
@@ -0,0 +1,13 @@
+coinsValue :: Int -> Int
+coinsValue n =
+ [1, 2, 5, 10, 20, 50, 100, 200] !! (n - 1)
+
+consCoins :: Int -> Int -> Int
+consCoins amount kindsOfCoins
+ | 0 == amount = 1
+ | amount < 0 || 0 == kindsOfCoins = 0
+ | otherwise = part1 + part2
+ where part1 = consCoins amount (kindsOfCoins - 1)
+ part2 = consCoins (amount - (coinsValue kindsOfCoins)) kindsOfCoins
+
+main = print $ consCoins 200 8
View
@@ -0,0 +1,23 @@
+import Data.Char
+
+numLength :: Int -> Int
+numLength num =
+ let rec n len =
+ if n <= 0 then len else rec (n `div` 10) (len + 1)
+ in rec num 0
+
+helper :: Int -> Int
+helper n =
+ let rec index testNum =
+ let len = numLength testNum
+ in if index <= len
+ then (ord $ (show testNum) !! (index - 1)) - ord '0'
+ else rec (index - len) (testNum + 1)
+ in rec n 1
+
+exptBy10 :: Int -> Int
+exptBy10 e
+ | 0 >= e = 1
+ | otherwise = 10 * (exptBy10 $ e - 1)
+
+main = print $ product $ map helper $ map exptBy10 [0..6]
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,18 @@
+;;; Iterative version
+(defun pro1 (num)
+ (let ((sum 0))
+ (loop for i from 1 to (1- num)
+ do (if (or (zerop (mod i 3))
+ (zerop (mod i 5)))
+ (incf sum i)))
+ sum))
+;;; Recursive version
+(defun pro1 (num)
+ (labels ((rec (n sum)
+ (if (>= n num)
+ sum
+ (if (or (zerop (mod n 3))
+ (zerop (mod n 5)))
+ (rec (1+ n) (+ sum n))
+ (rec (1+ n) sum)))))
+ (rec 1 0)))
View
@@ -0,0 +1,17 @@
+(defun primep (number)
+ (labels ((rec (n)
+ (if (> (* n n) number)
+ t
+ (if (zerop (rem number n))
+ nil
+ (rec (1+ n))))))
+ (and (/= 1 number)
+ (rec 2))))
+
+(defun pro10 ()
+ (let ((sum 0))
+ (loop
+ :for n :from 1 :upto 2000000
+ :do (if (primep n)
+ (incf sum n)))
+ sum))
View
@@ -0,0 +1,27 @@
+(defun divisor-count (number)
+ (let ((cnt 0))
+ (dotimes (i (truncate (sqrt number)))
+ (if (zerop (mod number (1+ i)))
+ (incf cnt 2)))
+ cnt))
+
+(defun triangle-number-p (number)
+ (let ((root (truncate (/ (1- (sqrt (1+ (* 8 number)))) 2))))
+ (= number
+ (/ (* root (1+ root)) 2))))
+
+(defun pro12 ()
+ (labels ((rec (n)
+ (if (and (triangle-number-p n)
+ (> (divisor-count n) 500))
+ n
+ (rec (1+ n)))))
+ (rec 28)))
+
+(defun pro12 ()
+ (labels ((rec (test inc)
+ (let ((cnt (divisor-count test)))
+ (if (> cnt 500)
+ test
+ (rec (+ test inc) (1+ inc))))))
+ (rec 1 2)))
View
@@ -0,0 +1,45 @@
+(defun collatz-length (n)
+ (labels ((rec (m times)
+ (if (= 1 m)
+ times
+ (if (evenp m)
+ (rec (/ m 2) (1+ times))
+ (rec (1+ (* 3 m)) (1+ times))))))
+ (rec n 1)))
+
+(defun pro14 (n)
+ (let ((max nil)
+ (win 0))
+ (loop
+ :for i from 2 upto n
+ :do (let ((len (collatz-length i)))
+ (if (> len win)
+ (setf max i
+ win len))))
+ (values max win)))
+;;; The version used the hash table
+(defparameter *collatz*
+ (make-hash-table :test #'eql))
+
+(setf (gethash 1 *collatz*) 1)
+
+(defun collatz-length/hash (n)
+ (multiple-value-bind (value found)
+ (gethash n *collatz*)
+ (if found
+ value
+ (let ((m (if (evenp n) (/ n 2) (1+ (* 3 n)))))
+ (let ((v (collatz-length/hash m)))
+ (setf (gethash m *collatz*) v)
+ (1+ v))))))
+
+(defun pro14/hash (n)
+ (let ((max nil)
+ (win 0))
+ (loop
+ :for i from 2 upto n
+ :do (let ((len (collatz-length/hash i)))
+ (if (> len win)
+ (setf max i
+ win len))))
+ (values max win)))
View
@@ -0,0 +1,7 @@
+(defun pro16 (n)
+ (labels ((rec (num sum)
+ (if (zerop num)
+ sum
+ (rec (truncate (/ num 10))
+ (+ sum (rem num 10))))))
+ (rec n 0)))
View
@@ -0,0 +1,9 @@
+(defun pro17 (n)
+ (let ((cnt 0))
+ (loop
+ :for i from 1 upto n
+ :do (incf cnt (count-if #'(lambda (c)
+ (and (char/= #\Space c)
+ (char/= #\- c)))
+ (format nil "~R" i))))
+ cnt))
View
@@ -0,0 +1,9 @@
+(defun even-fib-sum (limit)
+ (labels ((rec (st nd sum)
+ (if (> nd limit)
+ sum
+ (rec nd (+ st nd)
+ (if (evenp nd)
+ (+ sum nd)
+ sum)))))
+ (rec 1 2 0)))
View
@@ -0,0 +1,13 @@
+(defun fact (n)
+ (labels ((rec (m prod)
+ (if (= 1 m)
+ prod
+ (rec (- m 1) (* prod m)))))
+ (rec n 1)))
+
+(defun pro20 (n)
+ (reduce #'(lambda (init c)
+ (+ init
+ (- (char-code c) (char-code #\0))))
+ (format nil "~A" n)
+ :initial-value 0))
View
@@ -0,0 +1,20 @@
+(defun divisor-sum (n)
+ (labels ((rec (test acc)
+ (if (> (* test test) n)
+ acc
+ (if (zerop (mod n test))
+ (rec (1+ test) (+ acc test (/ n test)))
+ (rec (1+ test) acc)))))
+ (rec 2 1)))
+
+(defun amicable-number-p (num)
+ (let ((tmp (divisor-sum num)))
+ (and (/= tmp num)
+ (= num (divisor-sum tmp)))))
+
+(defun pro21 ()
+ (let ((sum 0))
+ (loop :for i :from 1 :upto 10000
+ :do (if (amicable-number-p i)
+ (incf sum i)))
+ sum))
View
@@ -0,0 +1,22 @@
+(ql:quickload "cl-ppcre")
+
+(defun name-score (name-word pos)
+ (* pos
+ (apply #'+ (map 'list #'(lambda (c)
+ (1+ (- (char-code c)
+ (char-code #\A))))
+ name-word))))
+
+(defun pro22 ()
+ (with-open-file (stream "names.txt")
+ (let ((name-list (sort (cl-ppcre:split "," (read-line stream))
+ #'string<=))
+ (sum 0)
+ (index 1))
+ (dolist (nm name-list)
+ (incf sum (name-score ((lambda (name)
+ (let ((len (length name)))
+ (subseq name 1 (- len 1))))
+ nm) index))
+ (incf index))
+ sum)))
View
@@ -0,0 +1,24 @@
+(defun fib (n)
+ (labels ((rec (a b m)
+ (if (= 2 m)
+ b
+ (rec b (+ a b) (- m 1)))))
+ (if (= 1 n)
+ 1
+ (if (= 2 n)
+ 1
+ (rec 1 1 n)))))
+
+(defun num-length (n)
+ (labels ((rec (num len)
+ (if (zerop num)
+ len
+ (rec (truncate (/ num 10)) (1+ len)))))
+ (rec n 0)))
+
+(defun pro25 (len)
+ (labels ((rec (term)
+ (if (>= (num-length (fib term)) len)
+ term
+ (rec (1+ term)))))
+ (rec 13)))
View
@@ -0,0 +1,9 @@
+(defun largest-prime-factor (n)
+ (labels ((rec (num acc)
+ (cond ((= 1 num) acc)
+ ((<= num 2) num)
+ (t (let ((fac (do ((i 2 (1+ i)))
+ ((or (>= i num)
+ (zerop (mod num i))) i))))
+ (rec (/ num fac) fac))))))
+ (rec n 1)))
View
@@ -0,0 +1,14 @@
+(defun coins-value (n)
+ (nth (1- n) '(1 2 5 10 20 50 100 200)))
+
+(defun cons-coins (amount kinds-of-coins)
+ (cond ((= amount 0) 1)
+ ((or (< amount 0) (= kinds-of-coins 0)) 0)
+ (t (+ (cons-coins amount
+ (1- kinds-of-coins))
+ (cons-coins (- amount
+ (coins-value kinds-of-coins))
+ kinds-of-coins)))))
+
+(defun pro31 ()
+ (cons-coins 200 8))
View
@@ -0,0 +1,13 @@
+(defun palindromicp (number)
+ (let ((str (format nil "~A" number)))
+ (string= str (reverse str))))
+
+(defun pro4 ()
+ (let ((max 0))
+ (dotimes (i 899)
+ (dotimes (j 899)
+ (let ((mul (* (+ 100 i) (+ 100 j))))
+ (if (and (palindromicp mul)
+ (> mul max))
+ (setf max mul)))))
+ max))
Oops, something went wrong.

0 comments on commit f67f426

Please sign in to comment.