defun add-all-3-5 optional max 1000 there are 1000 or about 333 multip

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(defun add-all-3-5 (&optional (max 1000))
; there are 1000/3 or about 333 multiple of 3
; and 200 multiple of 5
; so, use an upper limit of 334
(let ((limit 334)
(add5 0)
(add3 0)
(mult (make-hash-table))
(sum 0)) ; where we accumulate the sum
(dotimes (i limit sum)
(setf add5 (* i 5))
(setf add3 (* i 3))
(unless (gethash add5 mult)
(setf (gethash add5 mult) t)
(when (< add5 max)
(incf sum add5)))
(unless (gethash add3 mult)
(setf (gethash add3 mult) t)
(when (< add3 max)
(incf sum add3))))))
(time (add-all-3-5 1000))
Timing the evaluation of (ADD-ALL-3-5 1000)
Elapsed time = 0:00:01
233168