如何显示前N个自然数,知道Lisp中的除数 [英] How to display first N natural numbers, knowing the divisors in Lisp

查看:154
本文介绍了如何显示前N个自然数,知道Lisp中的除数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显示第一个 N 个自然数,其除数仅为2、3和7. 我写了类似的东西.我是Lisp的初学者.谢谢!

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

推荐答案

由于我刚刚花了一些时间,所以可以看看一下.可能不是完美的解决方案,但对于初学者来说应该是一个很好的起点.在信息"标签中查看书籍,以了解语法等.

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

这篇关于如何显示前N个自然数,知道Lisp中的除数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