highflybir 发表于 2010-6-4 23:24
在上篇帖子中我已经说明了这个算法的大致思想。下面我贴出源码,具体算法我要强调的一点是:
我构造了一个循 ...
正在研读你指引给我的这个链接。
前段时间之所以关心素数,觉得让使用者输入200~2000有点不知所措,故改成如下- ;;;The procedure for Test
- (defun C:w1 (/ AREA1 AREA2 LST N PP PTLIST SCORE SEL T0 X)
- (princ "\nPlease select LWPOLYLINE,LINE,SPLINE,POINT")
- (cond
- ((setq
- sel (ssget (list '(0 . "POINT,LWPOLYLINE,LINE,SPLINE,ARC,CIRCLE,ELLIPSE")))
- )
- (initget 7)
- (setq n (getint "\n精度:"))
- (setq t0 (getvar "TDUSRTIMER")) ;The start time of this algorithm
- (setq lst (Sprime2 200 2000)) ;素数
- (setq area1 0)
- (while (and (setq x (car lst)) (not (equal area1 area2 n)))
- (setq area2 area1)
- (setq ptlist (Graham-scan (getpt sel x))) ;construct the CCW Hull of this set.
- ;|;扫描后的点本身就逆时针(sort-by-angle-distance),下面一句好象没有什么用处。
- (if (<= (det (car ptlist) (cadr ptlist) (caddr ptlist)) 0.0)
- ;ensure the hull is CCW.
- (setq ptlist (reverse ptlist)) ;if it isn't CCW,then reverse it
- )|;
- (setq score (MinAreaRectangle ptlist))
- (setq area1 (cdr score))
- (setq lst (cdr lst))
- )
- (princ "\nIt takes :")
- (princ (* (- (getvar "TDUSRTIMER") t0) 86400)) ;The End time
- (princ "seconds")
- (if (setq pp (car score)) ;start calculating
- (make-poly pp) ;draw rectangle.
- )
- )
- )
- (princ)
- )
- ;;[功能] 小于n的质数
- ;;(SPrime 100)=>(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)
- (defun Sprime (n / I K L LST ROOT)
- (setq i 1)
- (setq root (sqrt n))
- (while (<= (setq i (+ i 2)) n) (setq l (cons i l)))
- (setq l (reverse l))
- (while (and (setq k (car l)) (<= k root))
- (setq l (vl-remove-if '(lambda (x) (= (rem x k) 0)) (cdr l)))
- (setq lst (cons k lst))
- )
- (append '(2) (reverse lst) l)
- )
- ;;[功能] n1 n2之间的质数
- ;;(Sprime 200 2000)
- (defun Sprime2 (n1 n2 / LST)
- (setq lst (Sprime n2))
- (vl-remove-if '(lambda (x) (< x n1)) lst)
- )
|