-2

I just started playing around with Common Lisp and tried the first euler problem which states to sum all the divisors of 3 OR 5 under some given parameter x. I would like to use a recursive solution but I can figure out why this won't work

(defun my-sum (x)
  (if (< x 1)
      0
      (if (or (equal (div-3 x) 0) (equal (div-5 x) 0))
          (+ x (my-sum (- x 1)))
          (my-sum (- x 1)))))
New contributor
Patrick Donegan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • 1
    Are you sure div-3 and div-5 do what you want? Please add their definitions so we can run the code. Commented 19 hours ago
  • 1
    "divisors" should be "multiples". Commented 19 hours ago
  • 2
    When I define those functions, I get the correct answer: (my-sum 9) => 23 Commented 19 hours ago
  • 2
    Remember that "under x" means it shouldn't include x. So for the example at projecteuler.net for numbers under 10, you should call (my-sum 9). If you call (my-sum 10) you'll get 33 because 10 is a multiple of 5. Commented 19 hours ago
  • 1
    Barmar you are correct! I was calling (my-sum 10) instead of (my-sum 9). silly mistake on my part! thank you very much! Commented 19 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.