# Largest factor of a given number which is a perfect square

Given a number . The task is to find the largest factor of that number which is a perfect square.
Examples

`Input : N = 420Output : 4Input : N = 100Output : 100`

A Simple Solution is to traverse all of the numbers in decreasing order from the given number down till 1 and if any of these numbers is a factor of the given number and is also a perfect square, print that number.
Time Complexity: O(N)
Better Solution : A better solution is to use prime factorization of the given number.

• First find all the prime factors of that number till sqrt(num).
• Take a variable, answer and initialize it to 1. It represents the largest square number which is also a factor of that number.
• Now, Check If any prime number occurs even number of times in the prime factorization of the given number, if yes then multiply the answer with that prime factor the number of times it occurs.
• Else, if it occurs odd number of times then multiply the answer with prime number (K – 1) times, K is the frequency of that prime factor in the prime factorization.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the largest factor of``// a number which is also a perfect square` `#include ``#include ``using` `namespace` `std;` `// Function to find the largest factor``// of a given number which``// is a perfect square``int` `largestSquareFactor(``int` `num)``{``    ``// Initialise the answer to 1``    ``int` `answer = 1;` `    ``// Finding the prime factors till sqrt(num)``    ``for` `(``int` `i = 2; i < ``sqrt``(num); ++i) {``        ``// Frequency of the prime factor in the``        ``// factorisation initialised to 0``        ``int` `cnt = 0;``        ``int` `j = i;` `        ``while` `(num % j == 0) {``            ``cnt++;``            ``j *= i;``        ``}` `        ``// If the frequency is odd then multiply i``        ``// frequency-1 times to the answer``        ``if` `(cnt & 1) {``            ``cnt--;``            ``answer *= ``pow``(i, cnt);``        ``}``        ``// Else if it is even, multiply``        ``// it frequency times``        ``else` `{``            ``answer *= ``pow``(i, cnt);``        ``}``    ``}` `    ``return` `answer;``}` `// Driver Code``int` `main()``{``    ``int` `N = 420;` `    ``cout << largestSquareFactor(N);` `    ``return` `0;``}`

## Java

 `// Java program to find the largest factor of ``// a number which is also a perfect square ` `class` `GFG``{``  ` `// Function to find the largest factor ``// of a given number which ``// is a perfect square ``static` `int` `largestSquareFactor(``int` `num) ``{ ``    ``// Initialise the answer to 1 ``    ``int` `answer = ``1``; ``  ` `    ``// Finding the prime factors till sqrt(num) ``    ``for` `(``int` `i = ``2``; i < Math.sqrt(num); ++i) { ``        ``// Frequency of the prime factor in the ``        ``// factorisation initialised to 0 ``        ``int` `cnt = ``0``; ``        ``int` `j = i; ``  ` `        ``while` `(num % j == ``0``) { ``            ``cnt++; ``            ``j *= i; ``        ``} ``  ` `        ``// If the frequency is odd then multiply i ``        ``// frequency-1 times to the answer ``        ``if` `((cnt & ``1``)!=``0``) { ``            ``cnt--; ``            ``answer *= Math.pow(i, cnt); ``        ``} ``        ``// Else if it is even, multiply ``        ``// it frequency times ``        ``else` `{ ``            ``answer *= Math.pow(i, cnt); ``        ``} ``    ``} ``  ` `    ``return` `answer; ``} ``  ` `// Driver Code ``public` `static` `void`  `main(String args[]) ``{ ``    ``int` `N = ``420``; ``  ` `    ``System.out.println(largestSquareFactor(N)); ``  ` `}``} `

