📜  在O(n ^ 1/3)中计算n的除数

📅  最后修改于: 2021-05-04 23:19:10             🧑  作者: Mango

给定数字n,计算其所有除数。

例子:

Input : 18
Output : 6
Divisors of 18 are 1, 2, 3, 6, 9 and 18.

Input : 100
Output : 9
Divisors of 100 are 1, 2, 4, 5, 10, 20,
25, 50 and 100

一个幼稚的解决方案是将所有数字从1迭代到sqrt(n),检查该数字是否除以n并增加除数的数量。这种方法需要O(sqrt(n))时间。

C++
// C implementation of Naive method to count all
// divisors
#include 
using namespace std;
 
// function to count the divisors
int countDivisors(int n)
{
    int cnt = 0;
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
            // If divisors are equal,
            // count only one
            if (n / i == i)
                cnt++;
 
            else // Otherwise count both
                cnt = cnt + 2;
        }
    }
    return cnt;
}
 
/* Driver program to test above function */
int main()
{
    printf("Total distinct divisors of 100 are : %d",
           countDivisors(100));
    return 0;
}


Java
// JAVA implementation of Naive method
// to count all divisors
import java.io.*;
import java.math.*;
 
class GFG {
 
    // function to count the divisors
    static int countDivisors(int n)
    {
        int cnt = 0;
        for (int i = 1; i <= Math.sqrt(n); i++)
        {
            if (n % i == 0) {
                // If divisors are equal,
                // count only one
                if (n / i == i)
                    cnt++;
 
                else // Otherwise count both
                    cnt = cnt + 2;
            }
        }
        return cnt;
    }
 
