99道lisp练习题----(二)数学

本文介绍了99道Lisp编程练习中的数学题目,提供了相关链接供读者参考更多解法,主要涉及Arithmetic领域的挑战。
摘要由CSDN通过智能技术生成

其他解法请参考: http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html

Arithmetic

P31 (**) Determine whether a given integer number is prime. (判断一个数是不是
Example:
* (is-prime 7)

T

(defun is-prime (num)
  (let ((n (sqrt num)))
    (labels ((rec (i)
	       (if (> i n)
		   t
		   (and (not (zerop (mod num i)))
			(rec (1+ i))))))
      (rec 2))))

P32 (**) Determine the greatest common divisor of two positive integer numbers.
Use Euclid's algorithm.
Example:
* (gcd 36 63)

9

(defun my-gcd (n1 n2)
  (if (zerop n2)
      n1
      (my-gcd n2 (mod n1 n2))))

P33 (*) Determine whether two positive integer numbers are coprime.
Two numbers are coprime if their greatest common divisor equals 1.
Example:
* (coprime 35 64)

T

(defun coprime (n1 n2)
  (= (my-gcd n1 n2)
     1))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值