📜  最大唯一因子个数

📅  最后修改于: 2021-04-30 02:56:03             🧑  作者: Mango

给定数字N,请找到在[1,N]范围内任何数字都可以具有的唯一素数的最大数目。

例子:

Input : N = 500
Output : 4
The maximum number of prime factors
for any number in [1, 500] is 4. A
number in range that has 4 prime 
factors is 210 (2 x 3 x 5 x 7)

Input  : N = 3
Output : 1

Input : N = 5000
Output : 5

方法1(强力):
对于从1到N的每个整数,请找到每个整数的质数因子的数量,并找到最大唯一质数因子的数量。

方法2(更好的方法):
使用筛分法计算每个小于N的素数的个数,并找到具有最大个数的最小数。

以下是此方法的实现:

C++
// C++ program to find maximum number of prime
// factors for a number in range [1, N]
#include 
using namespace std;
  
// Return smallest number having maximum
// prime factors.
int maxPrimefactorNum(int N)
{
    // Sieve of eratosthenes method to count
    // number of unique prime factors.
    int arr[N + 1];
    memset(arr, 0, sizeof(arr));
    for (int i = 2; i * i <= N; i++) {
        if (!arr[i])
            for (int j = 2 * i; j <= N; j += i)
                arr[j]++;
  
        arr[i] = 1; 
    }
  
    // Return maximum element in arr[]
    return *max_element(arr, arr+N);
}
  
// Driven Program
int main()
{
    int N = 40;
    cout << maxPrimefactorNum(N) << endl;
    return 0;
}


Java
// Java program to find maximum 
// number of prime factors for 
// a number in range [1, N]
class GFG
{
static int getMax(int[] Arr)
{ 
    int max = Arr[0]; 
    for(int i = 1; i < Arr.length; i++) 
    if(Arr[i] > max)
        max = Arr[i]; 
    return max; 
}
  
// Return smallest number 
// having maximum prime factors.
static int maxPrimefactorNum(int N)
{
    // Sieve of eratosthenes method 
    // to count number of unique
    // prime factors.
    int[] arr = new int[N + 1];
    for (int i = 2; i * i <= N; i++) 
    {
        if (arr[i] == 0)
            for (int j = 2 * i; j <= N; j += i)
                arr[j]++;
  
        arr[i] = 1; 
    }
  
    // Return maximum element in arr[]
    return getMax(arr);
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 40;
    System.out.println(maxPrimefactorNum(N));
}
}
  
// This code is contributed by mits


Python3
# Python3 program to find maximum number 
# of prime factors for a number in range [1, N]
  
# Return smallest number having maximum
# prime factors.
def maxPrimefactorNum(N):
  
    # Sieve of eratosthenes method to count
    # number of unique prime factors.
    arr = [0] * (N + 1);
    i = 2;
    while (i * i <= N): 
        if (arr[i] > 0):
            for j in range(2 * i, N + 1, i):
                arr[j] += 1;
        i += 1;
  
        arr[i] = 1; 
  
    # Return maximum element in arr[]
    return max(arr);
  
# Driver Code
N = 40;
print(maxPrimefactorNum(N));
  
# This code is contributed by mits


C#
// C# program to find maximum 
// number of prime factors for 
// a number in range [1, N]
using System;
  