    /* Driver program to test above function */
    public static void main(String args[])
    {
        System.out.println("Total distinct "
                  + "divisors of 100 are : " 
                       + countDivisors(100));
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python3 implementation of Naive method
# to count all divisors
 
import math
 
# function to count the divisors
def countDivisors(n) :
    cnt = 0
    for i in range(1, (int)(math.sqrt(n)) + 1) :
        if (n % i == 0) :
             
            # If divisors are equal,
            # count only one
            if (n / i == i) :
                cnt = cnt + 1
            else : # Otherwise count both
                cnt = cnt + 2
                 
    return cnt
     
# Driver program to test above function */
 
print("Total distinct divisors of 100 are : ",
      countDivisors(100))
 
# This code is contributed by Nikita Tiwari.


C#
// C# implementation of Naive method
// to count all divisors
using System;
 
class GFG {
 
    // function to count the divisors
    static int countDivisors(int n)
    {
        int cnt = 0;
        for (int i = 1; i <= Math.Sqrt(n);
                                      i++)
        {
            if (n % i == 0) {
                 
                // If divisors are equal,
                // count only one
                if (n / i == i)
                    cnt++;
 
                // Otherwise count both
                else
                    cnt = cnt + 2;
            }
        }
         
        return cnt;
    }
 
    // Driver program
    public static void Main()
    {
        Console.WriteLine("Total distinct"
               + " divisors of 100 are : "
                    + countDivisors(100));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
// C++ program to count distinct divisors
// of a given number n
#include 
using namespace std;
 
void SieveOfEratosthenes(int n, bool prime[],
                         bool primesquare[], int a[])
{
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    for (int i = 2; i <= n; i++)
        prime[i] = true;
 
    // Create a boolean array "primesquare[0..n*n+1]"
    // and initialize all entries it as false. A value
    // in squareprime[i] will finally be true if i is
    // square of prime, else false.
    for (int i = 0; i <= (n * n + 1); i++)
        primesquare[i] = false;
 
    // 1 is not a prime number
    prime[1] = false;
 
    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 multiples of p
            for (int i = p * 2; i <= n; i += p)
                prime[i] = false;
        }
    }
 
    int j = 0;
    for (int p = 2; p <= n; p++) {
        if (prime[p]) {
            // Storing primes in an array
            a[j] = p;
 
            // Update value in primesquare[p*p],
            // if p is prime.
            primesquare[p * p] = true;
            j++;
        }
    }
}
 
// Function to count divisors
int countDivisors(int n)
{
    // If number is 1, then it will have only 1
    // as a factor. So, total factors will be 1.
    if (n == 1)
        return 1;
 
    bool prime[n + 1], primesquare[n * n + 1];
 
    int a[n]; // for storing primes upto n
 
    // Calling SieveOfEratosthenes to store prime
    // factors of n and to store square of prime
    // factors of n
    SieveOfEratosthenes(n, prime, primesquare, a);
 
    // ans will contain total number of distinct
    // divisors
    int ans = 1;
 
    // Loop for counting factors of n
    for (int i = 0;; i++) {
        // a[i] is not less than cube root n
        if (a[i] * a[i] * a[i] > n)
            break;
 
        // Calculating power of a[i] in n.
        int cnt = 1; // cnt is power of prime a[i] in n.
        while (n % a[i] == 0) // if a[i] is a factor of n
        {
            n = n / a[i];
            cnt = cnt + 1; // incrementing power
        }
 
        // Calculating the number of divisors
        // If n = a^p * b^q then total divisors of n
        // are (p+1)*(q+1)
        ans = ans * cnt;
    }
 
    // if a[i] is greater than cube root of n
 
    // First case
    if (prime[n])
        ans = ans * 2;
 
    // Second case
    else if (primesquare[n])
        ans = ans * 3;
 
    // Third case
    else if (n != 1)
        ans = ans * 4;
 
    return ans; // Total divisors
}
 
// Driver Program
int main()
{
    cout << "Total distinct divisors of 100 are : "
         << countDivisors(100) << endl;
    return 0;
}


Java
// JAVA program to count distinct
// divisors of a given number n
import java.io.*;
 
class GFG {
 
    static void SieveOfEratosthenes(int n, boolean prime[],
                                    boolean primesquare[], int a[])
    {
        // Create a boolean array "prime[0..n]" and
        // initialize all entries it as true. A value
        // in prime[i] will finally be false if i is
        // Not a prime, else true.
        for (int i = 2; i <= n; i++)
            prime[i] = true;
 
        /* Create a boolean array "primesquare[0..n*n+1]"
         and initialize all entries it as false.
         A value in squareprime[i] will finally
         be true if i is square of prime,
         else false.*/
        for (int i = 0; i < ((n * n) + 1); i++)
            primesquare[i] = false;
 
        // 1 is not a prime number
        prime[1] = false;
 
        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 multiples of p
                for (int i = p * 2; i <= n; i += p)
                    prime[i] = false;
            }
        }
 
        int j = 0;
        for (int p = 2; p <= n; p++) {
            if (prime[p]) {
                // Storing primes in an array
                a[j] = p;
 
                // Update value in
                // primesquare[p*p],
                // if p is prime.
                primesquare[p * p] = true;
                j++;
            }
        }
    }
 
    // Function to count divisors
    static int countDivisors(int n)
    {
        // If number is 1, then it will
        // have only 1 as a factor. So,
        // total factors will be 1.
        if (n == 1)
            return 1;
 
        boolean prime[] = new boolean[n + 1];
        boolean primesquare[] = new boolean[(n * n) + 1];
 
        // for storing primes upto n
        int a[] = new int[n];
 
        // Calling SieveOfEratosthenes to
        // store prime factors of n and to
        // store square of prime factors of n
        SieveOfEratosthenes(n, prime, primesquare, a);
 
        // ans will contain total number
        // of distinct divisors
        int ans = 1;
 
        // Loop for counting factors of n
        for (int i = 0;; i++) {
            // a[i] is not less than cube root n
            if (a[i] * a[i] * a[i] > n)
                break;
 
            // Calculating power of a[i] in n.
            // cnt is power of prime a[i] in n.
            int cnt = 1;
 
            // if a[i] is a factor of n
            while (n % a[i] == 0) {
                n = n / a[i];
 
                // incrementing power
                cnt = cnt + 1;
            }
 
            // Calculating the number of divisors
            // If n = a^p * b^q then total
            // divisors of n are (p+1)*(q+1)
            ans = ans * cnt;
        }
 
        // if a[i] is greater than cube root
        // of n
 
        // First case
        if (prime[n])
            ans = ans * 2;
 
        // Second case
        else if (primesquare[n])
            ans = ans * 3;
 
        // Third case
        else if (n != 1)
            ans = ans * 4;
 
        return ans; // Total divisors
    }
 
