Friday, March 13, 2026
HomeData Modelling & AIFind minimum number to be divided to make a number a perfect...

Find minimum number to be divided to make a number a perfect square

Given a positive integer n. Find the minimum number which divide n to make it a perfect square.
Examples: 
 

Input : n = 50
Output : 2
By Dividing n by 2, we get which is a perfect square.

Input : n = 6
Output : 6
By Dividing n by 6, we get which is a perfect square.

Input : n = 36
Output : 1

 

A number is perfect square if all prime factors appear even number of times. The idea is to find the prime factor of n and find each prime factor power. Now, find and multiply all the prime factor whose power is odd. The resultant of the multiplication is the answer. 
 

C++




// C++ program to find minimum number which divide n
// to make it a perfect square.
#include<bits/stdc++.h>
using namespace std;
  
// Return the minimum number to be divided to make
// n a perfect square.
int findMinNumber(int n)
{
    int count = 0, ans = 1;
  
    // Since 2 is only even prime, compute its
    // power separately.
    while (n%2 == 0)
    {
        count++;
        n /= 2;
    }
  
    // If count is odd, it must be removed by dividing
    // n by prime number.
    if (count%2)
        ans *= 2;
  
    for (int i = 3; i <= sqrt(n); i += 2)
    {
        count = 0;
        while (n%i == 0)
        {
            count++;
            n /= i;
        }
  
        // If count is odd, it must be removed by
        // dividing n by prime number.
        if (count%2)
            ans *= i;
    }
  
    if (n > 2)
        ans *= n;
  
    return ans;
}
  
// Driven Program
int main()
{
    int n = 72;
    cout << findMinNumber(n) << endl;
    return 0;
}


Java




// Java program to find minimum number 
// which divide n to make it a perfect square.
  
class GFG
{
    // Return the minimum number to be 
    // divided to make n a perfect square.
    static int findMinNumber(int n)
    {
        int count = 0, ans = 1;
      
        // Since 2 is only even prime, 
        // compute its power separately.
        while (n % 2 == 0)
        {
            count++;
            n /= 2;
        }
      
        // If count is odd, it must be removed by dividing
        // n by prime number.
        if (count % 2 == 1)
            ans *= 2;
      
        for (int i = 3; i <= Math.sqrt(n); i += 2)
        {
            count = 0;
            while (n % i == 0)
            {
                count++;
                n /= i;
            }
      
            // If count is odd, it must be removed by
            // dividing n by prime number.
            if (count % 2 == 1)
                ans *= i;
        }
      
        if (n > 2)
            ans *= n;
      
        return ans;
    }
  
    // Driver code
    public static void main (String[] args)
    {
        int n = 72;
        System.out.println(findMinNumber(n));
    }
}
  
// This code is contributed by Anant Agarwal.


Python3




# Python program to find 
# minimum number which 
# divide n to make it a 
# perfect square.
import math
  
# Return the minimum 
# number to be divided 
# to make n a perfect 
# square.
def findMinNumber(n):
    count = 0
    ans = 1
  
    # Since 2 is only 
    # even prime, compute 
    # its power separately.
    while n % 2 == 0:
        count += 1
        n //= 2
  
    # If count is odd, 
    # it must be removed
    # by dividing n by 
    # prime number.
    if count % 2 is not 0:
        ans *= 2
  
    for i in range(3, (int)(math.sqrt(n)) + 1, 2):
        count = 0
        while n % i == 0:
            count += 1
            n //= i
  
        # If count is odd, it 
        # must be removed by 
        # dividing n by prime 
        # number.
        if count % 2 is not 0:
            ans *= i
  
    if n > 2:
        ans *= n
  
    return ans
  
# Driver Code
n = 72
print(findMinNumber(n))
  
# This code is contributed
# by Sanjit_Prasad.


C#




// C# program to find minimum 
// number which divide n to
// make it a perfect square.
using System;
  
class GFG
{
      
    // Return the minimum number
    // to be divided to make 
    // n a perfect square.
    static int findMinNumber(int n)
    {
        int count = 0, ans = 1;
      
        // Since 2 is only even prime, 
        // compute its power separately.
        while (n % 2 == 0)
        {
            count++;
            n /= 2;
        }
      
        // If count is odd, it must 
        // be removed by dividing
        // n by prime number.
        if (count % 2 == 1)
            ans *= 2;
      
        for (int i = 3; i <= Math.Sqrt(n); 
                                  i += 2)
        {
            count = 0;
            while (n % i == 0)
            {
                count++;
                n /= i;
            }
      
            // If count is odd, it must 
            // be removed by dividing n
            // by prime number.
            if (count % 2 == 1)
                ans *= i;
        }
      
        if (n > 2)
            ans *= n;
      
        return ans;
    }
  
    // Driver code
    public static void Main ()
    {
        int n = 72;
        Console.WriteLine(findMinNumber(n));
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find minimum 
// number which divide n
// to make it a perfect square.
  
// Return the minimum number 
// to be divided to make
// n a perfect square.
function findMinNumber($n)
{
    $count = 0;
    $ans = 1;
  
    // Since 2 is only 
    // even prime, 
    // compute its
    // power separately.
    while ($n % 2 == 0)
    {
        $count++;
        $n /= 2;
    }
  
    // If count is odd, 
    // it must be removed 
    // by dividing n by 
    // prime number.
    if ($count % 2)
        $ans *= 2;
  
    for ($i = 3; $i <= sqrt($n); $i += 2)
    {
        $count = 0;
        while ($n % $i == 0)
        {
            $count++;
            $n /= $i;
        }
  
        // If count is odd, 
        // it must be removed
        // by dividing n by 
        // prime number.
        if ($count % 2)
            $ans *= $i;
    }
  
    if ($n > 2)
        $ans *= $n;
  
    return $ans;
}
  
    // Driver Code
    $n = 72;
    echo findMinNumber($n), "\n";
      
// This code is contributed by ajit.
?>


Javascript




<script>
  
// Javascript program to find minimum 
// number which divide n
// to make it a perfect square.
  
// Return the minimum number 
// to be divided to make
// n a perfect square.
function findMinNumber(n)
{
    let count = 0;
    let ans = 1;
  
    // Since 2 is only 
    // even prime, 
    // compute its
    // power separately.
    while (n % 2 == 0)
    {
        count++;
        n /= 2;
    }
  
    // If count is odd, 
    // it must be removed 
    // by dividing n by 
    // prime number.
    if (count % 2)
        ans *= 2;
  
    for (let i = 3; i <= Math.sqrt(n); i += 2)
    {
        count = 0;
        while (n % i == 0)
        {
            count++;
            n /= i;
        }
  
        // If count is odd, 
        // it must be removed
        // by dividing n by 
        // prime number.
        if (count % 2)
            ans *= i;
    }
  
    if (n > 2)
        ans *= n;
  
    return ans;
}
  
    // Driver Code
    let n = 72;
    document.write(findMinNumber(n) + "\n");
      
// This code is contributed by _saurabh_jaiswal.
  
</script>


Output:  

2

Time Complexity: O(?n log n) 
Auxiliary Space: O(1)

This article is contributed by Aarti_Rathi and Anuj Chauhan. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS