-
-
Save orpheus/e803cb8fb90f925fda90b07576273741 to your computer and use it in GitHub Desktop.
clojure functions to find the highest prime factorial of a number
This file contains 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
; returns the biggest factor for num x, or 1 if it's prime | |
(defn getFactor | |
([x] | |
; if x divided by 2 is not greater or equal to 2, return nil | |
; this handles x = 1, 2, or 3 which are primes (kinda) | |
(if (>= (/ x 2) 2) | |
; if divisible by 2, return the quotient | |
(if (== (mod x 2) 0) | |
(/ x 2) | |
; else increase 2 by 1 and recurse | |
(getFactor x (+ 2 1))))) | |
([x n] | |
; if the remainder of x/n is 0, return x/n, else increase n and recurse | |
(if (== (mod x n) 0) (/ x n) (getFactor x (+ n 1))))) | |
(defn highest-prime-factorial [x] | |
(println x) | |
(let [f (getFactor x)] | |
(cond | |
(nil? f ) x ; if nil, return x | |
(== f 1) x ; if highest factorial is 1, then found the highest prime factorial | |
:else (highest-prime-factorial f)))) ; if not prime, check the next factorial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment