# Product of factors of number

Given a number n, find the product of all factors of n. Since the product can be very large answer it modulo 10^9 + 7.

Examples :

```Input : 12
Output : 1728
1 * 2 * 3 * 4 * 6 * 12 = 1728

Input : 18
Output : 5832
1 * 2 * 3 * 6 * 9 * 18 = 5832
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1(Naive Approach):
We can run loop for i from 1 to n and if n is divisible by i multiply the numbers. Time complexity for this solution will be O(n).

But this approach is insufficient for large value of n.

Method 2(Better Approach):
A better approach is to run loop for i from 1 to sqrt(n). If number is divisible by i multiply it with i and n/i.
Time complexity of this solution will be O(sqrt(n)).

## C++

 `// C++ program to calculate product ` `// of factors of number ` `#include ` `#define M 1000000007 ` `using` `namespace` `std; ` ` `  `// function to product the factors ` `long` `long` `multiplyFactors(``int` `n) ` `{ ` `    ``long` `long` `prod = 1; ` `    ``for` `(``int` `i = 1; i * i <= n; i++)  ` `    ``{ ` `        ``if` `(n % i == 0)  ` `        ``{ ` `            ``// If factors are equal, ` `            ``// multiply only once ` `            ``if` `(n / i == i) ` `                ``prod = (prod * i) % M; ` ` `  `            ``// Otherwise multiply both ` `            ``else` `{ ` `                ``prod = (prod * i) % M; ` `                ``prod = (prod * n / i) % M; ` `            ``} ` `        ``} ` `    ``} ` `    ``return` `prod; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 12; ` ` `  `    ``cout << multiplyFactors(n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to calculate product ` `// of factors of number ` `class` `GFG  ` `{ ` `public` `static` `final` `long` `M = ``1000000007``; ` ` `  `    ``// function to product the factors ` `    ``static` `long` `multiplyFactors(``int` `n) ` `    ``{ ` `        ``long` `prod = ``1``; ` `        ``for` `(``int` `i = ``1``; i * i <= n; i++)  ` `        ``{ ` `            ``if` `(n % i == ``0``) ` `            ``{ ` ` `  `                ``// If factors are equal, ` `                ``// multiply only once ` `                ``if` `(n / i == i) ` `                    ``prod = (prod * i) % M; ` ` `  `                ``// Otherwise multiply both ` `                ``else` `{ ` `                    ``prod = (prod * i) % M; ` `                    ``prod = (prod * n / i) % M; ` `                ``} ` `            ``} ` `        ``} ` `        ``return` `prod; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``12``; ` `        ``System.out.println(multiplyFactors(n)); ` `    ``} ` `} `

## Python

 `# Python program to calculate product ` `# of factors of number ` ` `  `M ``=` `1000000007` ` `  `# function to product the factors ` `def` `multiplyFactors(n) : ` `    ``prod ``=` `1` `     `  `    ``i ``=` `1` `    ``while` `i ``*` `i <``=` `n : ` `        ``if` `(n ``%` `i ``=``=` `0``) : ` `             `  `            ``# If factors are equal, ` `            ``# multiply only once ` `            ``if` `(n ``/` `i ``=``=` `i) : ` `                ``prod ``=` `(prod ``*` `i) ``%` `M ` `                 `  `            ``#Otherwise multiply both ` `            ``else` `: ` `                ``prod ``=` `(prod ``*` `i) ``%` `M ` `                ``prod ``=` `(prod ``*` `n ``/` `i) ``%` `M ` `        ``i ``=` `i ``+` `1` ` `  `    ``return` `prod ` ` `  `# Driver Code ` ` `  `n ``=` `12` `print` `(multiplyFactors(n)) ` ` `  ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `//C# program to calculate product ` `// of factors of number ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `public` `static` `long` `M = 1000000007; ` ` `  `    ``// function to product the factors ` `    ``static` `long` `multiplyFactors(``int` `n) ` `    ``{ ` `        ``long` `prod = 1; ` `        ``for` `(``int` `i = 1; i * i <= n; i++)  ` `        ``{ ` `            ``if` `(n % i == 0)  ` `            ``{ ` ` `  `                ``// If factors are equal, ` `                ``// multiply only once ` `                ``if` `(n / i == i) ` `                    ``prod = (prod * i) % M; ` ` `  `                ``// Otherwise multiply both ` `                ``else` `{ ` `                    ``prod = (prod * i) % M; ` `                    ``prod = (prod * n / i) % M; ` `                ``} ` `            ``} ` `        ``} ` `        ``return` `prod; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 12; ` `        ``Console.Write(multiplyFactors(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal. `

## PHP

 ` `

Output :

```1728
```

Method 3 (Another Approach):
Let us observe a thing:

```All factors of 12 are: 1, 2, 3, 4, 6, 12
1 * 2 * 3 * (2*2) * (2*3) * (2*2*3) = 2^6 * 3^3 = 12^3
and number of factors are 6
```

