Skip to content

Instantly share code, notes, and snippets.

@orpheus
Created January 8, 2021 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orpheus/e803cb8fb90f925fda90b07576273741 to your computer and use it in GitHub Desktop.
Save orpheus/e803cb8fb90f925fda90b07576273741 to your computer and use it in GitHub Desktop.
clojure functions to find the highest prime factorial of a number
; 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