    // Driver Program
    public static void main(String args[])
    {
        System.out.println("Total distinct divisors"
                           + " of 100 are : " + countDivisors(100));
    }
}
 
/*This code is contributed by Nikita Tiwari*/


Python3
# Python3 program to count distinct
# divisors of a given number n
 
def SieveOfEratosthenes(n, prime,primesquare, a):
    # Create a boolean array "prime[0..n]"
    # and initialize all entries it as
    # true. A value in prime[i] will finally
    # be false if i is not a prime, else true.
    for i in range(2,n+1):
        prime[i] = True
 
    # Create a boolean array "primesquare[0..n*n+1]"
    # and initialize all entries it as false.
    # A value in squareprime[i] will finally be
    # true if i is square of prime, else false.
    for i in range((n * n + 1)+1):
        primesquare[i] = False
 
    # 1 is not a prime number
    prime[1] = False
 
    p = 2
    while(p * p <= n):
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p] == True):
            # Update all multiples of p
            i = p * 2
            while(i <= n):
                prime[i] = False
                i += p
        p+=1
     
 
    j = 0
    for p in range(2,n+1):
        if (prime[p]==True):
            # Storing primes in an array
            a[j] = p
 
            # Update value in primesquare[p*p],
            # if p is prime.
            primesquare[p * p] = True
            j+=1
 
# Function to count divisors
def countDivisors(n):
    # If number is 1, then it will
    # have only 1 as a factor. So,
    # total factors will be 1.
    if (n == 1):
        return 1
 
    prime = [False]*(n + 2)
    primesquare = [False]*(n * n + 2)
     
    # for storing primes upto n
    a = [0]*n
 
    # Calling SieveOfEratosthenes to
    # store prime factors of n and to
    # store square of prime factors of n
    SieveOfEratosthenes(n, prime, primesquare, a)
 
    # ans will contain total
    # number of distinct divisors
    ans = 1
 
    # Loop for counting factors of n
    i=0
    while(1):
        # a[i] is not less than cube root n
        if(a[i] * a[i] * a[i] > n):
            break
 
        # Calculating power of a[i] in n.
        cnt = 1 # cnt is power of
                # prime a[i] in n.
        while (n % a[i] == 0): # if a[i] is a factor of n
            n = n / a[i]
            cnt = cnt + 1 # incrementing power
 
        # Calculating number of divisors
        # If n = a^p * b^q then total
        # divisors of n are (p+1)*(q+1)
        ans = ans * cnt
        i+=1
 
    # if a[i] is greater than
    # cube root of n
     
    n=int(n)
    # First case
    if (prime[n]==True):
        ans = ans * 2
 
    # Second case
    elif (primesquare[n]==True):
        ans = ans * 3
 
    # Third case
    elif (n != 1):
        ans = ans * 4
 
    return ans # Total divisors
 
# Driver Code
if __name__=='__main__':
    print("Total distinct divisors of 100 are :",countDivisors(100))
 
# This code is contributed
# by mits


C#
// C# program to count distinct
// divisors of a given number n
using System;
 
class GFG {
 