class GFG
{
static int getMax(int[] Arr)
{ 
    int max = Arr[0]; 
    for(int i = 1; i < Arr.Length; i++) 
    if(Arr[i] > max)
        max = Arr[i]; 
    return max; 
}
  
// Return smallest number 
// having maximum prime factors.
static int maxPrimefactorNum(int N)
{
    // Sieve of eratosthenes method 
    // to count number of unique
    // prime factors.
    int[] arr = new int[N + 1];
    for (int i = 2; i * i <= N; i++) 
    {
        if (arr[i] == 0)
            for (int j = 2 * i; 
                     j <= N; j += i)
                arr[j]++;
  
        arr[i] = 1; 
    }
  
    // Return maximum
    // element in arr[]
    return getMax(arr);
}
  
// Driver Code
public static void Main()
{
    int N = 40;
    Console.WriteLine(maxPrimefactorNum(N));
}
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


C++
// C++ program to find maximum number of prime
// factors in first N natural numbers
#include 
using namespace std;
  
// Return maximum number of prime factors for
// any number in [1, N]
int maxPrimefactorNum(int N)
{
    if (N < 2)
        return 0;
  
    // Based on Sieve of Eratosthenes
    // https://www.geeksforgeeks.org/sieve-of-eratosthenes/
    bool arr[N+1];
    memset(arr, true, sizeof(arr));
    int prod = 1, res = 0;
    for (int p=2; p*p<=N; p++)
    {
        // If p is prime
        if (arr[p] == true)
        {
            for (int i=p*2; i<=N; i += p)
                arr[i] = false;
  
            // We simply multiply first set
            // of prime numbers while the
            // product is smaller than N.
            prod *= p;
            if (prod > N)
                return res;
            res++;
        }
    }
  
    return res;
}
  
// Driven Program
int main()
{
    int N = 500;
    cout << maxPrimefactorNum(N) << endl;
    return 0;
}


Java
// Java program to find maximum 
// number of prime factors in 
// first N natural numbers
  
class GFG
{
// Return maximum number 
// of prime factors for
// any number in [1, N]
static int maxPrimefactorNum(int N)
{
    if (N < 2)
        return 0;
  
    // Based on Sieve of Eratosthenes
    // https://www.geeksforgeeks.org/sieve-of-eratosthenes/
    boolean[] arr = new boolean[N + 1];
    int prod = 1, res = 0;
    for (int p = 2; p * p <= N; p++)
    {
        // If p is prime
        if (arr[p] == false)
        {
            for (int i = p * 2; 
                     i <= N; i += p)
                arr[i] = true;
  
            // We simply multiply first set
            // of prime numbers while the
            // product is smaller than N.
            prod *= p;
            if (prod > N)
                return res;
            res++;
        }
    }
  
    return res;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 500;
    System.out.println(maxPrimefactorNum(N));
}
}
  
// This code is contributed by mits


Python3
# Python3 program to find maximum number 
# of prime factors in first N natural numbers 
  
# Return maximum number of prime factors 
# for any number in [1, N] 
def maxPrimefactorNum(N): 
  
    if (N < 2): 
        return 0; 
  
    arr = [True] * (N + 1); 
    prod = 1; 
    res = 0;
    p = 2;
    while (p * p <= N):
          
        # If p is prime 
        if (arr[p] == True): 
            for i in range(p * 2, N + 1, p): 
                arr[i] = False; 
  
            # We simply multiply first set 
            # of prime numbers while the 
            # product is smaller than N. 
            prod *= p; 
            if (prod > N): 
                return res; 
            res += 1;
        p += 1;
  
    return res; 
  
# Driver Code 
N = 500; 
print(maxPrimefactorNum(N)); 
  
# This code is contributed by mits


C#
// C# program to find maximum number of 
// prime factors in first N natural numbers 
using System;
  
class GFG 
{ 
  
// Return maximum number of prime 
// factors for any number in [1, N] 
static int maxPrimefactorNum(int N) 
{ 
    if (N < 2) 
        return 0; 
  
    // Based on Sieve of Eratosthenes 
    // https://www.geeksforgeeks.org/sieve-of-eratosthenes/ 
    bool[] arr = new bool[N + 1]; 
    int prod = 1, res = 0; 
    for (int p = 2; p * p <= N; p++) 
    { 
        // If p is prime 
        if (arr[p] == false) 
        { 
            for (int i = p * 2; 
                     i <= N; i += p) 
                arr[i] = true; 
  
            // We simply multiply first set 
            // of prime numbers while the 
            // product is smaller than N. 
            prod *= p; 
            if (prod > N) 
                return res; 
            res++; 
        } 
    } 
  
    return res; 
} 
  
// Driver Code 
public static void Main() 
{ 
    int N = 500; 
    Console.WriteLine(maxPrimefactorNum(N)); 
} 
} 
  
// This code is contributed 
// by 29AjayKumar


PHP
 $N)
                return $res;
            $res++;
        }
    }
  
    return $res;
}
  
// Driver Code
$N = 500;
echo maxPrimefactorNum($N) . "\n";
  
// This code is contributed by mits
?>


输出:

3

方法3(有效方法):
使用Sieve生成N之前的所有素数。现在,将连续的质数(从第一个质数开始)一个接一个地相乘,直到乘积小于N。这个想法基于这样一个简单的事实:第一组质数会导致最大的唯一质数因子。

