📜  n个因素中没有!

📅  最后修改于: 2021-05-06 22:12:40             🧑  作者: Mango

给定一个正整数n,在n中找出所有因子!其中n <= 10 5
例子 :

Input : n = 3
Output : 4
Factors of 3! are 1, 2, 3, 6

Input : n = 4
Output : 8
Factors of 4! are 1, 2, 3, 4, 
                 6, 8, 12, 24
                  
Input : n = 16
Output : 5376

请注意,暴力破解方法甚至无法在这里使用,因为我们找不到n!对于这么大的我们需要一种更现实的方法来解决此问题。

这个想法是基于勒让德的公式。
任何正整数都可以表示为其素因数的乘积。假设一个数n = p 1 a1 xp 2 a2 xp 3 a3 ,…。,p k ak其中p 1 ,p 2 ,p 3 ,…。,p k是不同的素数,a1,a2,a3,………… ..,ak是它们各自的指数。
那么n的除数的个数=(a1 + 1)x(a2 + 1)x(a3 + 1)… x(ak + 1)
因此,没有。的n个因子!现在,可以通过首先找到素因子直到n,然后计算它们各自的指数来轻松地计算出。
我们算法的主要步骤是:

  1. 从p = 1迭代到p = n,并在每次迭代时检查p是否为质数。
  2. 如果p是素数,则意味着它是n的素数!因此我们在n中找到p的指数!这是
  3. 在找到所有主要因子的各个指数后,假设它们分别是a1,a2,a3,….,ak,然后是n! =(a1 + 1)x(a2 + 1)x(a3 + 1)……………(ak + 1)
Here is an illustration on how the algorithm works 
for finding factors of 16!:

All prime less than 16 will be the prime factors of 16!. 
so instead of finding all the prime factor of 16!.
we just need to find the primes less than 16 that will 
automatically be the prime factors of 16!

Prime factors of 16! are: 2,3,5,7,11,13

Now to the exponent of 2 in 16!  
              = ⌊16/2⌋+ ⌊16/4⌋+ ⌊16/8⌋ + ⌊16/16⌋ 
              = 8 + 4 + 2 + 1
              = 15

Similarly, 
   exponent of 3 in 16! =  ⌊16/3⌋ + ⌊16/9⌋ = 6
   exponent of 5 in 16! = 3 
   exponent of 7 in 16! = 2
   exponent of 11 in 16! = 1
   exponent of 13 in 16! = 1

So, the no of factors of 16! 
         = (15+1) * (6+1) * (3+1) *(2+1)* (1+1) * (1+1)
         = 5376 

下面是上述想法的实现:

C++
// C++ program to count number of factors of n
#include 
using namespace std;
typedef long long int ll;
 
// Sieve of Eratosthenes to mark all prime number
// in array prime as 1
void sieve(int n, bool prime[])
{
    // Initialize all numbers as prime
    for (int i=1; i<=n; i++)
        prime[i] = 1;
 
    // Mark composites
    prime[1] = 0;
    for (int i=2; i*i<=n; i++)
    {
        if (prime[i])
        {
            for (int j=i*i; j<=n; j += i)
                prime[j] = 0;
        }
    }
}
 
// Returns the highest exponent of p in n!
int expFactor(int n, int p)
{
    int x = p;
    int exponent = 0;
    while ((n/x) > 0)
    {
        exponent += n/x;
        x *= p;
    }
    return exponent;
}
 
// Returns the no of factors in n!
ll countFactors(int n)
{
    // ans stores the no of factors in n!
    ll ans = 1;
 
    // Find all primes upto n
    bool prime[n+1];
    sieve(n, prime);
 
    // Multiply exponent (of primes) added with 1
    for (int p=1; p<=n; p++)
    {
        // if p is a prime then p is also a
        // prime factor of n!
        if (prime[p]==1)
            ans *= (expFactor(n, p) + 1);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int n = 16;
    printf("Count of factors of %d! is %lld\n",
                                n, countFactors(n));
    return 0;
}


Java
// Java program to count number of factors of n
import java.io.*;
class GFG {
 
    // Sieve of Eratosthenes to mark all prime number
    // in array prime as 1
    static void sieve(int n, int prime[])
    {
        // Initialize all numbers as prime
        for (int i = 1; i <= n; i++)
            prime[i] = 1;
 
        // Mark composites
        prime[1] = 0;
        for (int i = 2; i * i <= n; i++) {
            if (prime[i] == 1) {
                for (int j = i * i; j <= n; j += i)
                    prime[j] = 0;
            }
        }
    }
 
    // Returns the highest exponent of p in n!
    static int expFactor(int n, int p)
    {
        int x = p;
        int exponent = 0;
        while ((n / x) > 0) {
            exponent += n / x;
            x *= p;
        }
        return exponent;
    }
 
    // Returns the no of factors in n!
    static long countFactors(int n)
    {
        // ans stores the no of factors in n!
        long ans = 1;
 
        // Find all primes upto n
        int prime[] = new int[n + 1];
        sieve(n, prime);
 
        // Multiply exponent (of primes) added with 1
        for (int p = 1; p <= n; p++) {
 
            // if p is a prime then p is also a
            // prime factor of n!
            if (prime[p] == 1)
                ans *= (expFactor(n, p) + 1);
        }
 
        return ans;
    }
 
    // Driver code
     public static void main(String args[])
    {
        int n = 16;
        System.out.println("Count of factors of " +
                       n + " is " + countFactors(n));
    }
}
// This code is contributed by Nikita Tiwari


Python 3
# python 3 program to count
# number of factors of n
 
# Returns the highest
# exponent of p in n!
def expFactor(n, p):
    x = p
    exponent = 0
    while n // x > 0:
     
        exponent += n // x
        x *= p
    return exponent
 
# Returns the no
# of factors in n!
def countFactors(n):
 
    # ans stores the no
    # of factors in n!
    ans = 1
 
    # Find all primes upto n
    prime = [None]*(n+1)
     
    # Initialize all
    # numbers as prime
    for i in range(1,n+1):
        prime[i] = 1
 
    # Mark composites
    prime[1] = 0
    i = 2
    while i * i <= n:
     
        if (prime[i]):
         
            for j in range(i * i,n+1,i):
                prime[j] = 0
        i += 1
 
    # Multiply exponent (of
    # primes) added with 1
    for p in range(1,n+1):
     
        # if p is a prime then p
        # is also a prime factor of n!
        if (prime[p] == 1):
            ans *= (expFactor(n, p) + 1)
 
    return ans
 
# Driver Code
if __name__=='__main__':
    n = 16
    print("Count of factors of " + str(n) +
         "! is " +str( countFactors(n)))
          
# This code is contributed by ChitraNayal


C#
// C# program to count number
// of factors of n
using System;
 
class GFG {
 
    // Sieve of Eratosthenes to mark all
    // prime number in array prime as 1
    static void sieve(int n, int []prime)
    {
         
        // Initialize all numbers as prime
        for (int i = 1; i <= n; i++)
            prime[i] = 1;
 
        // Mark composites
        prime[1] = 0;
        for (int i = 2; i * i <= n; i++)
        {
            if (prime[i] == 1)
            {
                for (int j = i * i; j <= n; j += i)
                    prime[j] = 0;
            }
        }
    }
 
    // Returns the highest exponent of p in n!
    static int expFactor(int n, int p)
    {
        int x = p;
        int exponent = 0;
        while ((n / x) > 0)
        {
            exponent += n / x;
            x *= p;
        }
        return exponent;
    }
 
    // Returns the no of factors in n!
    static long countFactors(int n)
    {
        // ans stores the no of factors in n!
        long ans = 1;
 
        // Find all primes upto n
        int []prime = new int[n + 1];
        sieve(n, prime);
 
        // Multiply exponent (of primes)
        // added with 1
        for (int p = 1; p <= n; p++)
        {
 
            // if p is a prime then p is
            // also a prime factor of n!
            if (prime[p] == 1)
                ans *= (expFactor(n, p) + 1);
        }
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 16;
        Console.Write("Count of factors of " +
                       n + " is " + countFactors(n));
    }
}
 
// This code is contributed by Nitin Mittal.


PHP
 0)
    {
        $exponent += intval($n / $x);
        $x *= $p;
    }
    return $exponent;
}
 
// Returns the no
// of factors in n!
function countFactors($n)
{
    // ans stores the no
    // of factors in n!
    $ans = 1;
 
    // Find all primes upto n
    $prime = array();
     
    // Initialize all
    // numbers as prime
    for ($i = 1; $i <= $n; $i++)
        $prime[$i] = 1;
 
    // Mark composites
    $prime[1] = 0;
    for ($i = 2; $i * $i <= $n; $i++)
    {
        if ($prime[$i])
        {
            for ($j = $i * $i; $j <= $n; $j += $i)
                $prime[$j] = 0;
        }
    }
 
    // Multiply exponent (of
    // primes) added with 1
    for ($p = 1; $p <= $n; $p++)
    {
        // if p is a prime then p
        // is also a prime factor of n!
        if ($prime[$p] == 1)
            $ans *= intval(expFactor($n, $p) + 1);
    }
 
    return $ans;
}
 
// Driver Code
$n = 16;
echo "Count of factors of " . $n .
       "! is " . countFactors($n);
     
// This code is contributed by Sam007
?>


Javascript


输出 :

Count of factors of 16! is 5376