Given an integer **N**, the task is to find the number of factors of **N** which are a perfect square.

**Examples:**

Input:N = 100Output:4Explanation:

There are four factors of

100 (1, 4, 25, 100) that are perfect square.Input:N = 900Output:8Explanation:

There are eight factors of 900 (1, 4, 9, 25, 36, 100, 225, 900) that are perfect square.

**Naive Approach:** The simplest approach to solve this problem is to find all possible factors of the given number **N** and for each factor, check if the factor is a perfect square or not. For every factor found to be so, increase **count**. Print the final **count**. **Time Complexity:** O(N) **Auxiliary Space:** O(1)**Efficient Approach:**

The following observations need to be made to optimize the above approach:

The number of factors for a number is given by:

Factors of N = (1 + a

_{1})*(1 + a_{2})*(1 + a_{3})*..*(1 + a_{n})

where a_{1}, a_{2}, a_{3}, …, a_{n}are the count of distinct prime factors of N.

In a perfect square, the count of distinct prime factors must be divisible by 2. Therefore, the count of factors that are a perfect square is given by:

Factors of N that are perfect square = (1 + a

_{1}/2)*(1 + a_{2}/2)*…*(1 + a_{n}/2) where a_{1}, a_{2}, a_{3}, …, a_{n}are the count of distinct prime factors of N.

**Illustration:**

The prime factors of N = 100 are 2, 2, 5, 5.

Therefore, the number of factors that are perfect square are (1 + 2/2) * (1 + 2/2) = 4.

The factors are 1, 4, 25, 100.

Therefore, find the count of prime factors and apply the above formula to find the count of factors that are a perfect square.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function that returns the count of` `// factors that are perfect squares` `int` `noOfFactors(` `int` `N)` `{` ` ` `if` `(N == 1)` ` ` `return` `1;` ` ` `// Stores the count of number` ` ` `// of times a prime number` ` ` `// divides N.` ` ` `int` `count = 0;` ` ` `// Stores the number of factors` ` ` `// that are perfect square` ` ` `int` `ans = 1;` ` ` `// Count number of 2's` ` ` `// that divides N` ` ` `while` `(N % 2 == 0) {` ` ` `count++;` ` ` `N = N / 2;` ` ` `}` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / 2 + 1);` ` ` `// Check for all the possible` ` ` `// numbers that can divide it` ` ` `for` `(` `int` `i = 3;` ` ` `i * i <= N; i = i + 2) {` ` ` `count = 0;` ` ` `// Check the number of` ` ` `// times prime number` ` ` `// i divides it` ` ` `while` `(N % i == 0) {` ` ` `count++;` ` ` `N = N / i;` ` ` `}` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / 2 + 1);` ` ` `}` ` ` `// Return final count` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 100;` ` ` `cout << noOfFactors(N);` ` ` `return` `0;` `}` |

## Java

`// Java program to implement` `// the above approach` `import` `java.util.*;` `class` `GFG{` `// Function that returns the count of` `// factors that are perfect squares` `static` `int` `noOfFactors(` `int` `N)` `{` ` ` `if` `(N == ` `1` `)` ` ` `return` `1` `;` ` ` `// Stores the count of number` ` ` `// of times a prime number` ` ` `// divides N.` ` ` `int` `count = ` `0` `;` ` ` `// Stores the number of factors` ` ` `// that are perfect square` ` ` `int` `ans = ` `1` `;` ` ` `// Count number of 2's` ` ` `// that divides N` ` ` `while` `(N % ` `2` `== ` `0` `)` ` ` `{` ` ` `count++;` ` ` `N = N / ` `2` `;` ` ` `}` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / ` `2` `+ ` `1` `);` ` ` `// Check for all the possible` ` ` `// numbers that can divide it` ` ` `for` `(` `int` `i = ` `3` `; i * i <= N; i = i + ` `2` `)` ` ` `{` ` ` `count = ` `0` `;` ` ` `// Check the number of` ` ` `// times prime number` ` ` `// i divides it` ` ` `while` `(N % i == ` `0` `)` ` ` `{` ` ` `count++;` ` ` `N = N / i;` ` ` `}` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / ` `2` `+ ` `1` `);` ` ` `}` ` ` `// Return final count` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `100` `;` ` ` `System.out.print(noOfFactors(N));` `}` `}` `// This code is contributed by 29AjayKumar` |

## Python3

