[Cs254f11] Prime Q

Lee Spector lspector at hampshire.edu
Mon Sep 26 18:08:34 EDT 2011


Hi Jamie,

Your factorsB does all of the necessary work to get the factors. The filter function traverses the list of numbers produced by (range 1 (+ n 1)) and calls  #(integer? (/ n %)) on each of them, returning those for which this returns true. So it gives you the numbers that produce an integer when divided into n, which are the factors of n. So you don't need to map anything.

In your factorsA you take the same list and then map #(/ n %) down it. This divides each n by each factor and returns a list of those results. Of course, they're all factors of n so they all divide n evenly, giving you the other factors that you get from these divisions, as in:

user=> (factorsB 50)
(1 2 5 10 25 50)

user=> (factorsA 50)
(50 25 10 5 2 1)

The 50 here is because (/ 50 1) is 50, and the 25 is because (/ 50 2) is 25, etc.

Make sense?

 -Lee




On Sep 26, 2011, at 3:59 PM, James Matheson wrote:

> So here I have two (working) functions defining the factors (labeled
> factorsA and factorsB) which seem to work exactly the same. However I
> don't map factorsB. Can someone explain to me how map is working here?
> 
> (defn factorsB [n]
>  (filter #(integer? (/ n %)) (range 1 (+ n 1))))
> 
> (defn factorsA [n]
>  (map #(/ n %) (filter #(integer? (/ n %)) (range 1 (+ n 1)))))
> 
> (defn prime? [n]
>  (or (= n 2) (= 2 (count (take 3 (factorsB n))))))
> 
> (defn prime-factors [n]
>  (filter prime? (factorsB n)))
> 
> --Jamie
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list