This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 14th, 2012  |  syntax: None  |  size: 0.88 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (fact (primefactors 1) => (list))
  2.  
  3. (fact (primefactors 2) => (list 2))
  4.  
  5. (fact (primefactors 3) => (list 3))
  6.  
  7. (fact (primefactors 4) => (list 2 2))
  8.        
  9. (defn primefactors
  10.     ([n] (primefactors n 2))
  11.     ([n candidate]
  12.         (cond (<= n 1) (list)
  13.               (= 0 (rem n candidate)) (conj (primefactors (/ n candidate)) candidate)
  14.               :else (primefactors n (inc candidate))
  15.         )
  16.     )
  17. )
  18.        
  19. (fact (primefactors 1000001) => (list 101 9901))
  20.        
  21. (defn primefactors
  22.   ([n]
  23.     (primefactors n 2 '()))
  24.   ([n candidate acc]
  25.     (cond (<= n 1) (reverse acc)
  26.           (zero? (rem n candidate)) (recur (/ n candidate) candidate (cons candidate acc))
  27.           :else (recur n (inc candidate) acc))))
  28.        
  29. (defn primefactors
  30.   ([n] (primefactors n 2 '()))
  31.   ([n candidate acc]
  32.     ...)
  33.        
  34. (primefactors n (inc candidate))
  35.        
  36. (recur n (inc candidate))
  37.        
  38. (primefactors (/ n candidate))
clone this paste RAW Paste Data