Product of divisors

From Rosetta Code
Revision as of 15:56, 20 December 2020 by Blek (talk | contribs) (Python added)
Product of divisors is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a positive integer, return the product of its positive divisors.

Task

Show the result for the first 50 positive integers.



Python

Finding divisors efficiently

<lang Python>def product_of_divisors(n):

   assert(isinstance(n, int) and 0 < n)
   ans = i = j = 1
   while i*i <= n:
       if 0 == n%i:
           ans *= i
           j = n//i
           if j != i:
               ans *= j
       i += 1
   return ans
   

if __name__ == "__main__":

   print([product_of_divisors(n) for n in range(1,51)])</lang>
Output:
[1, 2, 3, 8, 5, 36, 7, 64, 27, 100, 11, 1728, 13, 196, 225, 1024, 17, 5832, 19, 8000, 441, 484, 23, 331776, 125, 676, 729, 21952, 29, 810000, 31, 32768, 1089, 1156, 1225, 10077696, 37, 1444, 1521, 2560000, 41, 3111696, 43, 85184, 91125, 2116, 47, 254803968, 343, 125000]