📜  计算N的素因子!

📅  最后修改于: 2021-04-17 18:02:36             🧑  作者: Mango

给定整数N ,任务是计算N的素数个数

例子:

天真的方法:请按照以下步骤解决问题:

  1. 初始化一个变量,例如fac ,以存储数字的阶乘。
  2. 初始化一个变量,例如count ,以计算N的素数
  3. 迭代范围[2,fac] ,如果数字不是素数,则递增count
  4. 打印计数作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate
// factorial of a number
int factorial(int f)
{
    // Base Case
    if (f == 0 || f == 1) {
        return 1;
    }
    else {
 
        // Recursive call
        return (f * factorial(f - 1));
    }
}
 
// Function to check if a
// number is prime or not
bool isPrime(int element)
{
    for (int i = 2;
         i <= sqrt(element); i++) {
        if (element % i == 0) {
 
            // Not prime
            return false;
        }
    }
 
    // Is prime
    return true;
}
 
// Function to count the number
// of prime factors of N!
int countPrimeFactors(int N)
{
    // Stores factorial of N
    int fac = factorial(N);
 
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Iterate over the rage [2, fac]
    for (int i = 2; i <= fac; i++) {
 
        // If not prime
        if (fac % i == 0 && isPrime(i)) {
 
            // Increment count
            count++;
        }
    }
 
    // Print the count
    cout << count;
}
 