    static void SieveOfEratosthenes(int n, bool[] prime,
                                    bool[] primesquare, int[] a)
    {
 
        // Create a boolean array "prime[0..n]" and
        // initialize all entries it as true. A value
        // in prime[i] will finally be false if i is
        // Not a prime, else true.
        for (int i = 2; i <= n; i++)
            prime[i] = true;
 
        /* Create a boolean array "primesquare[0..n*n+1]"
        and initialize all entries it as false.
        A value in squareprime[i] will finally
        be true if i is square of prime,
        else false.*/
        for (int i = 0; i < ((n * n) + 1); i++)
            primesquare[i] = false;
 
        // 1 is not a prime number
        prime[1] = false;
 
        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 multiples of p
                for (int i = p * 2; i <= n; i += p)
                    prime[i] = false;
            }
        }
 
        int j = 0;
        for (int p = 2; p <= n; p++) {
            if (prime[p]) {
 
                // Storing primes in an array
                a[j] = p;
 
                // Update value in
                // primesquare[p*p],
                // if p is prime.
                primesquare[p * p] = true;
                j++;
            }
        }
    }
 
    // Function to count divisors
    static int countDivisors(int n)
    {
 
        // If number is 1, then it will
        // have only 1 as a factor. So,
        // total factors will be 1.
        if (n == 1)
            return 1;
 
        bool[] prime = new bool[n + 1];
        bool[] primesquare = new bool[(n * n) + 1];
 
        // for storing primes upto n
        int[] a = new int[n];
 
        // Calling SieveOfEratosthenes to
        // store prime factors of n and to
        // store square of prime factors of n
        SieveOfEratosthenes(n, prime, primesquare, a);
 
        // ans will contain total number
        // of distinct divisors
        int ans = 1;
 
        // Loop for counting factors of n
        for (int i = 0;; i++) {
 
            // a[i] is not less than cube root n
            if (a[i] * a[i] * a[i] > n)
                break;
 
            // Calculating power of a[i] in n.
            // cnt is power of prime a[i] in n.
            int cnt = 1;
 
            // if a[i] is a factor of n
            while (n % a[i] == 0) {
                n = n / a[i];
 
                // incrementing power
                cnt = cnt + 1;
            }
 
            // Calculating the number of divisors
            // If n = a^p * b^q then total
            // divisors of n are (p+1)*(q+1)
            ans = ans * cnt;
        }
 
        // if a[i] is greater than cube root
        // of n
 
        // First case
        if (prime[n])
            ans = ans * 2;
 
        // Second case
        else if (primesquare[n])
            ans = ans * 3;
 
        // Third case
        else if (n != 1)
            ans = ans * 4;
 
        return ans; // Total divisors
    }
 
    // Driver Program
    public static void Main()
    {
        Console.Write("Total distinct divisors"
                      + " of 100 are : " + countDivisors(100));
    }
}
 
// This code is contributed by parashar.


PHP
 $n)
            break;
 
        // Calculating power of a[i] in n.
        $cnt = 1; // cnt is power of
                  // prime a[i] in n.
        while ($n % $a[$i] == 0) // if a[i] is a
                                 // factor of n
        {
            $n = $n / $a[$i];
            $cnt = $cnt + 1; // incrementing power
        }
 
        // Calculating the number of divisors
        // If n = a^p * b^q then total
        // divisors of n are (p+1)*(q+1)
        $ans = $ans * $cnt;
    }
 
    // if a[i] is greater than
    // cube root of n
 
    // First case
    if ($prime[$n])
        $ans = $ans * 2;
 
    // Second case
    else if ($primesquare[$n])
        $ans = $ans * 3;
 
    // Third case
    else if ($n != 1)
        $ans = $ans * 4;
 
    return $ans; // Total divisors
}
 
// Driver Code
echo "Total distinct divisors of 100 are : ".
                    countDivisors(100). "\n";
 
// This code is contributed
// by ChitraNayal
?>


Javascript


输出 :

Total distinct divisors of 100 are : 9

优化解(O(n ^ 1/3))

  1. 分割数n在两个数x和y使得n = X * Y,其中x包含范围2 <= X <= N(1/3)中,用较高的素因子大于nÿ只涉及素数因子(1/3 )
  2. 使用朴素的试验除法计算x的总因子。令此计数为F(x)。
  3. 使用以下三种情况计算y的总因子。将此计数设为F(y)。
    • 如果y是素数,则因数将是1,y本身。这意味着F(y)= 2。
    • 如果y是素数的平方,则因数将为1,sqrt(y)和y本身。这意味着F(y)= 3。
    • 如果y是两个不同质数的乘积,则因子将为1,质数和y本身。这意味着F(y)= 4。
  4. 由于F(x * y)是一个乘法函数并且gcd(x,y)= 1,这意味着F(x * y)= F(x)* F(y)给出n的总不同因数的数量。

注意,由于只有最大的两个y因子,所以只有这三种情况可以计算y因子。如果它有两个以上的质数因子,那么其中一个肯定会小于等于n (1/3) ,因此它将包含在x中而不是y中。

C++