## Python 3

 `# Python 3 program to find the largest ``# factor of a number which is also a ``# perfect square``import` `math` `# Function to find the largest factor``# of a given number which is a``# perfect square``def` `largestSquareFactor( num):` `    ``# Initialise the answer to 1``    ``answer ``=` `1` `    ``# Finding the prime factors till sqrt(num)``    ``for` `i ``in` `range``(``2``, ``int``(math.sqrt(num))) :``        ` `        ``# Frequency of the prime factor in the``        ``# factorisation initialised to 0``        ``cnt ``=` `0``        ``j ``=` `i` `        ``while` `(num ``%` `j ``=``=` `0``) :``            ``cnt ``+``=` `1``            ``j ``*``=` `i` `        ``# If the frequency is odd then multiply i``        ``# frequency-1 times to the answer``        ``if` `(cnt & ``1``) :``            ``cnt ``-``=` `1``            ``answer ``*``=` `pow``(i, cnt)``        ` `        ``# Else if it is even, multiply``        ``# it frequency times``        ``else` `:``            ``answer ``*``=` `pow``(i, cnt)``    ``return` `answer` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``N ``=` `420` `    ``print``(largestSquareFactor(N))` `# This code is contributed ``# by ChitraNayal`

## C#

 `// C# program to find the largest factor of ``// a number which is also a perfect square ``using` `System;` `class` `GFG``{``    ` `// Function to find the largest factor of ``// a given number which is a perfect square ``static` `double` `largestSquareFactor(``double` `num) ``{ ``    ``// Initialise the answer to 1 ``    ``double` `answer = 1; ` `    ``// Finding the prime factors``    ``// till sqrt(num) ``    ``for` `(``int` `i = 2; i < Math.Sqrt(num); ++i) ``    ``{ ``        ``// Frequency of the prime factor in ``        ``// the factorisation initialised to 0 ``        ``int` `cnt = 0; ``        ``int` `j = i; ` `        ``while` `(num % j == 0)``        ``{ ``            ``cnt++; ``            ``j *= i; ``        ``} ` `        ``// If the frequency is odd then multiply i ``        ``// frequency-1 times to the answer ``        ``if` `((cnt & 1) != 0) ``        ``{ ``            ``cnt--; ``            ``answer *= Math.Pow(i, cnt); ``        ``} ``        ` `        ``// Else if it is even, multiply ``        ``// it frequency times ``        ``else``        ``{ ``            ``answer *= Math.Pow(i, cnt); ``        ``} ``    ``} ` `    ``return` `answer; ``} ` `// Driver Code ``static` `public` `void` `Main ()``{``    ``int` `N = 420; ``    ``Console.WriteLine(largestSquareFactor(N)); ``}``}` `// This code is contributed by Sach_Code`

## Javascript

 ``

## PHP

 ``

Output
```4

```

Time Complexity: O( sqrtn*logn)
Auxiliary Space: O(1)

Approach:

1. Initialize a variable max_factor to 1, which will store the largest factor of the given number that is a perfect square.
2. Iterate through all factors of the given number num using a for loop that goes from 1 to the square root of num.
3. Check if i is a factor of num using the modulo operator. If it is, then find the corresponding factor num/i.
4. Check if factor1 is a perfect square using the sqrt and floor functions from the cmath library. If it is, then update max_factor to be the maximum of max_factor and factor1.
5. Check if factor2 is a perfect square using the sqrt and floor functions from the cmath library. If it is, then update max_factor to be the maximum of max_factor and factor2.
6. Return the final value of max_factor.

Below is the implementation of the above approach:

## C++

 `#include ``using` `namespace` `std;` `// Function to find the largest factor``// of a given number which is a perfect square``int` `largestSquareFactor(``int` `num)``{``    ``int` `max_factor = 1;``    ``for` `(``int` `i = 1; i <= ``sqrt``(num); i++) {``        ``if` `(num % i == 0) {``            ``int` `factor1 = i;``            ``int` `factor2 = num / i;``            ``if` `(``sqrt``(factor1) == ``floor``(``sqrt``(factor1))) {``                ``max_factor = max(max_factor, factor1);``            ``}``            ``if` `(``sqrt``(factor2) == ``floor``(``sqrt``(factor2))) {``                ``max_factor = max(max_factor, factor2);``            ``}``        ``}``    ``}``    ``return` `max_factor;``}` `// Driver Code``int` `main()``{``    ``int` `N = 420;``    ``cout << largestSquareFactor(N);``    ``return` `0;``}`

