Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Given the following problem:

;3.3 Tabulate the function k = 2^n for n = 1..50. 
;Do this for the fewest possible multiplications.[3]

I wrote this answer:

(defun k (n) (ash 2 (1- n)))

(loop for n from 1 to 50 do
      (format t "k(~a) = ~a ~%" n (k n)))

What do you think?

EDIT: based on feedback received, (ash 2 (1- n)) has been simplified to (ash 1 n):

(defun k (n) (ash 1 n))

(loop for n from 1 to 50 do
      (format t "k(~a) = ~a ~%" n (k n)))
share|improve this question
What does ash do? – Omnifarious Mar 22 '11 at 17:57

1 Answer

up vote 0 down vote accepted

If you use 1 instead of 2 as the number to shift, you won't have to subtract one from n. I.e. you can just define k as (ash 1 n).

Other than that your code looks fine.

share|improve this answer
Good point! Thanks. – jaresty Mar 23 '11 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.