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

476 Views Asked by At

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

There are 1 best solutions below

2
On

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