📜  在给定数字N的除数中找到最大的良好数

📅  最后修改于: 2021-04-29 03:51:36             🧑  作者: Mango

给定数字N。任务是在给定数字N的除数中找到最大的良好数。如果不存在这样的正整数a> 1,使得a ^ 2是…的除数,则将数字X定义为良好数。 X。

例子:

Input: N = 10
Output: 10
In 1, 2, 5, 10. 
10 is the largest good number

Input: N = 12
Output: 6
In 1, 2, 3, 4, 6, 12. 
6 is the largest good number

方法:找到N的所有主要除数。假定它们是p1,p2,…,pk(以O(sqrt(n))时间复杂度表示)。如果答案是a,那么我们知道对于每个1 <= I <= k,显然,a不能被pi ^ 2(以及pi的所有更大幂)整除。因此,<= p1×p2×…×pk。而且我们知道p1×p2×…×pk本身就是一个好数字。因此,答案是p1×p2×…×pk。

下面是上述方法的实现:

C++
// CPP program to find the largest, good
// number in the divisors of given number N.
#include 
using namespace std;
  
// function to return distinct prime factors
vector PrimeFactors(int n)
{
    // to store distinct prime factors
    vector v;
  
    int x = n;
  
    // run a loop upto sqrt(n)
    for (int i = 2; i * i <= n; i++) {
        if (x % i == 0) {
  
            // place this prime factor in vector
            v.push_back(i);
            while (x % i == 0)
                x /= i;
        }
    }
  
    // This condition is to handle the case when n
    // is a prime number greater than 1
    if (x > 1)
        v.push_back(x);
  
    return v;
}
  
// function that returns good number
int GoodNumber(int n)
{
    // distinct prime factors
    vector v = PrimeFactors(n);
  
    // to store answer
    int ans = 1;
  
    // product of all distinct prime
    // factors is required answer
    for (int i = 0; i < v.size(); i++)
        ans *= v[i];
  
    return ans;
}
  
// Driver code
int main()
{
    int n = 12;
  
    // function call
    cout << GoodNumber(n);
  
    return 0;
}


Java
// Java program to find the largest, good
// number in the divisors of given number N.
  
import java.util.*;
  
class GFG {
    // function to return distinct prime factors
  
    static Vector PrimeFactors(int n)
    {
  
        // to store distinct prime factors
        Vector v = new Vector();
        int x = n;
  
        // run a loop upto sqrt(n)
        for (int i = 2; i * i <= n; i++) {
            if (x % i == 0) {
  
                // place this prime factor in vector
                v.add(i);
                while (x % i == 0)
                    x /= i;
            }
        }
  
        // This condition is to handle the case when n
        // is a prime number greater than 1
        if (x > 1)
            v.add(x);
  
        return v;
    }
  
    // function that returns good number
    static int GoodNumber(int n)
    {
        // distinct prime factors
        Vector v = new Vector(PrimeFactors(n));
  
        // to store answer
        int ans = 1;
  
        // product of all distinct prime
        // factors is required answer
        for (int i = 0; i < v.size(); i++)
            ans *= v.get(i);
  
        return ans;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 12;
  
        // function call
        System.out.println(GoodNumber(n));
    }
}
  
// This code is contributed by ihritik


Python 3
# Python 3 program to find the 
# largest, good number in the 
# divisors of given number N.
  
# function to return distinct 
# prime factors
def PrimeFactors(n):
  
    # to store distinct 
    # prime factors
    v = []
  
    x = n
  
    # run a loop upto sqrt(n)
    i = 2
    while(i * i <= n):
        if (x % i == 0) :
  
            # place this prime factor
            # in vector
            v.append(i)
            while (x % i == 0):
                x //= i
                  
        i += 1
  
    # This condition is to handle 
    # the case when n is a prime 
    # number greater than 1
    if (x > 1):
        v.append(x)
  
    return v
  
# function that returns good number
def GoodNumber(n):
      
    # distinct prime factors
    v = PrimeFactors(n)
  
    # to store answer
    ans = 1
  
    # product of all distinct prime
    # factors is required answer
    for i in range(len(v)):
        ans *= v[i]
  
    return ans
  
# Driver code
if __name__ == "__main__":
    n = 12
  
    # function call
    print(GoodNumber(n))
  
# This code is contributed
# by ChitraNayal


C#
// C# program to find the largest, good
// number in the divisors of given number N.
using System;
using System.Collections.Generic;
public class GFG {
    // function to return distinct prime factors
  
    static List PrimeFactors(int n)
    {
  
        // to store distinct prime factors
        List v = new List();
        int x = n;
  
        // run a loop upto sqrt(n)
        for (int i = 2; i * i <= n; i++) {
            if (x % i == 0) {
  
                // place this prime factor in vector
                v.Add(i);
                while (x % i == 0)
                    x /= i;
            }
        }
  
        // This condition is to handle the case when n
        // is a prime number greater than 1
        if (x > 1)
            v.Add(x);
  
        return v;
    }
  
    // function that returns good number
    static int GoodNumber(int n)
    {
        // distinct prime factors
        List v = new List(PrimeFactors(n));
  
        // to store answer
        int ans = 1;
  
        // product of all distinct prime
        // factors is required answer
        for (int i = 0; i < v.Count; i++)
            ans *= v[i];
  
        return ans;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int n = 12;
  
        // function call
        Console.WriteLine(GoodNumber(n));
    }
}
  
// This code has been contributed by 29AjayKumar


PHP
 1) 
        array_push($v, $x); 
  
    return $v; 
} 
  
// function that returns good number 
function GoodNumber($n) 
{ 
    // distinct prime factors 
    $v = PrimeFactors($n); 
  
    // to store answer 
    $ans = 1; 
  
    // product of all distinct prime 
    // factors is required answer 
    for ($i = 0; $i < count($v); $i++) 
        $ans *= $v[$i]; 
  
    return $ans; 
} 
  
// Driver code 
$n = 12; 
  
// function call 
echo GoodNumber($n); 
  
// This code is contributed by Rajput-Ji
?>


输出:
6