Find minimum number to be divided to make a number a perfect square
Last Updated :
12 Sep, 2023
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++
#include<bits/stdc++.h>
using namespace std;
int findMinNumber( int n)
{
int count = 0, ans = 1;
while (n%2 == 0)
{
count++;
n /= 2;
}
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%2)
ans *= i;
}
if (n > 2)
ans *= n;
return ans;
}
int main()
{
int n = 72;
cout << findMinNumber(n) << endl;
return 0;
}
|
Java
class GFG
{
static int findMinNumber( int n)
{
int count = 0 , ans = 1 ;
while (n % 2 == 0 )
{
count++;
n /= 2 ;
}
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 % 2 == 1 )
ans *= i;
}
if (n > 2 )
ans *= n;
return ans;
}
public static void main (String[] args)
{
int n = 72 ;
System.out.println(findMinNumber(n));
}
}
|
Python3
import math
def findMinNumber(n):
count = 0
ans = 1
while n % 2 = = 0 :
count + = 1
n / / = 2
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 % 2 is not 0 :
ans * = i
if n > 2 :
ans * = n
return ans
n = 72
print (findMinNumber(n))
|
C#
using System;
class GFG
{
static int findMinNumber( int n)
{
int count = 0, ans = 1;
while (n % 2 == 0)
{
count++;
n /= 2;
}
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 % 2 == 1)
ans *= i;
}
if (n > 2)
ans *= n;
return ans;
}
public static void Main ()
{
int n = 72;
Console.WriteLine(findMinNumber(n));
}
}
|
PHP
<?php
function findMinNumber( $n )
{
$count = 0;
$ans = 1;
while ( $n % 2 == 0)
{
$count ++;
$n /= 2;
}
if ( $count % 2)
$ans *= 2;
for ( $i = 3; $i <= sqrt( $n ); $i += 2)
{
$count = 0;
while ( $n % $i == 0)
{
$count ++;
$n /= $i ;
}
if ( $count % 2)
$ans *= $i ;
}
if ( $n > 2)
$ans *= $n ;
return $ans ;
}
$n = 72;
echo findMinNumber( $n ), "\n" ;
?>
|
Javascript
<script>
function findMinNumber(n)
{
let count = 0;
let ans = 1;
while (n % 2 == 0)
{
count++;
n /= 2;
}
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 % 2)
ans *= i;
}
if (n > 2)
ans *= n;
return ans;
}
let n = 72;
document.write(findMinNumber(n) + "\n" );
</script>
|
Output:
2
Time Complexity: O(?n log n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...