📌  相关文章
📜  找出要除的最小数,使一个数成为一个完美的平方

📅  最后修改于: 2021-04-24 19:20:55             🧑  作者: Mango

给定正整数n 。求出除以n的最小数,使其成为一个完美的平方。
例子:

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

如果所有质数出现偶数次,则数字是一个完美的平方。这个想法是找到n的素因数并找到每个素因数幂。现在,找到并乘以所有幂为奇数的素数。乘法的结果就是答案。

C++
// C++ program to find minimum number which divide n
// to make it a perfect square.
#include
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 seprately.
    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 seprately.
        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 seprately.
    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 seprately.
        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
 2)
        $ans *= $n;
 
    return $ans;
}
 
    // Driver Code
    $n = 72;
    echo findMinNumber($n), "\n";
     
// This code is contributed by ajit.
?>


Javascript


输出:

2