## Java

 `// Java implementation of above approach``import` `java.util.*;` `public` `class` `Main {``    ``// Function to find the largest factor``    ``// of a given number which is a perfect square``    ``public` `static` `int` `largestSquareFactor(``int` `num) {``        ``int` `max_factor = ``1``;``        ``for` `(``int` `i = ``1``; i <= Math.sqrt(num); i++) {``            ``if` `(num % i == ``0``) {``                ``int` `factor1 = i;``                ``int` `factor2 = num / i;``                ``if` `(Math.sqrt(factor1) == Math.floor(Math.sqrt(factor1))) {``                    ``max_factor = Math.max(max_factor, factor1);``                ``}``                ``if` `(Math.sqrt(factor2) == Math.floor(Math.sqrt(factor2))) {``                    ``max_factor = Math.max(max_factor, factor2);``                ``}``            ``}``        ``}``        ``return` `max_factor;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args) {``        ``int` `N = ``420``;``        ``System.out.println(largestSquareFactor(N));``    ``}``}`

## Python3

 `import` `math` `# Function to find the largest factor``# of a given number which is a perfect square``def` `largestSquareFactor(num):``    ``max_factor ``=` `1``    ``for` `i ``in` `range``(``1``, ``int``(math.sqrt(num))``+``1``):``        ``if` `num ``%` `i ``=``=` `0``:``            ``factor1 ``=` `i``            ``factor2 ``=` `num ``/``/` `i``            ``if` `math.sqrt(factor1) ``=``=` `int``(math.sqrt(factor1)):``                ``max_factor ``=` `max``(max_factor, factor1)``            ``if` `math.sqrt(factor2) ``=``=` `int``(math.sqrt(factor2)):``                ``max_factor ``=` `max``(max_factor, factor2)``    ``return` `max_factor` `# Driver Code``N ``=` `420``print``(largestSquareFactor(N))`

## C#

 `using` `System;` `public` `class` `Program``{``    ``// Function to find the largest factor of a given number which is a perfect square``    ``static` `int` `LargestSquareFactor(``int` `num)``    ``{``        ``int` `maxFactor = 1;` `        ``// Iterate through all numbers from 1 up to the square root of num``        ``for` `(``int` `i = 1; i <= Math.Sqrt(num); i++)``        ``{``            ``if` `(num % i == 0)``            ``{``                ``int` `factor1 = i;``                ``int` `factor2 = num / i;` `                ``// Check if factor1 is a perfect square``                ``if` `(Math.Sqrt(factor1) == Math.Floor(Math.Sqrt(factor1)))``                ``{``                    ``maxFactor = Math.Max(maxFactor, factor1);``                ``}` `                ``// Check if factor2 is a perfect square``                ``if` `(Math.Sqrt(factor2) == Math.Floor(Math.Sqrt(factor2)))``                ``{``                    ``maxFactor = Math.Max(maxFactor, factor2);``                ``}``            ``}``        ``}``        ``return` `maxFactor;``    ``}` `    ``public` `static` `void` `Main()``    ``{``        ``int` `N = 420;``        ``Console.WriteLine(LargestSquareFactor(N));``    ``}``}`

## Javascript

 `// Function to find the largest factor``// of a given number which is a perfect square``function` `largestSquareFactor(num) {``    ``let max_factor = 1;``    ``for` `(let i = 1; i <= Math.sqrt(num); i++) {``        ``if` `(num % i == 0) {``            ``let factor1 = i;``            ``let factor2 = num / i;``            ``if` `(Math.sqrt(factor1) == Math.floor(Math.sqrt(factor1))) {``                ``max_factor = Math.max(max_factor, factor1);``            ``}``            ``if` `(Math.sqrt(factor2) == Math.floor(Math.sqrt(factor2))) {``                ``max_factor = Math.max(max_factor, factor2);``            ``}``        ``}``    ``}``    ``return` `max_factor;``}` `// Driver Code``let N = 420;``console.log(largestSquareFactor(N));`

Output
```4

```

Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)

Previous
Next
Share your thoughts in the comments
Similar Reads