以下是此方法的实现:

C++

// C++ program to find maximum number of prime
// factors in first N natural numbers
#include 
using namespace std;
  
// Return maximum number of prime factors for
// any number in [1, N]
int maxPrimefactorNum(int N)
{
    if (N < 2)
        return 0;
  
    // Based on Sieve of Eratosthenes
    // https://www.geeksforgeeks.org/sieve-of-eratosthenes/
    bool arr[N+1];
    memset(arr, true, sizeof(arr));
    int prod = 1, res = 0;
    for (int p=2; p*p<=N; p++)
    {
        // If p is prime
        if (arr[p] == true)
        {
            for (int i=p*2; i<=N; i += p)
                arr[i] = false;
  
            // We simply multiply first set
            // of prime numbers while the
            // product is smaller than N.
            prod *= p;
            if (prod > N)
                return res;
            res++;
        }
    }
  
    return res;
}
  
// Driven Program
int main()
{
    int N = 500;
    cout << maxPrimefactorNum(N) << endl;
    return 0;
}

Java

// Java program to find maximum 
// number of prime factors in 
// first N natural numbers
  
class GFG
{
// Return maximum number 
// of prime factors for
// any number in [1, N]
static int maxPrimefactorNum(int N)
{
    if (N < 2)
        return 0;
  
    // Based on Sieve of Eratosthenes
    // https://www.geeksforgeeks.org/sieve-of-eratosthenes/
    boolean[] arr = new boolean[N + 1];
    int prod = 1, res = 0;
    for (int p = 2; p * p <= N; p++)
    {
        // If p is prime
        if (arr[p] == false)
        {
            for (int i = p * 2; 
                     i <= N; i += p)
                arr[i] = true;
  
            // We simply multiply first set
            // of prime numbers while the
            // product is smaller than N.
            prod *= p;
            if (prod > N)
                return res;
            res++;
        }
    }
  
    return res;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 500;
    System.out.println(maxPrimefactorNum(N));
}
}
  
// This code is contributed by mits

Python3

# Python3 program to find maximum number 
# of prime factors in first N natural numbers 
  
# Return maximum number of prime factors 
# for any number in [1, N] 
def maxPrimefactorNum(N): 
  
    if (N < 2): 
        return 0; 
  
    arr = [True] * (N + 1); 
    prod = 1; 
    res = 0;
    p = 2;
    while (p * p <= N):
          
        # If p is prime 
        if (arr[p] == True): 
            for i in range(p * 2, N + 1, p): 
                arr[i] = False; 
  
            # We simply multiply first set 
            # of prime numbers while the 
            # product is smaller than N. 
            prod *= p; 
            if (prod > N): 
                return res; 
            res += 1;
        p += 1;
  
    return res; 
  
# Driver Code 
N = 500; 
print(maxPrimefactorNum(N)); 
  
# This code is contributed by mits 

C#

// C# program to find maximum number of 
// prime factors in first N natural numbers 
using System;
  
class GFG 
{ 
  
// Return maximum number of prime 
// factors for any number in [1, N] 
static int maxPrimefactorNum(int N) 
{ 
    if (N < 2) 
        return 0; 
  
    // Based on Sieve of Eratosthenes 
    // https://www.geeksforgeeks.org/sieve-of-eratosthenes/ 
    bool[] arr = new bool[N + 1]; 
    int prod = 1, res = 0; 
    for (int p = 2; p * p <= N; p++) 
    { 
        // If p is prime 
        if (arr[p] == false) 
        { 
            for (int i = p * 2; 
                     i <= N; i += p) 
                arr[i] = true; 
  
            // We simply multiply first set 
            // of prime numbers while the 
            // product is smaller than N. 
            prod *= p; 
            if (prod > N) 
                return res; 
            res++; 
        } 
    } 
  
    return res; 
} 
  
// Driver Code 
public static void Main() 
{ 
    int N = 500; 
    Console.WriteLine(maxPrimefactorNum(N)); 
} 
} 
  
// This code is contributed 
// by 29AjayKumar

的PHP

 $N)
                return $res;
            $res++;
        }
    }
  
    return $res;
}
  
// Driver Code
$N = 500;
echo maxPrimefactorNum($N) . "\n";
  
// This code is contributed by mits
?>

输出:

4