// C++ program to count distinct divisors
// of a given number n
#include 
using namespace std;
 
void SieveOfEratosthenes(int n, bool prime[],
                         bool primesquare[], int a[])
{
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    for (int i = 2; i <= n; i++)
        prime[i] = true;
 
    // Create a boolean array "primesquare[0..n*n+1]"
    // and initialize all entries it as false. A value
    // in squareprime[i] will finally be true if i is
    // square of prime, else false.
    for (int i = 0; i <= (n * n + 1); i++)
        primesquare[i] = false;
 
    // 1 is not a prime number
    prime[1] = false;
 
    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 multiples of p
            for (int i = p * 2; i <= n; i += p)
                prime[i] = false;
        }
    }
 
    int j = 0;
    for (int p = 2; p <= n; p++) {
        if (prime[p]) {
            // Storing primes in an array
            a[j] = p;
 
            // Update value in primesquare[p*p],
            // if p is prime.
            primesquare[p * p] = true;
            j++;
        }
    }
}
 
// Function to count divisors
int countDivisors(int n)
{
    // If number is 1, then it will have only 1
    // as a factor. So, total factors will be 1.
    if (n == 1)
        return 1;
 
    bool prime[n + 1], primesquare[n * n + 1];
 
    int a[n]; // for storing primes upto n
 
    // Calling SieveOfEratosthenes to store prime
    // factors of n and to store square of prime
    // factors of n
    SieveOfEratosthenes(n, prime, primesquare, a);
 
    // ans will contain total number of distinct
    // divisors
    int ans = 1;
 
    // Loop for counting factors of n
    for (int i = 0;; i++) {
        // a[i] is not less than cube root n
        if (a[i] * a[i] * a[i] > n)
            break;
 
        // Calculating power of a[i] in n.
        int cnt = 1; // cnt is power of prime a[i] in n.
        while (n % a[i] == 0) // if a[i] is a factor of n
        {
            n = n / a[i];
            cnt = cnt + 1; // incrementing power
        }
 
        // Calculating the number of divisors
        // If n = a^p * b^q then total divisors of n
        // are (p+1)*(q+1)
        ans = ans * cnt;
    }
 
    // if a[i] is greater than cube root of n
 
    // First case
    if (prime[n])
        ans = ans * 2;
 
    // Second case
    else if (primesquare[n])
        ans = ans * 3;
 
    // Third case
    else if (n != 1)
        ans = ans * 4;
 
    return ans; // Total divisors
}
 
// Driver Program
int main()
{
    cout << "Total distinct divisors of 100 are : "
         << countDivisors(100) << endl;
    return 0;
}

Java

// JAVA program to count distinct
// divisors of a given number n
import java.io.*;
 
class GFG {
 
    static void SieveOfEratosthenes(int n, boolean prime[],
                                    boolean primesquare[], int a[])
    {
        // Create a boolean array "prime[0..n]" and
        // initialize all entries it as true. A value
        // in prime[i] will finally be false if i is
        // Not a prime, else true.
        for (int i = 2; i <= n; i++)
            prime[i] = true;
 
        /* Create a boolean array "primesquare[0..n*n+1]"
         and initialize all entries it as false.
         A value in squareprime[i] will finally
         be true if i is square of prime,
         else false.*/
        for (int i = 0; i < ((n * n) + 1); i++)
            primesquare[i] = false;
 
        // 1 is not a prime number
        prime[1] = false;
 
        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 multiples of p
                for (int i = p * 2; i <= n; i += p)
                    prime[i] = false;
            }
        }
 
        int j = 0;
        for (int p = 2; p <= n; p++) {
            if (prime[p]) {
                // Storing primes in an array
                a[j] = p;
 
                // Update value in
                // primesquare[p*p],
                // if p is prime.
                primesquare[p * p] = true;
                j++;
            }
        }
    }
 
    // Function to count divisors
    static int countDivisors(int n)
    {
        // If number is 1, then it will
        // have only 1 as a factor. So,
        // total factors will be 1.
        if (n == 1)
            return 1;
 
        boolean prime[] = new boolean[n + 1];
        boolean primesquare[] = new boolean[(n * n) + 1];
 
        // for storing primes upto n
        int a[] = new int[n];
 
        // Calling SieveOfEratosthenes to
        // store prime factors of n and to
        // store square of prime factors of n
        SieveOfEratosthenes(n, prime, primesquare, a);
 
        // ans will contain total number
        // of distinct divisors
        int ans = 1;
 
        // Loop for counting factors of n
        for (int i = 0;; i++) {
            // a[i] is not less than cube root n
            if (a[i] * a[i] * a[i] > n)
                break;
 
            // Calculating power of a[i] in n.
            // cnt is power of prime a[i] in n.
            int cnt = 1;
 
            // if a[i] is a factor of n
            while (n % a[i] == 0) {
                n = n / a[i];
 
                // incrementing power
                cnt = cnt + 1;
            }
 
            // Calculating the number of divisors
            // If n = a^p * b^q then total
            // divisors of n are (p+1)*(q+1)
            ans = ans * cnt;
        }
 
        // if a[i] is greater than cube root
        // of n
 
        // First case
        if (prime[n])
            ans = ans * 2;
 
        // Second case
        else if (primesquare[n])
            ans = ans * 3;
 
        // Third case
        else if (n != 1)
            ans = ans * 4;
 
        return ans; // Total divisors
    }
 
    // Driver Program
    public static void main(String args[])
    {
        System.out.println("Total distinct divisors"
                           + " of 100 are : " + countDivisors(100));
    }
}
 
/*This code is contributed by Nikita Tiwari*/

Python3

# Python3 program to count distinct
# divisors of a given number n
 
def SieveOfEratosthenes(n, prime,primesquare, a):
    # Create a boolean array "prime[0..n]"
    # and initialize all entries it as
    # true. A value in prime[i] will finally
    # be false if i is not a prime, else true.
    for i in range(2,n+1):
        prime[i] = True
 
    # Create a boolean array "primesquare[0..n*n+1]"
    # and initialize all entries it as false.
    # A value in squareprime[i] will finally be
    # true if i is square of prime, else false.
    for i in range((n * n + 1)+1):
        primesquare[i] = False
 
    # 1 is not a prime number
    prime[1] = False
 
    p = 2
    while(p * p <= n):
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p] == True):
            # Update all multiples of p
            i = p * 2
            while(i <= n):
                prime[i] = False
                i += p
        p+=1
     
 
    j = 0
    for p in range(2,n+1):
        if (prime[p]==True):
            # Storing primes in an array
            a[j] = p
 
            # Update value in primesquare[p*p],
            # if p is prime.
            primesquare[p * p] = True
            j+=1
 
# Function to count divisors
def countDivisors(n):
    # If number is 1, then it will
    # have only 1 as a factor. So,
    # total factors will be 1.
    if (n == 1):
        return 1
 
    prime = [False]*(n + 2)
    primesquare = [False]*(n * n + 2)
     
    # for storing primes upto n
    a = [0]*n
 
    # Calling SieveOfEratosthenes to
    # store prime factors of n and to
    # store square of prime factors of n
    SieveOfEratosthenes(n, prime, primesquare, a)
 
    # ans will contain total
    # number of distinct divisors
    ans = 1
 
    # Loop for counting factors of n
    i=0
    while(1):
        # a[i] is not less than cube root n
        if(a[i] * a[i] * a[i] > n):
            break
 
        # Calculating power of a[i] in n.
        cnt = 1 # cnt is power of
                # prime a[i] in n.
        while (n % a[i] == 0): # if a[i] is a factor of n
            n = n / a[i]
            cnt = cnt + 1 # incrementing power
 
        # Calculating number of divisors
        # If n = a^p * b^q then total
        # divisors of n are (p+1)*(q+1)
        ans = ans * cnt
        i+=1
 
    # if a[i] is greater than
    # cube root of n
     
    n=int(n)
    # First case
    if (prime[n]==True):
        ans = ans * 2
 
    # Second case
    elif (primesquare[n]==True):
        ans = ans * 3
 
    # Third case
    elif (n != 1):
        ans = ans * 4
 
    return ans # Total divisors
 
# Driver Code
if __name__=='__main__':
    print("Total distinct divisors of 100 are :",countDivisors(100))
 
