求a^N幂方
http://bbs.xdcad.net/
;;;求a^N幂方如(pow 5 10000)将得到6990位的大数
(defun fbin (n)
(setq binlst nil)
(while (> n 0)
(setq b (rem n 2))
(setq binlst (cons b binlst))
(setq n (lsh n -1))
)
binlst
)
(defun add (lst1 lst2)
(setq k1 (length lst1))
(setq k2 (length lst2))
(if (> k1 k2)
(repeat (- k1 k2)(setq lst2 (cons 0 lst2)) )
(repeat (- k2 k1)(setq lst1 (cons 0 lst1)) )
)
(setq addlst (mapcar '(lambda (x y) (+ x y)) lst1 lst2 ))
addlst
)
(defun lnum (lst n n0)
(setq lst (mapcar '(lambda (x) (* x n))lst))
(setq zlst (reverse lst))
(repeat n0 (setq zlst (cons 0 zlst) ) )
(reverse zlst)
)
(defun xc(lst1 lst2)
(setq n1 (length lst1))
(setq n2 (length lst2))
(setq j 1 fflst '(0))
(if (> n1 n2)
(while (<= j n2)
(setq fflst (add (lnum lst1 (nth (- j 1) lst2) (- n2 j) ) fflst))
(setq j (+ 1 j))
)
(while (<= j n1)
(setq fflst (add (lnum lst2 (nth (- j 1) lst1) (- n1 j) ) fflst))
(setq j (+ 1 j))
)
)
fflst
)
(defun jinw (blst)
(while (not (apply 'and (mapcar '(lambda (x) (<= x 9)) blst )))
(setq hlst (mapcar '(lambda (x) (rem x 10)) blst))
(setq qlst (mapcar '(lambda (x) (/ x 10)) blst))
(setq qlst (append qlst (list 0)))
(setq blst (add hlst qlst))
)
blst
)
(defun sr (a)
(setq n (strlen a) i 1 lst nil)
(while (<= i n)
(setq lst (cons (atoi (substr a i 1)) lst))
(setq i (+ 1 i))
)
(reverse lst)
)
(defun dc (a b)
(setq lst1 (sr a))
(setq lst2 (sr b))
(setq tenlst (jinw (xc lst1 lst2)))
(setq str (apply 'strcat (mapcar '(lambda (x) (itoa x)) tenlst ) ) )
(if (= (car tenlst) 0)
(setq str (vl-string-left-trim "0" str))
)
str
)
(defun pow (x n)
(setq x (itoa x))
(if (= (rem n 2) 0) (setq y "1")(setq y x))
(setq binlst (cdr (reverse (fbin n))))
(setq xlst (mapcar '(lambda (i) (setq x (dc x x)))binlst))
(setq ylst (mapcar '(lambda (i j) (if (= i 1) (setq y (dc y j)))) binlst xlst))
(last ylst)
)
(pow 5 10000)
页:
[1]