📜  偶数

📅  最后修改于: 2021-04-22 03:38:37             🧑  作者: Mango

给定一个偶数N ,任务是检查它是否为一个完美数而不找到其除数

例子:

方法:

  1. 找到给定数字的平方根,得到接近2 q – 1的数字
  2. 从数字的平方根中找到q-1,然后检查2 q-1 *(2 q -1)是否给出输入的数字。如果不是,那么它不是一个完美的数字,否则请继续。
  3. 检查q是否为质数。如果不是素数,则2 q -1不能为素数,随后检查2 q -1是否为素数。
  4. 如果上述所有条件都成立,那么它是一个偶数个完美的数,否则不是。

下面是上述方法的实现:

C++
// C++ program for the above approach 
#include 
using namespace std; 
  
bool isPrime(long n); 
  
// Function to check for perfect number 
void check(long num) 
{ 
      
    // Find a number close to 2^q-1 
    long root = (long)sqrt(num); 
  
    // Calculate q-1 
    long poww = (long)(log(root) / log(2)); 
  
    // Condition of perfect number 
    if (num == (long)(pow(2, poww) * 
                    (pow(2, poww + 1) - 1))) 
    { 
  
        // Check whether q is prime or not 
        if (isPrime(poww + 1)) 
        { 
              
            // Check whether 2^q - 1 is a 
            // prime number or not 
            if (isPrime((long)pow(2, 
                poww + 1) - 1)) 
                cout << "Yes" << endl; 
            else
                cout << "No" << endl; 
        } 
        else
            cout << "No" << endl; 
    } 
    else
        cout << "No" << endl; 
} 
  
// Function to check for prime number 
bool isPrime(long n) 
{ 
    if (n <= 1) 
        return false; 
  
    // Check whether it is equal to 2 or 3 
    else if (n == 2 || n == 3) 
        return true; 
  
    else
    { 
          
        // Check if it can be divided by 2 
        // and 3 then it is not prime number 
        if (n % 2 == 0 || n % 3 == 0) 
            return false; 
  
        // Check whether the given number be 
        // divide by other prime numbers 
        for(long i = 5; i <= sqrt(n); i += 6) 
        { 
            if (n % i == 0 || n % (i + 2) == 0) 
                return false; 
        } 
        return true; 
    } 
} 
  
// Driver Code 
int main() 
{ 
    long num = 6; 
      
    check(num); 
      
    return 0; 
} 
  
// This code is contributed by rutvik_56


Java
// Java program for the above approach 
  
class GFG { 
  
    // Function to check for perfect number 
    private static void check(long num) 
    { 
        // Find a number close to 2^q-1 
        long root = (long)Math.sqrt(num); 
  
        // Calculate q-1 
        long pow 
            = (long)(Math.log(root) 
                    / Math.log(2)); 
  
        // Condition of perfect number 
        if (num 
            == (long)(Math.pow(2, pow) 
                    * (Math.pow(2, pow + 1) - 1))) { 
  
            // Check whether q is prime or not 
            if (isPrime(pow + 1)) { 
  
                // Check whether 2^q - 1 is a 
                // prime number or not 
                if (isPrime( 
                        (long)Math.pow( 
                            2, pow + 1) 
                        - 1)) 
                    System.out.println("Yes"); 
  
                else
                    System.out.println("No"); 
            } 
            else
                System.out.println("No"); 
        } 
        else
            System.out.println("No"); 
    } 
  
    // Function to check for prime number 
    public static boolean isPrime(long n) 
    { 
        if (n <= 1) 
            return false; 
  
        // Check whether it is equal to 2 or 3 
        else if (n == 2 || n == 3) 
            return true; 
  
        else { 
            // Check if it can be divided by 2 
            // and 3 then it is not prime number 
            if (n % 2 == 0 || n % 3 == 0) 
                return false; 
  
            // Check whether the given number be 
            // divide by other prime numbers 
            for (long i = 5; 
                i <= Math.sqrt(n); 
                i += 6) { 
                if (n % i == 0
                    || n % (i + 2) == 0) 
                    return false; 
            } 
            return true; 
        } 
    } 
  
    // Driver code 
    public static void main(String args[]) 
    { 
        long num = 6; 
        check(num); 
    } 
}


Python3
# Python3 program for the above approach
import math
  
# Function to check for perfect number 
def check(num): 
      
    # Find a number close to 2^q-1 
    root = (int)(math.sqrt(num))
  
    # Calculate q-1 
    poww = (int)(math.log(root) /
                 math.log(2))
  
    # Condition of perfect number 
    if (num == (int)(pow(2, poww) *
                    (pow(2, poww + 1) - 1))):
  
        # Check whether q is prime or not 
        if (isPrime(poww + 1)):
              
            # Check whether 2^q - 1 is a 
            # prime number or not 
            if (isPrime((int)(pow(2, 
                poww + 1)) - 1)): 
                print("Yes") 
            else:
                print("No") 
                  
        else:
            print("No") 
    else:
        print("No") 
  
# Function to check for prime number 
def isPrime(n): 
  
    if (n <= 1): 
        return bool(False) 
  
    # Check whether it is equal to 2 or 3 
    elif (n == 2 or n == 3): 
        return bool(True) 
  
    else: 
          
        # Check if it can be divided by 2 
        # and 3 then it is not prime number 
        if (n % 2 == 0 or n % 3 == 0): 
            return bool(False)
  
        # Check whether the given number be 
        # divide by other prime numbers 
        for i in range(5, sqrt(n + 1) + 1, 6):
            if (n % i == 0 or n % (i + 2) == 0):
                return bool(False)
                  
        return bool(True) 
  
# Driver Code         
num = 6
      
check(num)
  
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach 
using System; 
using System.Collections.Generic; 
  
class GFG{ 
  
// Function to check for perfect number 
private static void check(long num) 
{ 
      
    // Find a number close to 2^q-1 
    long root = (long)Math.Sqrt(num); 
  
    // Calculate q-1 
    long pow = (long)(Math.Log(root) / 
                    Math.Log(2)); 
  
    // Condition of perfect number 
    if (num == (long)(Math.Pow(2, pow) * 
                    (Math.Pow(2, pow + 1) - 1))) 
    { 
          
        // Check whether q is prime or not 
        if (isPrime(pow + 1)) 
        { 
  
            // Check whether 2^q - 1 is a 
            // prime number or not 
            if (isPrime((long)Math.Pow(2, pow + 1) - 1)) 
                Console.WriteLine("Yes"); 
  
            else
                Console.WriteLine("No"); 
        } 
        else
            Console.WriteLine("No"); 
    } 
    else
        Console.WriteLine("No"); 
} 
  
// Function to check for prime number 
public static bool isPrime(long n) 
{ 
    if (n <= 1) 
        return false; 
  
    // Check whether it is equal to 2 or 3 
    else if (n == 2 || n == 3) 
        return true; 
  
    else
    { 
          
        // Check if it can be divided by 2 
        // and 3 then it is not prime number 
        if (n % 2 == 0 || n % 3 == 0) 
            return false; 
  
        // Check whether the given number be 
        // divide by other prime numbers 
        for(long i = 5; 
                i <= Math.Sqrt(n); 
                i += 6) 
        { 
            if (n % i == 0 || n % (i + 2) == 0) 
                return false; 
        } 
        return true; 
    } 
} 
  
// Driver code 
public static void Main(String []args) 
{ 
    long num = 6; 
    check(num); 
} 
} 
  
// This code is contributed by amal kumar choubey


输出:
Yes

时间复杂度: O(N 1/4 )
辅助空间: O(1)