%mathpiper,def="DivisorsList"

/* Implementation of some number theoretical functions for MathPiper */
/* (C) 2002 Pablo De Napoli <pdenapo@yahoo.com> under GNU GPL */

/* DivisorsList(n) = the list of divisors of n */

DivisorsList(n_IsPositiveInteger) <--
[
 Local(nFactors,f,result,oldresult,x);
 nFactors:= Factors(n);
 result := {1};
 ForEach (f,nFactors)
    [
      oldresult := result;
      For (k:=1,k<=f[2],k++)
        ForEach (x,oldresult)
	  result:=Append(result,x*f[1]^k);
    ];
  result;
];

%/mathpiper



%mathpiper_docs,name="DivisorsList",categories="User Functions;Number Theory"
*CMD DivisorsList --- the list of divisors
*STD
*CALL
	DivisorsList(n)
*PARMS

{n} -- positive integer

*DESC

{DivisorsList} creates a list of the divisors of $n$.
This is useful for loops like

	ForEach(d,DivisorsList(n))

*E.G.

In> DivisorsList(18)
Result: {1,2,3,6,9,18};

*SEE DivisorsSum
%/mathpiper_docs