`# Python3 program to implement` `# the above approach` `# Function that returns the count of` `# factors that are perfect squares` `def` `noOfFactors(N):` ` ` `if` `(N ` `=` `=` `1` `):` ` ` `return` `1` ` ` `# Stores the count of number` ` ` `# of times a prime number` ` ` `# divides N.` ` ` `count ` `=` `0` ` ` `# Stores the number of factors` ` ` `# that are perfect square` ` ` `ans ` `=` `1` ` ` `# Count number of 2's` ` ` `# that divides N` ` ` `while` `(N ` `%` `2` `=` `=` `0` `):` ` ` `count ` `+` `=` `1` ` ` `N ` `=` `N ` `/` `/` `2` ` ` `# Calculate ans according` ` ` `# to above formula` ` ` `ans ` `*` `=` `(count ` `/` `/` `2` `+` `1` `)` ` ` `# Check for all the possible` ` ` `# numbers that can divide it` ` ` `i ` `=` `3` ` ` `while` `i ` `*` `i <` `=` `N:` ` ` `count ` `=` `0` ` ` `# Check the number of` ` ` `# times prime number` ` ` `# i divides it` ` ` `while` `(N ` `%` `i ` `=` `=` `0` `):` ` ` `count ` `+` `=` `1` ` ` `N ` `=` `N ` `/` `/` `i` ` ` `# Calculate ans according` ` ` `# to above formula` ` ` `ans ` `*` `=` `(count ` `/` `/` `2` `+` `1` `)` ` ` `i ` `+` `=` `2` ` ` ` ` `# Return final count` ` ` `return` `ans` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `N ` `=` `100` ` ` `print` `(noOfFactors(N))` `# This code is contributed by chitranayal` |

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Function that returns the count of` `// factors that are perfect squares` `static` `int` `noOfFactors(` `int` `N)` `{` ` ` `if` `(N == 1)` ` ` `return` `1;` ` ` `// Stores the count of number` ` ` `// of times a prime number` ` ` `// divides N.` ` ` `int` `count = 0;` ` ` `// Stores the number of factors` ` ` `// that are perfect square` ` ` `int` `ans = 1;` ` ` `// Count number of 2's` ` ` `// that divides N` ` ` `while` `(N % 2 == 0)` ` ` `{` ` ` `count++;` ` ` `N = N / 2;` ` ` `}` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / 2 + 1);` ` ` `// Check for all the possible` ` ` `// numbers that can divide it` ` ` `for` `(` `int` `i = 3; i * i <= N; i = i + 2)` ` ` `{` ` ` `count = 0;` ` ` `// Check the number of` ` ` `// times prime number` ` ` `// i divides it` ` ` `while` `(N % i == 0)` ` ` `{` ` ` `count++;` ` ` `N = N / i;` ` ` `}` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / 2 + 1);` ` ` `}` ` ` `// Return final count` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `N = 100;` ` ` `Console.Write(noOfFactors(N));` `}` `}` `// This code is contributed by PrinciRaj1992` |

## Javascript

`<script>` `// Javascript program for` `// the above approach` `// Function that returns the count of` `// factors that are perfect squares` `function` `noOfFactors(N)` `{` ` ` `if` `(N == 1)` ` ` `return` `1;` ` ` ` ` `// Stores the count of number` ` ` `// of times a prime number` ` ` `// divides N.` ` ` `let count = 0;` ` ` ` ` `// Stores the number of factors` ` ` `// that are perfect square` ` ` `let ans = 1;` ` ` ` ` `// Count number of 2's` ` ` `// that divides N` ` ` `while` `(N % 2 == 0)` ` ` `{` ` ` `count++;` ` ` `N = N / 2;` ` ` `}` ` ` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / 2 + 1);` ` ` ` ` `// Check for all the possible` ` ` `// numbers that can divide it` ` ` `for` `(let i = 3; i * i <= N; i = i + 2)` ` ` `{` ` ` `count = 0;` ` ` ` ` `// Check the number of` ` ` `// times prime number` ` ` `// i divides it` ` ` `while` `(N % i == 0)` ` ` `{` ` ` `count++;` ` ` `N = N / i;` ` ` `}` ` ` ` ` `// Calculate ans according` ` ` `// to above formula` ` ` `ans *= (count / 2 + 1);` ` ` `}` ` ` ` ` `// Return final count` ` ` `return` `ans;` `}` ` ` `// Driver Code` ` ` ` ` `let N = 100;` ` ` ` ` `document.write(noOfFactors(N));` `</script>` |

**Output:**

4

**Time Complexity: ****Space Complexity: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**