# This code is contributed
# by mits

C#

// C# program to count distinct
// divisors of a given number n
using System;
 
class GFG {
 
    static void SieveOfEratosthenes(int n, bool[] prime,
                                    bool[] primesquare, int[] a)
    {
 
        // Create a boolean array "prime[0..n]" and
        // initialize all entries it as true. A value
        // in prime[i] will finally be false if i is
        // Not a prime, else true.
        for (int i = 2; i <= n; i++)
            prime[i] = true;
 
        /* Create a boolean array "primesquare[0..n*n+1]"
        and initialize all entries it as false.
        A value in squareprime[i] will finally
        be true if i is square of prime,
        else false.*/
        for (int i = 0; i < ((n * n) + 1); i++)
            primesquare[i] = false;
 
        // 1 is not a prime number
        prime[1] = false;
 
        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 multiples of p
                for (int i = p * 2; i <= n; i += p)
                    prime[i] = false;
            }
        }
 
        int j = 0;
        for (int p = 2; p <= n; p++) {
            if (prime[p]) {
 
                // Storing primes in an array
                a[j] = p;
 
                // Update value in
                // primesquare[p*p],
                // if p is prime.
                primesquare[p * p] = true;
                j++;
            }
        }
    }
 
    // Function to count divisors
    static int countDivisors(int n)
    {
 
        // If number is 1, then it will
        // have only 1 as a factor. So,
        // total factors will be 1.
        if (n == 1)
            return 1;
 
        bool[] prime = new bool[n + 1];
        bool[] primesquare = new bool[(n * n) + 1];
 
        // for storing primes upto n
        int[] a = new int[n];
 
        // Calling SieveOfEratosthenes to
        // store prime factors of n and to
        // store square of prime factors of n
        SieveOfEratosthenes(n, prime, primesquare, a);
 
        // ans will contain total number
        // of distinct divisors
        int ans = 1;
 
        // Loop for counting factors of n
        for (int i = 0;; i++) {
 
            // a[i] is not less than cube root n
            if (a[i] * a[i] * a[i] > n)
                break;
 
            // Calculating power of a[i] in n.
            // cnt is power of prime a[i] in n.
            int cnt = 1;
 
            // if a[i] is a factor of n
            while (n % a[i] == 0) {
                n = n / a[i];
 
                // incrementing power
                cnt = cnt + 1;
            }
 
            // Calculating the number of divisors
            // If n = a^p * b^q then total
            // divisors of n are (p+1)*(q+1)
            ans = ans * cnt;
        }
 
        // if a[i] is greater than cube root
        // of n
 
        // First case
        if (prime[n])
            ans = ans * 2;
 
        // Second case
        else if (primesquare[n])
            ans = ans * 3;
 
        // Third case
        else if (n != 1)
            ans = ans * 4;
 
        return ans; // Total divisors
    }
 
    // Driver Program
    public static void Main()
    {
        Console.Write("Total distinct divisors"
                      + " of 100 are : " + countDivisors(100));
    }
}
 
// This code is contributed by parashar.

的PHP

 $n)
            break;
 
        // Calculating power of a[i] in n.
        $cnt = 1; // cnt is power of
                  // prime a[i] in n.
        while ($n % $a[$i] == 0) // if a[i] is a
                                 // factor of n
        {
            $n = $n / $a[$i];
            $cnt = $cnt + 1; // incrementing power
        }
 
        // Calculating the number of divisors
        // If n = a^p * b^q then total
        // divisors of n are (p+1)*(q+1)
        $ans = $ans * $cnt;
    }
 
    // if a[i] is greater than
    // cube root of n
 
    // First case
    if ($prime[$n])
        $ans = $ans * 2;
 
    // Second case
    else if ($primesquare[$n])
        $ans = $ans * 3;
 
    // Third case
    else if ($n != 1)
        $ans = $ans * 4;
 
    return $ans; // Total divisors
}
 
// Driver Code
echo "Total distinct divisors of 100 are : ".
                    countDivisors(100). "\n";
 
// This code is contributed
// by ChitraNayal
?>

Java脚本


输出 :

Total distinct divisors of 100 are : 9

时间复杂度: O(n 1/3 )