Search found 6 matches

by anomaly
Tue Mar 03, 2009 7:37 am
Forum: Common Lisp
Topic: Newb: help with factoring function
Replies: 15
Views: 22238

Re: Newb: help with factoring function

Can you find a replacement for this line: (current-num num (/ num (apply #'* factors))) which doesn't need to multiply all factors found so far? (current-num num (/ current-num (first factors))) How's that? It works fine, but I'm still having the previously mentioned problem. Edit: This one is usin...
by anomaly
Mon Mar 02, 2009 4:19 pm
Forum: Common Lisp
Topic: Newb: help with factoring function
Replies: 15
Views: 22238

Re: Newb: help with factoring function

(defun primep (number) (when (> number 1) (loop for fac from 2 to (isqrt number) never (zerop (mod number fac))))) (defun next-prime (number) (loop for n from (+ number 1) when (primep n) return n)) (defun factor (num) (do* ((factors nil (cons factor factors)) (current-num num (/ num (apply #'* fac...
by anomaly
Sun Mar 01, 2009 2:22 pm
Forum: Common Lisp
Topic: Newb: help with factoring function
Replies: 15
Views: 22238

Re: Newb: help with factoring function

Okay, I have a working LOOP version, so I decided to see if I could translate it into one based on DO. LOOP factor: (defun factor (num) (loop for factors = nil then (cons factor factors) for current-num = (/ num (apply #'* factors)) for factor = (unless (= current-num 1) (loop for x from 2 until (ze...
by anomaly
Sun Mar 01, 2009 6:29 am
Forum: Common Lisp
Topic: Newb: help with factoring function
Replies: 15
Views: 22238

Re: Newb: help with factoring function

I thank you for your patience. When I tried to run your example it told me: WARNING: LOOP: FOR clauses should occur before the loop's main body I changed it slightly (superficially?) to this: (defun factor (num) (loop for factors = '(1) then (cons remainder factors) for current-num = num then (/ num...
by anomaly
Sun Mar 01, 2009 5:15 am
Forum: Common Lisp
Topic: Newb: help with factoring function
Replies: 15
Views: 22238

Re: Newb: help with factoring function

How much better is this? (defun factor-loop (num) (loop with factors = '(1) with remainder = nil with current-num = (/ num (apply #'* factors)) then (/ num (apply #'* factors)) do (loop with x = 0 while (or (= (rem num x) 0) (<= x (/ num 2))) finally ((setf remainder (rem num x))(return))) until (= ...
by anomaly
Sun Mar 01, 2009 1:40 am
Forum: Common Lisp
Topic: Newb: help with factoring function
Replies: 15
Views: 22238

Newb: help with factoring function

Okay, so I'm pretty new to lisp, and in the middle of reading Practical Common Lisp, I decide to test my knowledge of loop by using it to write a prime factoring function. I realize I don't know enough, but now I'm sucked into the problem and keep looking up specifics trying to figure it out. I don'...