编写一个函数,该函数吃掉一个 Nat 并返回一个 (列表 Nat),其中包含 1 到 n 之间的数字,这些数字正好能被



编写一个函数 (fff n(,该函数使用 Nat 并返回一个 (Nat( 列表,其中包含 1 到 n 之间的所有数字,这些数字正好可以被 2、3 和 5 中的一个整除。

当 n = 10 时,可被这些值中的至少一个整除的数字是 {2,3,4,5,6,8,9,10}。

但是 6 和 10 可以被其中两个数字整除。所以 (fff 10( => (列表 2 3 4 5 8 9(

到目前为止,我所拥有的是

;; q2
;;(divisible? n d) returns true if no remainder exists when d is divided by n
;;divisible?: Nat-> Num
;;Examples
(check-expect (divisible? 8 4) #true )
(define (divisible? n d) (= 0 (remainder n d)))
;;
(define (multiple-235? n)
  (cond
   ((divisible? n 2) #true)
   ((divisible? n 3) #true)
   ((divisible? n 5) #true)))

我不确定如何设置要检查的 1 到 n 的范围,我也不确定如何过滤列表以仅包含可被 2、3 或 5 整除的值。如何将其设置为只能被其中一个整除。

只需使用过滤器并使用PetSerAl的方法(或使用他的解决方案(修复您的multiple-235?

(define (multiple-235? n)
  (= 1 (count (λ (d) (divisible? n d))
              '(2 3 5))))
(define (fff n)
  (filter multiple-235? (range 1 (add1 n))))

最新更新