So we can observe that product of factors will be n^(number of factor/2). But when number of factor is odd (which means the number is perfect square) in that case product will be n^(number of factor/2) * sqrt(n). We can count number of factors similar to approach above. And we can calculate power efficiently using Modular Exponentiation

## C++

 `// C++ program to calculate product ` `// of factors of number ` `#include ` `#define M 1000000007 ` `using` `namespace` `std; ` ` `  `// Iterative Function to calculate ` `// (x^y) in O(log y) ` `long` `long` `power(``long` `long` `x, ``long` `long` `y) ` `{ ` `    ``long` `long` `res = 1; ` ` `  `    ``while` `(y > 0)  ` `    ``{ ` `        ``if` `(y & 1) ` `            ``res = (res * x) % M; ` `        ``y = (y >> 1) % M; ` `        ``x = (x * x) % M; ` `    ``} ` `    ``return` `res; ` `} ` ` `  `// function to count the factors ` `int` `countFactors(``int` `n) ` `{ ` `    ``int` `count = 0; ` `    ``for` `(``int` `i = 1; i * i <= n; i++)  ` `    ``{ ` `        ``if` `(n % i == 0)  ` `        ``{ ` ` `  `            ``// If factors are equal, ` `            ``// count only once ` `            ``if` `(n / i == i) ` `                ``count++; ` ` `  `            ``// Otherwise count both ` `            ``else` `                ``count += 2; ` `        ``} ` `    ``} ` `    ``return` `count; ` `} ` ` `  `long` `long` `multiplyFactors(``int` `n) ` `{ ` `    ``int` `numFactor = countFactors(n); ` ` `  `    ``// Calculate product of factors ` `    ``long` `long` `product = power(n, numFactor / 2); ` ` `  `    ``// If n is odd return ` `    ``// power(n, numFactor/2) * sqrt(n) ` `    ``if` `(n & 1) ` `        ``product = (product * (``int``)``sqrt``(n)) % M; ` ` `  `    ``return` `product; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 12; ` `    ``cout << multiplyFactors(n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to calculate product ` `// of factors of number ` `class` `GFG ` `{ ` `public` `static` `final` `long` `M = ``1000000007``; ` ` `  `    ``// Iterative Function to calculate ` `    ``// (x^y) in O(log y) ` `    ``static` `long` `power(``long` `x, ``long` `y) ` `    ``{ ` `        ``long` `res = ``1``; ` ` `  `        ``while` `(y > ``0``)  ` `        ``{ ` `            ``if` `(y % ``2` `== ``1``) ` `                ``res = (res * x) % M; ` `            ``y = (y >> ``1``) % M; ` `            ``x = (x * x) % M; ` `        ``} ` `        ``return` `res; ` `    ``} ` ` `  `    ``// function to count the factors ` `    ``static` `int` `countFactors(``int` `n) ` `    ``{ ` `        ``int` `count = ``0``; ` `        ``for` `(``int` `i = ``1``; i * i <= n; i++)  ` `        ``{ ` `            ``if` `(n % i == ``0``)  ` `            ``{ ` ` `  `                ``// If factors are equal, ` `                ``// count only once ` `                ``if` `(n / i == i) ` `                    ``count++; ` ` `  `                ``// Otherwise count both ` `                ``else` `                    ``count += ``2``; ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` ` `  `    ``static` `long` `multiplyFactors(``int` `n) ` `    ``{ ` `        ``int` `numFactor = countFactors(n); ` ` `  `        ``// Calculate product of factors ` `        ``long` `product = power(n, numFactor / ``2``); ` ` `  `        ``// If n is odd return ` `        ``// power(n, numFactor/2) * sqrt(n) ` `        ``if` `(n % ``2` `== ``1``) ` `            ``product = (product * (``int``)Math.sqrt(n)) % M; ` ` `  `        ``return` `product; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``12``; ` `        ``System.out.println(multiplyFactors(n)); ` `    ``} ` `} `

