How to display first N natural numbers, knowing the divisors in Lisp

点点圈 提交于 2019-12-31 02:54:44

问题


Display first N natural numbers, the divisors of which are only 2, 3 and 7. I wrote something like that. I am a beginner in Lisp. Thank you!

defvar x 1
(defun numbers(n)
    if(mod x 2 )
    (loop for x from 1 to n
    do(print x)
    )
)
print(numbers())

回答1:


Because I just had some time, you could have a look at this. Might not be the perfect solution but should be a good starting point for a beginner. Check out the books in the info tab to get into the syntax etc.

(defun divisible-by (n m)
   "Returns T if N is evenly divisible by M."
   (zerop (mod n m)))

(defun numbers (n)
   "Print all number upto N which are divisible by 2, 3 and 7."
    (loop
       for i from 1 upto N
       if (and (divisible-by i 2) (divisible-by i 3) (divisible-by i 7))
         do (format t "~D~%" i)))


来源:https://stackoverflow.com/questions/58246348/how-to-display-first-n-natural-numbers-knowing-the-divisors-in-lisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!