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

后端 未结 1 1815
自闭症患者
自闭症患者 2021-01-21 21:49

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!

def         


        
1条回答
  •  北恋
    北恋 (楼主)
    2021-01-21 22:32

    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)))
    

    0 讨论(0)
提交回复
热议问题