📌  相关文章
📜  用于检查给定数字是否为完全数的Java程序

📅  最后修改于: 2022-05-13 01:54:25.095000             🧑  作者: Mango

用于检查给定数字是否为完全数的Java程序

如果一个数的真除数(即除数本身之外的所有正除数)之和等于该数本身,则称该数为完全数。等分总和是一个数字的除数之和,不包括数字本身。因此,只有当一个数等于它的等分和时,它才是完全数。所有已知的完全数都是偶数

Example 1:

n = 9
Proper Divisors of 9 are 1 and 3.
Sum = 1+3 = 4 ≠ 9
⇒ 9 is not a perfect number.

Example 2:

n = 6
Proper Divisors of 6 are 1, 2 and 3.
Sum = 1+2+3 = 6 = 6
⇒ 6 is a perfect number.

Example 3:

n = 28
Proper Divisors of 28 are 1, 2, 4, 7 and 14.
Sum = 1+2+4+7+14 = 28 = 28
⇒ 28 is a perfect number.

Example 4:

n = 15
Proper Divisors of 15 are 1,3 and 5.
Sum = 1+3+5 = 9 ≠ 15
⇒ 15 is not a perfect number.

所以,我们基本上必须找到一个数的真因数之和。

方法一:

一个简单的解决方案是遍历从 1 到 n-1 的每个数字并检查它是否是除数,如果是,则将其添加到 sum 变量中,最后检查总和是否等于数字本身,然后它是一个完美的数字,否则不是。

Java
// Java program to check if a given
// number is perfect or not
  
class GFG {
  
    // Returns true if n is perfect
    static boolean isPerfect(int n)
    {
        // 1 is not a perfect number
        if (n == 1)
            return false;
  
        // sum will store the sum of proper divisors
        // As 1 is a proper divisor for all numbers
        // initialised sum with 1
        int sum = 1;
  
        // Looping through the numbers to check if they are
        // divisors or not
        for (int i = 2; i < n; i++) {
            
            if (n % i == 0) {
                sum += i;
            }
            
        }
  
        // If sum of divisors is equal to
        // n, then n is a perfect number
        if (sum == n)
            return true;
  
        return false;
    }
  
    // Driver program
    public static void main(String[] args)
    {
        int n = 6;
        
        // Call isPerfect function to
        // check if the number is perfect or not.
        if (isPerfect(n))
            System.out.println(n + " is a perfect number");
        else
            System.out.println(
                n + " is not a perfect number");
    }
}


Java
// Java program to check if a given
// number is perfect or not
  
class GFG {
  
    // Returns true if n is perfect
    static boolean isPerfect(int n)
    {
        // 1 is not a perfect number
        if (n == 1)
            return false;
  
        // sum will store the sum of proper divisors
        // As 1 is a proper divisor for all numbers
        // initialised sum with 1
        int sum = 1;
  
        // Looping through the numbers to check if they are
        // divisors or not
        for (int i = 2; i * i <= n; i++) {
            
            if (n % i == 0) {
                
                // n is a perfect square
                // let's take 25
                // we need to add 5 only once
                // sum += i + n / i will add it twice
                
                if (i * i == n)
                    sum += i;
                else
                    sum += i + (n / i);
            }
        }
  
        // If sum of divisors is equal to
        // n, then n is a perfect number
        
        if (sum == n)
            
            return true;
  
        return false;
    }
  
    // Driver program
    public static void main(String[] args)
    {
        int n = 6;
        
        // Call isPerfect function to
        // check if the number is perfect or not.
        if (isPerfect(n))
            
            System.out.println(n + " is a perfect number");
        
        else
            System.out.println(
                n + " is not a perfect number");
    }
}


输出
6 is a perfect number
  • 时间复杂度: O(n)

方法二:

一个有效的解决方案是遍历数字直到 n 的平方根

Java

// Java program to check if a given
// number is perfect or not
  
class GFG {
  
    // Returns true if n is perfect
    static boolean isPerfect(int n)
    {
        // 1 is not a perfect number
        if (n == 1)
            return false;
  
        // sum will store the sum of proper divisors
        // As 1 is a proper divisor for all numbers
        // initialised sum with 1
        int sum = 1;
  
        // Looping through the numbers to check if they are
        // divisors or not
        for (int i = 2; i * i <= n; i++) {
            
            if (n % i == 0) {
                
                // n is a perfect square
                // let's take 25
                // we need to add 5 only once
                // sum += i + n / i will add it twice
                
                if (i * i == n)
                    sum += i;
                else
                    sum += i + (n / i);
            }
        }
  
        // If sum of divisors is equal to
        // n, then n is a perfect number
        
        if (sum == n)
            
            return true;
  
        return false;
    }
  
    // Driver program
    public static void main(String[] args)
    {
        int n = 6;
        
        // Call isPerfect function to
        // check if the number is perfect or not.
        if (isPerfect(n))
            
            System.out.println(n + " is a perfect number");
        
        else
            System.out.println(
                n + " is not a perfect number");
    }
}
输出
6 is a perfect number

时间复杂度: O(√n)