📌  相关文章
📜  给定数字的最大因数是一个完美的平方

📅  最后修改于: 2021-05-04 09:25:40             🧑  作者: Mango

给定一个数字N   。任务是找到该数字的最大因数,即一个完美的平方。
例子

Input : N = 420
Output : 4

Input : N = 100
Output : 100

一个简单的解决方案是从给定的数字到1降序遍历所有数字,如果这些数字中的任何一个是给定数字的因数,并且也是一个理想的平方,则打印该数字。
时间复杂度:O(N)
更好的解决方案:更好的解决方案是使用给定数的素数分解。

  • 首先找到该数字的所有素数,直到sqrt(num)。
  • 取一个变量,答案并将其初始化为1。它代表最大的平方数,也是该数的一个因数。
  • 现在,检查给定数的质数分解中是否有质数出现偶数次,如果是,则将答案乘以该质数的次数。
  • 否则,如果发生奇数次,然后将答案乘以质数(K – 1)倍,则K是质因数分解中该质因数的频率。

下面是上述方法的实现:

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 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;
}


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


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


PHP


Javascript


输出:
4

时间复杂度:O(sqrt(N))