Skip to content
Browse files

Inline has-prime-factor?

  • Loading branch information...
1 parent b70d21c commit ff9c6796dc39b0fafdfe5784ace4dc62db71b5f7 @sattvik committed Feb 27, 2012
Showing with 11 additions and 12 deletions.
  1. +11 −12 src/clojure/primes/clojure/lazy_inline.clj
View
23 src/clojure/primes/clojure/lazy_inline.clj
@@ -11,19 +11,18 @@
[n d]
`(zero? (rem ~n ~d)))
-(defn has-prime-factor?
+(definline has-prime-factor?
"Returns true if n has a factor in primes."
- [^long n ^longs primes]
- (let [c (aget primes 0)
- sqrtn (long (Math/sqrt (double n)))]
- (loop [i 1]
- (if (= i c)
- false
- (let [p (aget primes i)]
- (cond
- (> p sqrtn) false
- (divides? n p) true
- :else (recur (inc i))))))))
+ [n ^longs primes]
+ `(let [c# (aget ~primes 0)
+ sqrtn# (long (Math/sqrt (double ~n)))]
+ (loop [i# 1]
+ (when-not (= i# c#)
+ (let [p# (aget ~primes i#)]
+ (cond
+ (> p# sqrtn#) false
+ (divides? ~n p#) true
+ :else (recur (inc i#))))))))
(definline next-prime
"Given n and a list of prime factors, return the smallest number greater than

0 comments on commit ff9c679

Please sign in to comment.
Something went wrong with that request. Please try again.