Permalink
Browse files

solve 7, 8, 9, 10nth problems.

  • Loading branch information...
Liutos committed Mar 11, 2012
1 parent 450e750 commit 893b22bb94d43d8c51a67deaa40c7206ae327bad
Showing with 203 additions and 1 deletion.
  1. 0 ocaml/#pro1.ml#
  2. +12 −1 pro1.lisp
  3. +17 −0 pro10.lisp
  4. +13 −0 pro4.lisp
  5. +16 −0 pro48.lisp
  6. +34 −0 pro52.lisp
  7. +17 −0 pro53.lisp
  8. +18 −0 pro7.lisp
  9. +15 −0 pro8.lisp
  10. +1 −0 pro8.txt
  11. +10 −0 pro9.lisp
  12. +12 −0 python/pro1.py
  13. +25 −0 python/pro14.py
  14. +13 −0 python/pro2.py
View
No changes.
View
@@ -1,7 +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))
+ 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,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))
View
@@ -0,0 +1,16 @@
+(defun expmod (base exp m)
+ (cond ((= exp 0) 1)
+ ((evenp exp)
+ (rem (expt (expmod base (/ exp 2) m) 2)
+ m))
+ (t
+ (rem (* base (expmod base (- exp 1) m))
+ m))))
+
+(defun pro48 (n)
+ (let ((sum 0)
+ (m (expt 10 10)))
+ (loop
+ :for i from 1 upto n
+ :do (incf sum (expmod i i m)))
+ sum))
View
@@ -0,0 +1,34 @@
+;;; This is really a ugly program
+(defun digit-chars (number)
+ (if (< number 10)
+ (list number)
+ (cons (rem number 10)
+ (digit-chars (/ (- number (rem number 10)) 10)))))
+
+(defun same-elts (lst1 lst2)
+ (and (every (lambda (elt)
+ (member elt lst2 :test #'=))
+ lst1)
+ (every (lambda (elt)
+ (member elt lst1 :test #'=))
+ lst2)))
+
+(defun digit-test (number)
+ (let ((org (digit-chars number))
+ (2org (digit-chars (* 2 number)))
+ (3org (digit-chars (* 3 number)))
+ (4org (digit-chars (* 4 number)))
+ (5org (digit-chars (* 5 number)))
+ (6org (digit-chars (* 6 number))))
+ (every (lambda (lst)
+ (same-elts org lst))
+ (list 2org 3org 4org 5org 6org))))
+
+(defun pro52 ()
+ (labels ((rec (test-number)
+ (if (> test-number 100000000)
+ nil
+ (if (digit-test test-number)
+ test-number
+ (rec (1+ test-number))))))
+ (rec 1)))
View
@@ -0,0 +1,17 @@
+(defun fac (n)
+ (let ((p 1))
+ (dotimes (i n p)
+ (setf p (* p (1+ i))))))
+
+(defun selects (n r)
+ (/ (fac n)
+ (* (fac r) (fac (- n r)))))
+
+(defun pro53 ()
+ (let ((cnt 0))
+ (loop
+ :for i from 1 upto 100
+ :do (dotimes (j i)
+ (if (> (selects i (1+ j)) 1000000)
+ (incf cnt))))
+ cnt))
View
@@ -0,0 +1,18 @@
+(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 pro7 (lim)
+ (labels ((rec (test cnt)
+ (if (= lim cnt)
+ (1- test)
+ (if (primep test)
+ (rec (1+ test) (1+ cnt))
+ (rec (1+ test) cnt)))))
+ (rec 2 0)))
View
@@ -0,0 +1,15 @@
+(defun digstr-prod (digstr)
+ (apply #'* (map 'list #'(lambda (c)
+ (- (char-code c) (char-code #\0)))
+ digstr)))
+
+(defun pro8 (digit-string)
+ (let ((max 0))
+ (dotimes (i 996)
+ (let ((num (digstr-prod (subseq digit-string i (+ 5 i)))))
+ (if (> num max)
+ (setf max num))))
+ max))
+
+(with-open-file (s "pro8.txt")
+ (pro8 (read-line s nil 'eof)))
View
@@ -0,0 +1 @@
+7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
View
@@ -0,0 +1,10 @@
+(defun pro9 ()
+ (do ((a 1 (1+ a)))
+ ((= a 1000))
+ (do ((b (1+ a) (1+ b)))
+ ((= b 1000))
+ (do ((c (1+ b) (1+ c)))
+ ((= c 1000))
+ (if (and (= (+ (* a a) (* b b)) (* c c))
+ (= (+ a b c) 1000))
+ (return-from pro9 (list a b c (* a b c))))))))
View
@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+
+def pro1(num):
+ sum = 0
+ i = 1
+ while i < num:
+ if (0 == (i % 3) or 0 == (i % 5)):
+ sum = sum + i
+ i = i + 1
+ return sum
+
+
View
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+
+def rec(m, times):
+ if (1 == m):
+ return times
+ else:
+ if (0 == m % 2):
+ return rec(m/2, times+1)
+ else:
+ return rec(3*m+1, times+1)
+
+def collatz_length(n):
+ return rec(n, 1)
+
+def pro14(n):
+ max = []
+ win = 0
+ i = 2
+ while i <= n:
+ len = collatz_length(i)
+ if (len > win):
+ max = i
+ win = len
+ i = i + 1
+ return max
View
@@ -0,0 +1,13 @@
+# -*- coding: utf-8 -*-
+
+def rec(st, nd, sum, limit):
+ if (nd > limit):
+ return sum
+ else:
+ if (0 == nd % 2):
+ return rec(nd, st+nd, sum+nd, limit)
+ else:
+ return rec(nd, st+nd, sum, limit)
+
+def pro2(limit):
+ return rec(1, 2, 0, limit)

0 comments on commit 893b22b

Please sign in to comment.