I'd like to take a number, 20, and a list. '(1 2 3 4 5 6 7 8 9 10)
, and return a collection containing two values for each value in the original list: the original value paired with the remainder when diving 20 by that value. It would be nice if the original values were somehow keyed to the remainders, so that I could easily retrieve each number that produced a particular remainder. Basically I want some function func
:
user=> (func 20 '(1 2 3 4 5 6 7 8 9 10))
'(:0 1, :0 2, :2 3,... :20 0)
I am however having an incredibly difficult time just figuring out how to iterate through the list. Could someone help me understand how to use the elements of a list independently, and then how to return the element that 20 was divided by and if it returns a remainder?
My thought was to use something like this in a program that calculates square roots. If the numbers were keyed by the remainder, then I could query the collection to get all numbers that divide the input with a remainder of 0.
Here was my preliminary way of going about that.
;; 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)