// Driver Code
int main()
{
    // Given value of N
    int N = 5;
 
    // Function call to count the
    // number of prime factors of N
    countPrimeFactors(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
  // Function to calculate
  // factorial of a number
  static int factorial(int f)
  {
    // Base Case
    if (f == 0 || f == 1) {
      return 1;
    }
    else {
 
      // Recursive call
      return (f * factorial(f - 1));
    }
  }
 
  // Function to check if a
  // number is prime or not
  static boolean isPrime(int element)
  {
    for (int i = 2;
         i <= (int)Math.sqrt(element); i++) {
      if (element % i == 0) {
 
        // Not prime
        return false;
      }
    }
 
    // Is prime
    return true;
  }
 
  // Function to count the number
  // of prime factors of N!
  static void countPrimeFactors(int N)
  {
    // Stores factorial of N
    int fac = factorial(N);
 
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Iterate over the rage [2, fac]
    for (int i = 2; i <= fac; i++) {
 
      // If not prime
      if ((fac % i == 0 && isPrime(i))) {
 
        // Increment count
        count++;
      }
    }
 
    // Print the count
    System.out.println(count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Given value of N
    int N = 5;
 
    // Function call to count the
    // number of prime factors of N
    countPrimeFactors(N);
  }
}
 
// This code is contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
 
class GFG
{
 
  // Function to calculate
  // factorial of a number
  static int factorial(int f)
  {
     
    // Base Case
    if (f == 0 || f == 1) {
      return 1;
    }
    else {
  
      // Recursive call
      return (f * factorial(f - 1));
    }
  }
  
  // Function to check if a
  // number is prime or not
  static bool isPrime(int element)
  {
    for (int i = 2;
         i <= (int)Math.Sqrt(element); i++) {
      if (element % i == 0) {
  
        // Not prime
        return false;
      }
    }
  
    // Is prime
    return true;
  }
  
  // Function to count the number
  // of prime factors of N!
  static void countPrimeFactors(int N)
  {
     
    // Stores factorial of N
    int fac = factorial(N);
  
    // Stores the count of
    // prime factors
    int count = 0;
  
    // Iterate over the rage [2, fac]
    for (int i = 2; i <= fac; i++) {
  
      // If not prime
      if ((fac % i == 0 && isPrime(i))) {
  
        // Increment count
        count++;
      }
    }
  
    // Print the count
    Console.Write(count);
  }
 
 
// Driver Code
public static void Main()
{
   
    // Given value of N
    int N = 5;
  
    // Function call to count the
    // number of prime factors of N
    countPrimeFactors(N);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python program for the above approach
from math import sqrt
 
# Function to calculate
# factorial of a number
def factorial(f):
     
    # Base Case
    if (f == 0 or f == 1):
        return 1
    else:
       
        # Recursive call
        return (f * factorial(f - 1))
         
# Function to check if a
# number is prime or not
def isPrime(element):
     
    for i in range(2,int(sqrt(element))+1):
        if (element % i == 0):
           
            # Not prime
            return False
     
    # Is prime
    return True
 
# Function to count the number
# of prime factors of N!
def countPrimeFactors(N):
     
    # Stores factorial of N
    fac = factorial(N)
     
    # Stores the count of
    # prime factors
    count = 0
     
    # Iterate over the rage [2, fac]
    for i in range(2, fac + 1):
         
        # If not prime
        if (fac % i == 0 and isPrime(i)):
             
            # Increment count
            count += 1
             
    # Prthe count
    print(count)
 
# Driver Code
# Given value of N
N = 5
 
# Function call to count the
# number of prime factors of N
countPrimeFactors(N)
 
# This code is contributed by shubhamsingh10


C++
// C++ approach for the above approach
 
#include 
using namespace std;
 
// Function to count the
// prime factors of N!
int countPrimeFactors(int N)
{
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Stores whether a number
    // is prime or not
    bool prime[N + 1];
 
    // Mark all as true initially
    memset(prime, true, sizeof(prime));
 
    // Sieve of Eratosthenes
    for (int p = 2; p * p <= N; p++) {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true) {
 
            // Update all subsequent multiples
            for (int i = p * p; i <= N; i += p)
                prime[i] = false;
        }
    }
 
    // Traverse in the range [2, N]
    for (int p = 2; p <= N; p++) {
 
        // If prime
        if (prime[p]) {
 
            // Increment the count
            count++;
        }
    }
 
    // Print the count
    cout << count;
}
 
// Driver Code
int main()
{
    // Given  value of N
    int N = 5;
 
    // Function call to count
    // the prime factors of N!
    countPrimeFactors(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to count the
  // prime factors of N!
  static void countPrimeFactors(int N)
  {
     
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Stores whether a number
    // is prime or not
    boolean[] prime = new boolean[N + 1];
 
    // Mark all as true initially
    Arrays.fill(prime, true);
 
    // Sieve of Eratosthenes
    for (int p = 2; p * p <= N; p++) {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime[p] == true) {
 
        // Update all subsequent multiples
        for (int i = p * p; i <= N; i += p)
          prime[i] = false;
      }
    }
 
    // Traverse in the range [2, N]
    for (int p = 2; p <= N; p++) {
 
      // If prime
      if (prime[p] != false) {
 
        // Increment the count
        count++;
      }
    }
 
    // Print the count
    System.out.print(count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Given  value of N
    int N = 5;
 
    // Function call to count
    // the prime factors of N!
    countPrimeFactors(N);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to count the
  // prime factors of N!
  static void countPrimeFactors(int N)
  {
     
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Stores whether a number
    // is prime or not
    bool[] prime = new bool[N + 1];
 
    // Mark all as true initially
    for (int i = 0; i < prime.Length; i++)
        prime[i] = true;
 
    // Sieve of Eratosthenes
    for (int p = 2; p * p <= N; p++) {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime[p] == true) {
 
        // Update all subsequent multiples
        for (int i = p * p; i <= N; i += p)
          prime[i] = false;
      }
    }
 
    // Traverse in the range [2, N]
    for (int p = 2; p <= N; p++) {
 
      // If prime
      if (prime[p] != false) {
 
        // Increment the count
        count++;
      }
    }
 
    // Print the count
    Console.Write(count);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
     
    // Given  value of N
    int N = 5;
 
    // Function call to count
    // the prime factors of N!
    countPrimeFactors(N);
  }
}
 
// This code is contributed by shikhasingrajput


输出:
3

时间复杂度: O(N!* sqrt(N))
辅助空间: O(1)

高效方法:为了优化上述方法,我们的想法是使用Eratosthenes筛网。请按照以下步骤解决问题:

  1. 初始化一个变量,例如count ,以存储N个素数的计数
  2. 初始化一个布尔数组,例如说prime [],以检查数字是否为质数。
  3. 如果发现质,执行Eratosthenes筛分并在每次迭代中填充计数
  4. 打印计数值作为答案。

下面是上述方法的实现:

C++

// C++ approach for the above approach
 
#include 
using namespace std;
 
// Function to count the
// prime factors of N!
int countPrimeFactors(int N)
{
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Stores whether a number
    // is prime or not
    bool prime[N + 1];
 
    // Mark all as true initially
    memset(prime, true, sizeof(prime));
 
    // Sieve of Eratosthenes
    for (int p = 2; p * p <= N; p++) {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true) {
 
            // Update all subsequent multiples
            for (int i = p * p; i <= N; i += p)
                prime[i] = false;
        }
    }
 
    // Traverse in the range [2, N]
    for (int p = 2; p <= N; p++) {
 
        // If prime
        if (prime[p]) {
 
            // Increment the count
            count++;
        }
    }
 
    // Print the count
    cout << count;
}
 
// Driver Code
int main()
{
    // Given  value of N
    int N = 5;
 
    // Function call to count
    // the prime factors of N!
    countPrimeFactors(N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to count the
  // prime factors of N!
  static void countPrimeFactors(int N)
  {
     
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Stores whether a number
    // is prime or not
    boolean[] prime = new boolean[N + 1];
 
    // Mark all as true initially
    Arrays.fill(prime, true);
 
    // Sieve of Eratosthenes
    for (int p = 2; p * p <= N; p++) {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime[p] == true) {
 
        // Update all subsequent multiples
        for (int i = p * p; i <= N; i += p)
          prime[i] = false;
      }
    }
 
    // Traverse in the range [2, N]
    for (int p = 2; p <= N; p++) {
 
      // If prime
      if (prime[p] != false) {
 
        // Increment the count
        count++;
      }
    }
 
    // Print the count
    System.out.print(count);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Given  value of N
    int N = 5;
 
    // Function call to count
    // the prime factors of N!
    countPrimeFactors(N);
  }
}
 
// This code is contributed by susmitakundugoaldanga.

C#

// C# program for the above approach
using System;
public class GFG
{
 
  // Function to count the
  // prime factors of N!
  static void countPrimeFactors(int N)
  {
     
    // Stores the count of
    // prime factors
    int count = 0;
 
    // Stores whether a number
    // is prime or not
    bool[] prime = new bool[N + 1];
 
    // Mark all as true initially
    for (int i = 0; i < prime.Length; i++)
        prime[i] = true;
 
    // Sieve of Eratosthenes
    for (int p = 2; p * p <= N; p++) {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime[p] == true) {
 
        // Update all subsequent multiples
        for (int i = p * p; i <= N; i += p)
          prime[i] = false;
      }
    }
 
    // Traverse in the range [2, N]
    for (int p = 2; p <= N; p++) {
 
      // If prime
      if (prime[p] != false) {
 
        // Increment the count
        count++;
      }
    }
 
    // Print the count
    Console.Write(count);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
     
    // Given  value of N
    int N = 5;
 
    // Function call to count
    // the prime factors of N!
    countPrimeFactors(N);
  }
}
 
// This code is contributed by shikhasingrajput

输出:
3

时间复杂度: O(N * log(logN))
辅助空间: O(N)