## Python

 `# Python program to calculate product ` `# of factors of number ` ` `  `M ``=` `1000000007` ` `  `# Iterative Function to calculate ` `# (x^y) in O(log y) ` `def` `power(x, y) : ` `     `  `    ``res ``=` `1` `    ``while` `(y > ``0``) : ` `         `  `        ``if` `(y ``%` `2` `=``=` `1``) : ` `            ``res ``=` `(res ``*` `x) ``%` `M ` `        ``y ``=` `(y >> ``1``) ``%` `M ` `        ``x ``=` `(x ``*` `x) ``%` `M ` `         `  `    ``return` `res ` `     `  `# function to count the factors ` `def` `countFactors(n) : ` `    ``count ``=` `0` `    ``i ``=` `1` `    ``while` `i ``*` `i <``=` `n : ` `        ``if` `(n ``%` `i ``=``=` `0``) : ` `            ``# If factors are equal, ` `            ``# count only once ` `            ``if` `(n ``/` `i ``=``=` `i) : ` `                ``count ``=` `count ``+` `1` `             `  `            ``# Otherwise count both ` `            ``else` `: ` `                ``count ``=` `count ``+` `2` `        ``i ``=` `i ``+` `1` `    ``return` `count ` `     `  `def` `multiplyFactors(n) : ` `     `  `    ``numFactor ``=` `countFactors(n) ` `     `  `    ``# Calculate product of factors ` `    ``product ``=` `power(n, numFactor ``/` `2``) ` ` `  `    ``# If n is odd return ` `    ``# power(n, numFactor/2) * sqrt(n) ` `    ``if` `(n ``%` `2` `=``=` `1``) : ` `        ``product ``=` `(product ``*` `                ``(``int``)(math.sqrt(n))) ``%` `M ` ` `  `    ``return` `product ` `     `  `# Driver Code ` `n ``=` `12` `print` `multiplyFactors(n) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# program to calculate product ` `// of factors of number ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``public` `static` `long` `M = 1000000007; ` ` `  `    ``// Iterative Function to calculate ` `    ``// (x^y) in O(log y) ` `    ``static` `long` `power(``long` `x, ``long` `y) ` `    ``{ ` `        ``long` `res = 1; ` ` `  `        ``while` `(y > 0)  ` `        ``{ ` `            ``if` `(y % 2 == 1) ` `                ``res = (res * x) % M; ` `            ``y = (y >> 1) % M; ` `            ``x = (x * x) % M; ` `        ``} ` `        ``return` `res; ` `    ``} ` ` `  `    ``// function to count the factors ` `    ``static` `int` `countFactors(``int` `n) ` `    ``{ ` `        ``int` `count = 0; ` `        ``for` `(``int` `i = 1; i * i <= n; i++)  ` `        ``{ ` `            ``if` `(n % i == 0)  ` `            ``{ ` ` `  `                ``// If factors are equal, ` `                ``// count only once ` `                ``if` `(n / i == i) ` `                    ``count++; ` ` `  `                ``// Otherwise count both ` `                ``else` `                    ``count += 2; ` `            ``} ` `        ``} ` `         `  `        ``return` `count; ` `    ``} ` ` `  `    ``static` `long` `multiplyFactors(``int` `n) ` `    ``{ ` `        ``int` `numFactor = countFactors(n); ` ` `  `        ``// Calculate product of factors ` `        ``long` `product = power(n, numFactor / 2); ` ` `  `        ``// If n is odd return ` `        ``// power(n, numFactor/2) * sqrt(n) ` `        ``if` `(n % 2 == 1) ` `            ``product = (product *  ` `                       ``(``int``)Math.Sqrt(n)) % M; ` ` `  `        ``return` `product; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 12; ` `        ``Console.Write(multiplyFactors(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal `

## PHP

 ` 0)  ` `    ``{ ` `        ``if` `(``\$y` `& 1) ` `            ``\$res` `= (``\$res` `* ``\$x``) % ``\$M``; ` `        ``\$y` `= (``\$y` `>> 1) % ``\$M``; ` `        ``\$x` `= (``\$x` `*``\$x``) % ``\$M``; ` `    ``} ` `    ``return` `\$res``; ` `} ` ` `  `// function to count the factors ` `function` `countFactors( ``\$n``) ` `{ ` `    ``\$count` `= 0; ` `    ``for` `(``\$i` `= 1; ``\$i` `* ``\$i` `<= ``\$n``; ``\$i``++)  ` `    ``{ ` `        ``if` `(``\$n` `% ``\$i` `== 0)  ` `        ``{ ` ` `  `            ``// If factors are equal, ` `            ``// count only once ` `            ``if` `(``\$n` `/ ``\$i` `== ``\$i``) ` `                ``\$count``++; ` ` `  `            ``// Otherwise count both ` `            ``else` `                ``\$count` `+= 2; ` `        ``} ` `    ``} ` `    ``return` `\$count``; ` `} ` ` `  `function` `multiplyFactors( ``\$n``) ` `{ ` `    ``\$numFactor` `= countFactors(``\$n``); ` ` `  `    ``// Calculate product of factors ` `    ``\$product` `= power(``\$n``, ``\$numFactor` `/ 2); ` ` `  `    ``// If n is odd return ` `    ``// power(n, numFactor/2) * sqrt(n) ` `    ``if` `(``\$n` `& 1) ` `        ``\$product` `= (``\$product` `* sqrt(``\$n``)) % ``\$M``; ` ` `  `    ``return` `\$product``; ` `} ` ` `  `    ``// Driver code ` `    ``\$n` `= 12; ` `    ``echo` `multiplyFactors(``\$n``); ` `     `  `// This code is contributed by anuj_67. ` `?> `

Output :

```1728
```

This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : nitin mittal, vt_m

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.