Permalink
Browse files

好久没提交了,只多做了几题……

  • Loading branch information...
1 parent ff36d2e commit 679a9a48472433e5223ce947ba863a6364714773 @Liutos committed Apr 30, 2012
Showing with 430 additions and 38 deletions.
  1. BIN erlang/pro1.beam
  2. +13 −0 erlang/pro1.erl
  3. +48 −0 lisp/#pro58.lisp#
  4. +1 −0 lisp/.#pro58.lisp
  5. +7 −5 lisp/pro10.lisp
  6. +12 −33 lisp/pro27.lisp
  7. +24 −0 lisp/pro28.lisp
  8. +10 −0 lisp/pro3.lisp
  9. +21 −0 lisp/pro39.lisp
  10. +45 −0 lisp/pro41.lisp
  11. +30 −0 lisp/pro43.lisp
  12. +13 −0 lisp/pro45.lisp
  13. +70 −0 lisp/pro46.lisp
  14. +41 −0 lisp/pro47.lisp
  15. +5 −0 lisp/pro5.lisp
  16. +35 −0 lisp/pro55.lisp
  17. +20 −0 lisp/pro56.lisp
  18. +35 −0 lisp/pro57.lisp
View
Binary file not shown.
View
@@ -0,0 +1,13 @@
+-module(pro1).
+-export([pro1/1]).
+
+rec(N, Sum, Num) ->
+ if
+ N >= Num -> Sum;
+ 0 == N rem 3 -> rec(N+1, Sum+N, Num);
+ 0 == N rem 5 -> rec(N+1, Sum+N, Num);
+ true -> rec(N+1, Sum, Num)
+ end.
+
+pro1(Num) ->
+ rec(1, 0, Num).
View
@@ -0,0 +1,48 @@
+(proclaim '(optimize speed))
+
+(defun nth-gof (n prev-last)
+ (let* ((init prev-last)
+ (len (- (* 2 n) 1))
+ (head (+ init len -1))
+ (dis (* 2 (- n 1))))
+ (list head
+ (+ head dis)
+ (+ head (* 2 dis))
+ (+ head (* 3 dis)))))
+
+(defun primep (n)
+ (let ((bound (truncate (sqrt n))))
+ (labels ((rec (test)
+ (cond ((> test bound) t)
+ ((= 0 (rem n test)) nil)
+ (t (rec (1+ test))))))
+ (or (= 2 n)
+ (and (oddp n)
+ (rec 3))))))
+
+(defun nth-ratio (n)
+ (labels ((rec (numerator denominator n-layer prev-last)
+ (if (> n-layer n)
+ (values numerator denominator)
+ (destructuring-bind (rt lt lb rb)
+ (nth-gof n-layer prev-last)
+ (rec (+ numerator
+ (count-if #'primep (list rt lt lb rb)))
+ (+ denominator 4)
+ (1+ n-layer)
+ rb)))))
+ (rec 0 1 2 1)))
+
+(defun pro58 ()
+ (labels ((rec (numerator denominator n-layer prev-last)
+ (if (and (/= 0 numerator)
+ (< (* 10 numerator) denominator))
+ (values numerator denominator (- (* 2 n-layer) 1))
+ (destructuring-bind (rt lt lb rb)
+ (nth-gof n-layer prev-last)
+ (rec (+ numerator
+ (count-if #'primep (list rt lt lb rb)))
+ (+ denominator 4)
+ (1+ n-layer)
+ rb)))))
+ (rec 0 1 2 1)))
View
View
@@ -1,17 +1,19 @@
+(declaim (optimize (speed 3)))
+
(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))))
+ (rec 2)))
(defun pro10 ()
- (let ((sum 0))
+ (let ((sum 2))
(loop
- :for n :from 1 :upto 2000000
+ :for n :from 3 :upto 2000000 :by 2
:do (if (primep n)
- (incf sum n)))
+ (incf sum
+ (the fixnum n))))
sum))
View
@@ -1,35 +1,14 @@
-(declaim (optimize (speed 3)))
+(proclaim '(optimize speed))
-(defun primep (n)
+(defun primep (number)
(labels ((rec (test)
- (if (> (* test test) n)
- t
- (if (zerop (mod n test))
- nil
- (rec (1+ test))))))
- (and (/= 1 n)
- (rec 2))))
-
-(defun cons-form (a b)
- #'(lambda (n)
- (+ (* n (+ n a)) b)))
-
-(defun prime-len (fn)
- (labels ((rec (acc test-n)
- (if (primep (funcall fn test-n))
- (rec (1+ acc) (1+ test-n))
- acc)))
- (rec 0 0)))
-
-(defun pro27 ()
- (let ((prod)
- (score 0))
- (do ((a -999 (1+ a)))
- ((<= 1000 a))
- (do ((b -999 (+ b 2)))
- ((>= b 100))
- (let ((len (prime-len (cons-form a b))))
- (if (> len score)
- (setf prod (* a b)
- score len)))))
- (values prod score)))
+ (cond ((> (* test test) number) t)
+ ((= 0 (rem number test)) nil)
+ (t (rec (1+ test))))))
+ (cond ((= 1 number) nil)
+ ((= 2 number) t)
+ (t (rec 2)))))
+
+(defun cons-fn (a b)
+ (lambda (n)
+ (+ (* n n) (* a n) b)))
View
@@ -0,0 +1,24 @@
+;;; The recursive version
+(defun helix-sum (n)
+ (if (= 1 n)
+ (values 1 1)
+ (multiple-value-bind (sum prev) (helix-sum (1- n))
+ (let ((len (1- (* 2 n)))
+ (rb-value (+ prev (* 2 (1- n)))))
+ (values (+ sum (* 4 rb-value) (* 6 (1- len)))
+ (+ rb-value (* 3 (1- len))))))))
+
+;;; The iterative version
+(defun helix-sum (n)
+ (labels ((rec (acc layer prev)
+ (if (> layer n)
+ acc
+ (let ((len (1- (* 2 layer)))
+ (rb-value (+ prev (* 2 (1- layer)))))
+ (rec (+ acc (* 4 rb-value) (* 6 (1- len)))
+ (1+ layer)
+ (+ rb-value (* 3 (1- len))))))))
+ (if (= 1 n) 1 (rec 1 2 1))))
+
+(defun pro28 (n)
+ (helix-sum (/ (1+ n) 2)))
View
@@ -1,10 +1,20 @@
+;;; 这道题目就是要计算一个数的最大质因数,所以只要从最小的质数开始计算就可以了。不过也没必要没每到一
+;;; 个因数就判断是否是因数,只要对于当前求最大质因数的数,假设为n,找出它的最小因数d(它必然是质数)
+;;; ,除以这个最小因数,得到的商为m=n/d,这个m的最大因数必然也是n的最大质因数,所以接下来就计算m的
+;;; 最大质因数就好了。很显然,这里可以使用递归来优雅地处理。smallest-factor函数用于计算最小的质因
+;;; 数,然后largest-prime-factor内部的rec就以(/ num fac)来作为新的等待求最大质因数的参数。
(defun smallest-factor (n)
(labels ((rec (test)
(if (or (>= test n) (zerop (rem n test)))
test
(rec (1+ test)))))
(rec 2)))
+;;; 由labels定义的函数rec的第一个参数是用于继续求最大质因数的数字,而acc则是存储着上一个数的最大质
+;;; 因数的参数。在每一次内部迭代中,我都要判断num是否为1。如果num为1,则表示原来的参数n已经被整除到
+;;; 极限了,acc就是上一个数的最大质因数,也是n的最大质因数。否则,就计算num的最小因数fac,用fac除
+;;; 参数num,得到的数的最大质因数必然还是num的最大质因数,同时也是n的最大质因数。至于代码中的第二个
+;;; 测试条件,则一般不会遇到了。
(defun largest-prime-factor (n)
(labels ((rec (num acc)
(cond ((= 1 num) acc)
View
@@ -0,0 +1,21 @@
+(defun triangle-numbers (n)
+ (labels ((rec (acc tt ts)
+ (cond ((or (> (* ts tt) n)
+ (> (/ (- (* ts ts) (* ts ts)) 2) n))
+ acc)
+ ((> (* ts (+ tt ts)) n)
+ (rec acc (1+ tt) (+ tt 2)))
+ ((or (= n (* ts (+ tt ts)))
+ (= 0 (rem n (* ts (+ ts ts)))))
+ (rec (1+ acc) tt (1+ ts)))
+ (t (rec acc tt (1+ ts))))))
+ (rec 0 1 2)))
+
+(defun pro39 ()
+ (let ((max 0) (win 0))
+ (loop :for i :from 1 :upto 1000
+ :do (let ((score (triangle-numbers i)))
+ (if (> score win)
+ (setf max i
+ win score))))
+ (values max win)))
View
@@ -0,0 +1,45 @@
+(proclaim '(optimize speed))
+
+(defun number-length (number)
+ (labels ((rec (acc n)
+ (if (= 0 n)
+ acc
+ (rec (1+ acc) (truncate (/ n 10))))))
+ (if (= 0 number) 1 (rec 0 number))))
+
+(defun pandigitalp (number)
+ (let ((len (number-length number)))
+ (let ((aux (make-array `(,len) :initial-element 0)))
+ (labels ((rec (n)
+ (if (= 0 n)
+ (every #'(lambda (x) (= 1 x)) aux)
+ (let ((lsb (rem n 10)))
+ (cond ((or (= 0 lsb) (> lsb len))
+ (return-from pandigitalp nil))
+ (t
+ (incf (aref aux (1- lsb)))
+ (if (> (aref aux (1- lsb)) 1)
+ (return-from pandigitalp nil))
+ (rec (/ (- n lsb) 10))))))))
+ (if (= 0 number) nil (rec number))))))
+
+(defun primep (number)
+ (let ((bnd (ceiling (sqrt number))))
+ (labels ((rec (test)
+ (cond ((> test bnd) t)
+ ((= 0 (rem number test)) nil)
+ (t (rec (+ test 2))))))
+ (cond ((= 1 number) nil)
+ ((= 2 number) t)
+ ((= 0 (rem number 2)) nil)
+ (t (rec 3))))))
+
+(defun pro41 ()
+ (labels ((rec (test)
+ (if (< test 1)
+ "Anwser not found"
+ (if (and (pandigitalp test)
+ (primep test))
+ test
+ (rec (- test 2))))))
+ (rec 7654321)))
View
@@ -0,0 +1,30 @@
+(proclaim '(optimize speed))
+
+(defun 0->9-pandigital-p (number)
+ (let ((aux (make-array '(10) :initial-element 0)))
+ (labels ((rec (test)
+ (cond ((= 0 test)
+ (every #'(lambda (x) (= 1 x)) aux))
+ (t
+ (incf (aref aux (rem test 10)))
+ (rec (truncate (/ test 10)))))))
+ (rec number))))
+
+(defun 3-subnum (number start)
+ (parse-integer (format nil "~D" number)
+ :start start
+ :end (+ start 3)))
+
+(defun help-test (number)
+ (and (0->9-pandigital-p number)
+ (every #'(lambda (x prime)
+ (= 0 (rem (3-subnum number x) prime)))
+ '(7 6 5 4 3 2 1)
+ '(17 13 11 7 5 3 2))))
+
+(defun pro43 ()
+ (let ((sum 0))
+ (do ((i 1245789306 (+ i 17)))
+ ((> i 9876543210) sum)
+ (if (help-test i)
+ (incf sum i)))))
View
@@ -0,0 +1,13 @@
+(defun help-test (n)
+ (let ((pr (/ (1+ (sqrt (1+ (* 24 n)))) 6)))
+ (and (= (truncate pr) pr)
+ (let ((hr (/ (1+ (sqrt (1+ (* 8 n)))) 4)))
+ (= (truncate hr) hr)))))
+
+(defun pro45 ()
+ (labels ((rec (index test)
+ (if (help-test test)
+ test
+ (rec (1+ index)
+ (+ test (1+ index))))))
+ (rec 286 (+ 40755 285 1))))
View
@@ -0,0 +1,70 @@
+(proclaim '(optimize speed))
+
+(defparameter *primes*
+ '(2 3 5 7 11 13 17 19 23 29 31 41))
+
+(defparameter *primes-list*
+ (cons *primes* (last *primes*)))
+
+(defun concat-prime (n)
+ (nconc (cdr *primes-list*) `(,n))
+ (setf (cdr *primes-list*)
+ (cddr *primes-list*)))
+
+(defun primep/list (n)
+ (labels ((rec (test-list)
+ (let ((test (car test-list)))
+ (cond ((> (* test test) n)
+ (if (> n (cadr *primes-list*))
+ (concat-prime n))
+ t)
+ ((= 0 (rem n test)) nil)
+ (t (rec (cdr test-list)))))))
+ (rec *primes*)))
+
+(defun primep/+ (n)
+ (labels ((rec (test)
+ (cond ((> (* test test) n) t)
+ ((= 0 (rem n test)) nil)
+ (t (rec (1+ test))))))
+ (rec 2)))
+
+(defun primep (n)
+ (cond ((= 1 n) nil)
+ ((let ((test (cadr *primes-list*)))
+ (< n (* test test)))
+ (primep/list n))
+ (t (primep/+ n))))
+
+(defun squarep (n)
+ (let ((sqrt (sqrt n)))
+ (= sqrt (truncate sqrt))))
+
+(defun helper (n p)
+ (let ((r (- n p)))
+ (and (evenp r) (squarep (/ r 2)) p)))
+
+(defun pro46-test (n)
+ (labels ((rec (primes test)
+ (cond (primes
+ (let ((p (car primes)))
+ (if (> p n)
+ nil
+ (or (helper n p)
+ (rec (cdr primes) p)))))
+ (t
+ (cond ((primep test)
+ (concat-prime test)
+ (if (helper n test)
+ test
+ (rec nil (+ test 2))))
+ (t
+ (rec nil (+ test 2))))))))
+ (rec *primes* 2)))
+
+(defun pro46 ()
+ (labels ((rec (test)
+ (if (pro46-test test)
+ (rec (+ test 2))
+ test)))
+ (rec 5)))
Oops, something went wrong.

0 comments on commit 679a9a4

Please sign in to comment.