Created
May 16, 2013 21:44
-
-
Save linuxsable/5595346 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn divides? | |
"Does divisor divide dividend evenly?" | |
[dividend divisor] | |
(zero? (rem dividend divisor))) | |
(defn factors | |
"Returns a sequence of all factors of p." | |
[p] | |
(filter #(divides? p %) (range 2 p))) | |
(defn prime? | |
"Returns true if p is prime, false otherwise." | |
[p] | |
(empty? (factors p))) | |
(defn medians | |
"Returns the two middle numbers of list." | |
[lis] | |
(if (empty? lis) | |
'() | |
(list (nth lis (/ (- (count lis) 1) 2)) | |
(nth lis (/ (count lis) 2))))) | |
(defn check-answer | |
"Checks that the two factors equal the number." | |
[fac1 fac2 num] | |
(= (* fac1 fac2) num)) | |
(defn squarest-factors | |
"Pass num and find the most square factors." | |
[num] | |
(if (prime? num) | |
(printf "1 * %d = %d \n" num num) | |
(let [nums (medians (factors num))] | |
(if (check-answer (first nums) (last nums) num) | |
(printf "%d * %d = %d \n" (first nums) (last nums) num) | |
(print "FAIL"))))) | |
; Run it | |
(time (squarest-factors 8276345)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment