📌  相关文章
📜  计算一个数字的所有完美除数

📅  最后修改于: 2021-04-23 15:53:04             🧑  作者: Mango

给定数字n,计算n的总完美除数。完美除数是那些整数的除数。例如8的完美除数是4。
例子:

Input  : n = 16 
Output : 3
Explanation : There are only 5 divisor of 16:
1, 2, 4, 8, 16. Only three of them are perfect 
squares: 1, 4, 16. Therefore the answer is 3

Input  : n = 7
Output : 1

天真的方法

蛮力找到一个数字的所有除数。计算所有平方为完美平方的除数。

C++
// Below is C++ code to count total perfect Divisors
#include
using namespace std;
 
// Utility function to check perfect square number
bool isPerfectSquare(int n)
{
    int sq = (int) sqrt(n);
    return (n == sq * sq);
}
 
// Returns count all perfect divisors of n
int countPerfectDivisors(int n)
{
    // Initialize result
    int count = 0;
 
    // Consider every number that can be a divisor
    // of n
    for (int i=1; i*i <= n; ++i)
    {
        // If i is a divisor
        if (n%i == 0)
        {
            if (isPerfectSquare(i))
                ++count;
            if (n/i != i && isPerfectSquare(n/i))
                ++count;
        }
    }
    return count;
}
 
// Driver code
int main()
{
    int n = 16;
 
    cout << "Total perfect divisors of "
         << n << " = " << countPerfectDivisors(n) << "\n";
 
    n = 12;
    cout << "Total perfect divisors of "
         << n << " = " << countPerfectDivisors(n);
 
    return 0;
}


Java
// Java code to count
// total perfect Divisors
import java.io.*;
 
class GFG
{
     
// Utility function to check
// perfect square number
static boolean isPerfectSquare(int n)
{
    int sq = (int) Math.sqrt(n);
    return (n == sq * sq);
}
 
// Returns count all
// perfect divisors of n
static int countPerfectDivisors(int n)
{
    // Initialize result
    int count = 0;
 
    // Consider every number
    // that can be a divisor of n
    for (int i = 1; i * i <= n; ++i)
    {
        // If i is a divisor
        if (n % i == 0)
        {
            if (isPerfectSquare(i))
                ++count;
            if (n / i != i &&
                isPerfectSquare(n / i))
                ++count;
        }
    }
    return count;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 16;
 
    System.out.print("Total perfect " +
                   "divisors of " + n);
    System.out.println(" = " +
               countPerfectDivisors(n));
 
    n = 12;
    System.out.print("Total perfect " +
                   "divisors of " + n);
    System.out.println(" = " +
               countPerfectDivisors(n));
}
}
 
// This code is contributed by ajit


Python3
# Python3 implementation of Naive method
# to count all perfect divisors
 
import math
 
def isPerfectSquare(x) :
    sq = (int)(math.sqrt(x))
    return (x == sq * sq)
 
# function to count all prefect divisors
def countPerfectDivisors(n) :
     
    # Initialize result
    cnt = 0
 
        # Consider every number that
        # can be a divisor of n
    for i in range(1, (int)(math.sqrt(n)) + 1) :
 
            # If i is a divisor
            if ( n % i == 0 ) :
 
                if isPerfectSquare(i):
                        cnt = cnt + 1
                if n/i != i and isPerfectSquare(n/i):
                        cnt = cnt + 1
    return cnt
     
         
# Driver program to test above function
print("Total perfect divisor of 16 = ",
    countPerfectDivisors(16))
     
print("Total perfect divisor of 12 = ",
    countPerfectDivisors(12))   
 
# This code is contributed by Saloni Gupta


C#
// C# code to count
// total perfect Divisors
using System;
 
class GFG
{
     
// Utility function to check
// perfect square number
static bool isPerfectSquare(int n)
{
    int sq = (int) Math.Sqrt(n);
    return (n == sq * sq);
}
 
// Returns count all
// perfect divisors of n
static int countPerfectDivisors(int n)
{
    // Initialize result
    int count = 0;
 
    // Consider every number
    // that can be a divisor of n
    for (int i = 1;
             i * i <= n; ++i)
    {
        // If i is a divisor
        if (n % i == 0)
        {
            if (isPerfectSquare(i))
                ++count;
            if (n / i != i &&
                isPerfectSquare(n / i))
                ++count;
        }
    }
    return count;
}
 
// Driver code
static public void Main ()
{
    int n = 16;
     
    Console.Write("Total perfect " +
                "divisors of " + n);
    Console.WriteLine(" = " +
            countPerfectDivisors(n));
     
    n = 12;
    Console.Write("Total perfect " +
                "divisors of " + n);
    Console.WriteLine(" = " +
            countPerfectDivisors(n));
}
}
 
// This code is contributed
// by akt_mit


PHP


Javascript


C++
// Below is C++ code to count total perfect
// divisors
#include
using namespace std;
#define MAX 100001
 
int perfectDiv[MAX];
 
// Pre-compute counts of all perfect divisors
// of all numbers upto MAX.
void precomputeCounts()
{
    for (int i=1; i*i < MAX; ++i)
    {
        // Iterate through all the multiples of i*i
        for (int j=i*i; j < MAX; j += i*i)
 
            // Increment all such multiples by 1
            ++perfectDiv[j];
    }
}
 
// Returns count of perfect divisors of n.
int countPerfectDivisors(int n)
{
    return perfectDiv[n];
}
 
// Driver code
int main()
{
    precomputeCounts();
 
    int n = 16;
    cout << "Total perfect divisors of "
         << n << " = " << countPerfectDivisors(n) << "\n";
 
    n = 12;
    cout << "Total perfect divisors of "
         << n << " = " << countPerfectDivisors(n);
 
    return 0;
}


Java
// Java code to count total perfect
// divisors
class GFG
{
     
static int MAX = 100001;
 
static int[] perfectDiv = new int[MAX];
 
// Pre-compute counts of all perfect divisors
// of all numbers upto MAX.
static void precomputeCounts()
{
    for (int i = 1; i * i < MAX; ++i)
    {
        // Iterate through all the multiples of i*i
        for (int j = i * i; j < MAX; j += i * i)
 
            // Increment all such multiples by 1
            ++perfectDiv[j];
    }
}
 
// Returns count of perfect divisors of n.
static int countPerfectDivisors(int n)
{
    return perfectDiv[n];
}
 
// Driver code
public static void main (String[] args)
{
    precomputeCounts();
 
    int n = 16;
    System.out.println("Total perfect divisors of " +
                    n + " = " + countPerfectDivisors(n));
 
    n = 12;
    System.out.println("Total perfect divisors of " +
                        n + " = " + countPerfectDivisors(n));
}
}
 
// This code is contributed by mits


Python3
# Below is Python3 code to count total perfect
# divisors
MAX = 100001
  
perfectDiv= [0]*MAX
  
# Pre-compute counts of all perfect divisors
# of all numbers upto MAX.
def precomputeCounts():
 
    i=1
    while i*i < MAX:
     
        # Iterate through all the multiples of i*i
        for j in range(i*i,MAX,i*i):
  
            # Increment all such multiples by 1
            perfectDiv[j] += 1
        i += 1
  
# Returns count of perfect divisors of n.
def countPerfectDivisors( n):
 
    return perfectDiv[n]
  
# Driver code
if __name__ == "__main__":
 
    precomputeCounts()
  
    n = 16
    print ("Total perfect divisors of "
         , n , " = " ,countPerfectDivisors(n))
  
    n = 12
    print ( "Total perfect divisors of "
         ,n ," = " ,countPerfectDivisors(n))


C#
// C# code to count total perfect
// divisors
using System;
class GFG
{
     
static int MAX = 100001;
 
static int[] perfectDiv = new int[MAX];
 
// Pre-compute counts of all perfect
// divisors of all numbers upto MAX.
static void precomputeCounts()
{
    for (int i = 1; i * i < MAX; ++i)
    {
        // Iterate through all the multiples of i*i
        for (int j = i * i; j < MAX; j += i * i)
 
            // Increment all such multiples by 1
            ++perfectDiv[j];
    }
}
 
// Returns count of perfect divisors of n.
static int countPerfectDivisors(int n)
{
    return perfectDiv[n];
}
 
// Driver code
public static void Main()
{
    precomputeCounts();
 
    int n = 16;
    Console.WriteLine("Total perfect divisors of " + n +
                       " = " + countPerfectDivisors(n));
 
    n = 12;
    Console.WriteLine("Total perfect divisors of " + n +
                       " = " + countPerfectDivisors(n));
}
}
 
// This code is contributed by mits


PHP


Output:
Total Perfect divisors of 16 = 3
Total Perfect divisors of 12 = 2

时间复杂度: O(sqrt(n))
辅助空间: O(1)

高效的方法(用于大量查询)

