allFactors.frink


// This function finds all divisors of a number, including 1 and the number
// itself.

allFactors[n] :=
{
   factors = factor[n]
   size = length[factors]
   count = new array
   allfactors = new array
   for i = 0 to size-1
      count@i=0

   index = 1
   done = false
   do
   {
      for n = 0 to factors@0@1
      {
         count@0 = n
         
         product = 1
         for i = 0 to size-1
            product = product * (factors@i@0)^(count@i)
         allfactors.push[product]
      }
      
      while (index < size) and (count@index == factors@index@1)
      {
         count@index = 0
         index = index + 1
      }
      if (index < size)
         count@index = count@index + 1
      else
         done = true
      index = 1
   } while ! done

   return sort[allfactors]
}


View or download allFactors.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 14705 days, 19 hours, 59 minutes ago.