(defun factorial (n)
(loop with result = 1
for i from 1 to n do
(setf result (* result i))
finally (return result)))
If you use loop, it's better to start multiplying the biggest integers
first, in case it goes into bignums:
cl-user> (defun factorial> (n)
(loop with result = 1
for i from 1 to n do
(setf result (* result i))
finally (return result)))
factorial>
cl-user> (defun factorial< (n)
(loop with result = 1
for i from n downto 1 do
(setf result (* result i))
finally (return result)))
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 59 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 18:13:26 |
| Calls: | 810 |
| Calls today: | 1 |
| Files: | 1,287 |
| D/L today: |
10 files (21,017K bytes) |
| Messages: | 193,637 |