这个想法是基于Eratosthenes筛。如果存在大量查询,则此方法很有用。以下是找到不超过n个数的所有理想除数的算法。

  1. 创建一个从1到n的n个连续整数的列表:(1、2、3,…,n)
  2. 最初,令d为2,第一个除数
  3. 从4(2的平方)开始,在perfectDiv []数组中将4的所有倍数递增1,因为所有这些倍数都包含4作为完美除数。这些数字将是4d,8d,12d等
  4. 重复所有其他号码的第三步。 perfectDiv []的最终数组将包含从1到n的所有完美除数的数量

以下是上述步骤的实现。

C++

// Below is C++ code to count total perfect
// divisors
#include
using namespace std;
#define MAX 100001
 
int perfectDiv[MAX];
 
// Pre-compute counts of all perfect divisors
// of all numbers upto MAX.
void precomputeCounts()
{
    for (int i=1; i*i < MAX; ++i)
    {
        // Iterate through all the multiples of i*i
        for (int j=i*i; j < MAX; j += i*i)
 
            // Increment all such multiples by 1
            ++perfectDiv[j];
    }
}
 
// Returns count of perfect divisors of n.
int countPerfectDivisors(int n)
{
    return perfectDiv[n];
}
 
// Driver code
int main()
{
    precomputeCounts();
 
    int n = 16;
    cout << "Total perfect divisors of "
         << n << " = " << countPerfectDivisors(n) << "\n";
 
    n = 12;
    cout << "Total perfect divisors of "
         << n << " = " << countPerfectDivisors(n);
 
    return 0;
}

Java

// Java code to count total perfect
// divisors
class GFG
{
     
static int MAX = 100001;
 
static int[] perfectDiv = new int[MAX];
 
// Pre-compute counts of all perfect divisors
// of all numbers upto MAX.
static void precomputeCounts()
{
    for (int i = 1; i * i < MAX; ++i)
    {
        // Iterate through all the multiples of i*i
        for (int j = i * i; j < MAX; j += i * i)
 
            // Increment all such multiples by 1
            ++perfectDiv[j];
    }
}
 
// Returns count of perfect divisors of n.
static int countPerfectDivisors(int n)
{
    return perfectDiv[n];
}
 
// Driver code
public static void main (String[] args)
{
    precomputeCounts();
 
    int n = 16;
    System.out.println("Total perfect divisors of " +
                    n + " = " + countPerfectDivisors(n));
 
    n = 12;
    System.out.println("Total perfect divisors of " +
                        n + " = " + countPerfectDivisors(n));
}
}
 
// This code is contributed by mits

Python3

# Below is Python3 code to count total perfect
# divisors
MAX = 100001
  
perfectDiv= [0]*MAX
  
# Pre-compute counts of all perfect divisors
# of all numbers upto MAX.
def precomputeCounts():
 
    i=1
    while i*i < MAX:
     
        # Iterate through all the multiples of i*i
        for j in range(i*i,MAX,i*i):
  
            # Increment all such multiples by 1
            perfectDiv[j] += 1
        i += 1
  
# Returns count of perfect divisors of n.
def countPerfectDivisors( n):
 
    return perfectDiv[n]
  
# Driver code
if __name__ == "__main__":
 
    precomputeCounts()
  
    n = 16
    print ("Total perfect divisors of "
         , n , " = " ,countPerfectDivisors(n))
  
    n = 12
    print ( "Total perfect divisors of "
         ,n ," = " ,countPerfectDivisors(n))

C#

// C# code to count total perfect
// divisors
using System;
class GFG
{
     
static int MAX = 100001;
 
static int[] perfectDiv = new int[MAX];
 
// Pre-compute counts of all perfect
// divisors of all numbers upto MAX.
static void precomputeCounts()
{
    for (int i = 1; i * i < MAX; ++i)
    {
        // Iterate through all the multiples of i*i
        for (int j = i * i; j < MAX; j += i * i)
 
            // Increment all such multiples by 1
            ++perfectDiv[j];
    }
}
 
// Returns count of perfect divisors of n.
static int countPerfectDivisors(int n)
{
    return perfectDiv[n];
}
 
// Driver code
public static void Main()
{
    precomputeCounts();
 
    int n = 16;
    Console.WriteLine("Total perfect divisors of " + n +
                       " = " + countPerfectDivisors(n));
 
    n = 12;
    Console.WriteLine("Total perfect divisors of " + n +
                       " = " + countPerfectDivisors(n));
}
}
 
// This code is contributed by mits

的PHP


输出:

Total Perfect divisors of 16 = 3
Total Perfect divisors of 12 = 2