I was about to bring this challenge up
So, this is a way to multiply large positive numbers:
(defun multip (a b / q m n r c l)
(setq q (max (length a) (length b)))
(repeat (- q (length a))
(setq a (cons 0 a))
)
(repeat (- q (length b))
(setq b (cons 0 b))
)
(setq c 0)
(repeat q
(setq q (1- q)
m (cons (nth q a) m)
n (cons (nth q b) n)
r (+ c (apply '+ (mapcar '* m (reverse n))))
c (/ r 10)
l (cons (rem r 10) l)
)
)
(setq n (reverse n)
m (reverse m)
)
(while (setq m (cdr m))
(setq n (cdr n)
r (+ c (apply '+ (mapcar '* m (reverse n))))
c (/ r 10)
l (cons (rem r 10) l)
)
)
(while (> c 0)
(setq l (cons (rem c 10) l)
c (/ c 10)
)
)
(while (zerop (car l)) (setq l (cdr l)))
l
)
_$ (MULTIP '(4 1 2 8 6 6 3 0 7) '(2 4 9 5 1 3 6 1 6))
(1 0 3 0 1 5 7 6 5 1 8 4 1 3 6 1 1 2)
_$ (MULTIP '(4 1 2 8 6 6 3 0 7 4 1 2 8 6 6 3 0 7 4 1 2 8 6 6 3 0 7) '(2 4 9 5 1 3 6 1 6 2 4 9 5 1 3 6 1 6 2 4 9 5 1 3 6 1 6))
(1 0 3 0 1 5 7 6 5 3 9 0 1 6 7 6 4 2 6 7 7 3 1 9 5 1 9 7 5 8 4 3 9 8 6 6 4 7 1 2 8 7 9 8 9 1 8 4 1 3 6 1 1 2)
_$
More efficient would be to divide the original number in fractions of thousand. The lisp above will work if 10 is replaced by 1000.
But I'm more interested of some clever codes. Mine is (almost) just a translation of the way the kids are doing multiplication in class.
... and the test function
(defun test (a b / l1 l2)
(if
(and
(eq (type a) 'int)
(eq (type b) 'int)
)
(progn
(while (> a 0)
(setq l1 (cons (rem a 10) l1)
a (/ a 10)
)
)
(while (> b 0)
(setq l2 (cons (rem b 10) l2)
b (/ b 10)
)
)
;;; (alert
(apply 'strcat (mapcar 'itoa (multip l1 l2)))
;;; )
)
)
)
_$ (test 412866307 249513616)
"103015765184136112"
_$