我想使用一个数字20和一个列表'(1 2 3 4 5 6 7 8 9 10)
,返回一个集合,其中包含原始列表中每个值的两个值:原始值与20除以该值时的余数。如果能将原始值与余数相关联,那就太好了,这样我就可以轻松地检索产生特定余数的每个数字。基本上,我需要一个名为func
的函数:
user=> (func 20 '(1 2 3 4 5 6 7 8 9 10))
'(:0 1, :0 2, :2 3,... :20 0)
我很难弄清如何遍历这个列表,尤其是独立使用列表中的元素以及如何返回20被哪个元素除以后的余数。请问有谁能帮帮我吗?
我的想法是在一个计算平方根的程序中使用类似这样的内容。如果数字按余数键入,那么我可以查询集合以获取所有将输入分为0余数的数字。
这是我最初的处理方式。
;; My idea on the best way to find a square root is simple.
;; If I want to find the square root of n, divide n in half
;; Then divide our initial number (n) by all numbers in the range 0...n/2
;; Separate out a list of results that only only return a remainder of 0.
;; Then test the results in a comparison to see if the elements of our returned
;; list when squared are equal with the number we want to find a square root of.
;; First I'll develop a function that works with evens and then odds
(defn sqroot-range-high-end [input] (/ input 2))
(sqroot-range-high-end 36) ; 18
(defn make-sqrt-range [input] (range (sqroot-range-high-end (+ 1 input))))
(make-sqrt-range 36) ; '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)
(defn zero-culler [input] (lazy-seq (remove zero? (make-sqrt-range input))))
(zero-culler 100) ; '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)
(defn odd-culler [input] (lazy-seq (remove odd? (zero-culler input))))
(odd-culler 100) ; '(2 4 6 8 10 12 14 16 18)
;;the following is where I got stuck
;;I'm new to clojure and programming,
;;and am just trying to learn in a way that I understand
(defn remainder-culler [input]
(if
(/ input (first odd-culler (input)))
input)
(recur (lazy-seq (input)))
)
(remainder-culler 100)