📜  在M到N范围内具有最大不同素数的最大数量的数字

📅  最后修改于: 2021-05-31 23:20:14             🧑  作者: Mango

给定两个数字M和N。任务是打印一个数字,该数字在M和N范围内具有最大数量的不同素数。如果存在多个数字,则打印最小的数字。

例子:

方法是使用Erathosthenes的筛子。创建一个factorCount []数组来存储数字的不同素数的数量。将数字标记为素数时,以其倍数递增素数的计数。最后,获取存储在factorCount []数组中的最大数量,这将是答案。

下面是上述方法的实现:

C++
// C++ program to print the
// Number which has the maximum number
// of distinct prime factors of
// numbers in range m to n
#include 
using namespace std;
 
// Function to return the maximum number
int maximumNumberDistinctPrimeRange(int m, int n)
{
    // array to store the number of distinct primes
    long long factorCount[n + 1];
 
    // true if index 'i' is a prime
    bool prime[n + 1];
 
    // initializing the number of factors to 0 and
    for (int i = 0; i <= n; i++) {
        factorCount[i] = 0;
        prime[i] = true; // Used in Sieve
    }
 
    for (int i = 2; i <= n; i++) {
 
        // condition works only when 'i' is prime,
        // hence for factors of all prime number,
        // the prime status is changed to false
        if (prime[i] == true) {
 
            // Number is prime
            factorCount[i] = 1;
 
            // number of factor of a prime number is 1
            for (int j = i * 2; j <= n; j += i) {
 
                // incrementing factorCount all
                // the factors of i
                factorCount[j]++;
 
                // and changing prime status to false
                prime[j] = false;
            }
        }
    }
 
    // Initialize the max and num
    int max = factorCount[m];
    int num = m;
 
    // Gets the maximum number
    for (int i = m; i <= n; i++) {
 
        // Gets the maximum number
        if (factorCount[i] > max) {
            max = factorCount[i];
            num = i;
        }
    }
    return num;
}
 
// Driver code
int main()
{
    int m = 4, n = 6;
    // Calling function
    cout << maximumNumberDistinctPrimeRange(m, n);
    return 0;
}


Java
// Java program to print the
// Number which has the maximum
// number of distinct prime
// factors of numbers in range
// m to n
import java.io.*;
 
class GFG
{
 
// Function to return
// the maximum number
static int maximumNumberDistinctPrimeRange(int m,
                                           int n)
{
    // array to store the
    // number of distinct primes
    long factorCount[] = new long[n + 1];
 
    // true if index 'i'
    // is a prime
    boolean prime[] = new boolean[n + 1];
 
    // initializing the number
    // of factors to 0 and
    for (int i = 0; i <= n; i++)
    {
        factorCount[i] = 0;
        prime[i] = true; // Used in Sieve
    }
 
    for (int i = 2; i <= n; i++)
    {
 
        // condition works only when
        // 'i' is prime, hence for
        // factors of all prime number,
        // the prime status is changed to false
        if (prime[i] == true)
        {
 
            // Number is prime
            factorCount[i] = 1;
 
            // number of factor of
            // a prime number is 1
            for (int j = i * 2; j <= n; j += i)
            {
 
                // incrementing factorCount
                // all the factors of i
                factorCount[j]++;
 
                // and changing prime
                // status to false
                prime[j] = false;
            }
        }
    }
 
    // Initialize the max and num
    int max = (int)factorCount[m];
    int num = m;
 
    // Gets the maximum number
    for (int i = m; i <= n; i++)
    {
 
        // Gets the maximum number
        if (factorCount[i] > max)
        {
            max = (int)factorCount[i];
            num = i;
        }
    }
    return num;
}
 
// Driver code
public static void main (String[] args)
{
int m = 4, n = 6;
 
// Calling function
System.out.println(maximumNumberDistinctPrimeRange(m, n));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python 3 program to print the
# Number which has the maximum number
# of distinct prime factors of
# numbers in range m to n
 
# Function to return the maximum number
def maximumNumberDistinctPrimeRange(m, n):
 
    # array to store the number
    # of distinct primes
    factorCount = [0] * (n + 1)
 
    # true if index 'i' is a prime
    prime = [False] * (n + 1)
 
    # initializing the number of
    # factors to 0 and
    for i in range(n + 1) :
        factorCount[i] = 0
        prime[i] = True # Used in Sieve
 
    for i in range(2, n + 1) :
 
        # condition works only when 'i'
        # is prime, hence for factors of
        # all prime number, the prime
        # status is changed to false
        if (prime[i] == True) :
 
            # Number is prime
            factorCount[i] = 1
 
            # number of factor of a
            # prime number is 1
            for j in range(i * 2, n + 1, i) :
 
                # incrementing factorCount all
                # the factors of i
                factorCount[j] += 1
 
                # and changing prime status
                # to false
                prime[j] = False
 
    # Initialize the max and num
    max = factorCount[m]
    num = m
 
    # Gets the maximum number
    for i in range(m, n + 1) :
 
        # Gets the maximum number
        if (factorCount[i] > max) :
            max = factorCount[i]
            num = i
    return num
 
# Driver code
if __name__ == "__main__":
    m = 4
    n = 6
     
    # Calling function
    print(maximumNumberDistinctPrimeRange(m, n))
     
# This code is contributed
# by ChitraNayal


C#
// C# program to print the
// Number which has the maximum
// number of distinct prime
// factors of numbers in range
// m to n
using System;
 
class GFG
{
 
// Function to return
// the maximum number
static int maximumNumberDistinctPrimeRange(int m,
                                           int n)
{
    // array to store the
    // number of distinct primes
    long []factorCount = new long[n + 1];
 
    // true if index 'i'
    // is a prime
    bool []prime = new bool[n + 1];
 
    // initializing the number
    // of factors to 0 and
    for (int i = 0; i <= n; i++)
    {
        factorCount[i] = 0;
        prime[i] = true; // Used in Sieve
    }
 
    for (int i = 2; i <= n; i++)
    {
 
        // condition works only x
        // when 'i' is prime, hence
        // for factors of all prime
        // number, the prime status
        // is changed to false
        if (prime[i] == true)
        {
 
            // Number is prime
            factorCount[i] = 1;
 
            // number of factor of
            // a prime number is 1
            for (int j = i * 2;
                     j <= n; j += i)
            {
 
                // incrementing factorCount
                // all the factors of i
                factorCount[j]++;
 
                // and changing prime
                // status to false
                prime[j] = false;
            }
        }
    }
 
    // Initialize the max and num
    int max = (int)factorCount[m];
    int num = m;
 
    // Gets the maximum number
    for (int i = m; i <= n; i++)
    {
 
        // Gets the maximum number
        if (factorCount[i] > max)
        {
            max = (int)factorCount[i];
            num = i;
        }
    }
    return num;
}
 
// Driver code
public static void Main ()
{
int m = 4, n = 6;
 
// Calling function
Console.WriteLine(
         maximumNumberDistinctPrimeRange(m, n));
}
}
 
// This code is contributed
// by anuj_67.


PHP
 $max)
        {
            $max = $factorCount[$i];
            $num = $i;
        }
    }
    return $num;
}
 
// Driver code
$m = 4; $n = 6;
 
// Calling function
echo maximumNumberDistinctPrimeRange($m, $n);
 
// This code is contributed
// by anuj_67.
?>


Javascript


输出:
6
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”