How the above implementation of RelativePrimes works

Lets consider what it does to compute RelativePrimes[42].

Part[FactorInteger[42],All,1] returns a list of prime factors of (42) which  is {2.3.7}.
Then the pure function (Range[#,41,#]&) is mapped over the list  {2, 3, 7}. This is computed as (lst1) in the next cell.. The #& notation is  explained in the discussion of Slot and SlotSequence.

lst1 = Range[#, 41, #] &/@{2, 3, 7}

{{2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40}, {3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39}, {7, 14, 21, 28, 35}}


Then (lst2) is simply a list of integers from 2 to 41 which is the output of
the next cell.

lst2 = Range[2, 41]

{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41}


Then the list of relative divisors is returned by effectively evaluating the
next cell.

Fold[Complement[#, #2] &, lst2, lst1]

{5, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41}


What Fold does in the previous cell is explicitly spelled out in the next
cell.

Complement[Complement[Complement[lst2, Part[lst1, 1]], Part[lst1, 2], Part[lst1, 3]]]

{5, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41}


This implementation is elegant and runs efficiently in Mathematica, but I
think the code is much more difficult to understand, so it's harder to
maintain.


Created by Mathematica  (May 17, 2004)