Largest factor of a given number which is a perfect square

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

Examples:

Input : N = 420
Output : 4

Input : N = 100
Output : 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the largest factor of
// a number which is also a perfect square
  
#include <cmath>
#include <iostream>
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 mutiply 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 mutiply 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)); 
    
}

chevron_right


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 mutiply 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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 mutiply 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the largest 
// factor of a number which is also 
// a perfect square 
  
// Function to find the largest 
// factor of a given number which 
// is a perfect square 
function largestSquareFactor($num
    // Initialise the answer to 1 
    $answer = 1; 
  
    // Finding the prime factors
    // till sqrt(num) 
    for ($i = 2; $i < sqrt($num); ++$i
    
        // Frequency of the prime factor 
        // in the factorisation initialised to 0 
        $cnt = 0; 
        $j = $i
  
        while ($num % $j == 0) 
        
            $cnt++; 
            $j *= $i
        
  
        // If the frequency is odd then 
        // mutiply 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 
$N = 420; 
echo largestSquareFactor($N); 
  
// This code is contributed 
// by Sach_Code
?>

chevron_right


Output:

4

Time Complexity: O( sqrt(N) )



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.