📜  令人钦佩的数字

📅  最后修改于: 2021-05-04 09:34:45             🧑  作者: Mango

给定一个整数N ,任务是检查N是否是一个可赞赏的数字。

例子:

方法:这个想法是找到一个数为sigma(N)的所有因子的总和。然后我们将找到数字D’的每个适当的除数,并检查是否存在N的适当除数D’,使得

sigma(N) - 2*D'= 2*N

下面是上述方法的实现:

C++
// C++ implementation to check if
// N is an admirable number
 
#include 
using namespace std;
 
// Function to calculate the sum of
// all divisors of a given number
int divSum(int n)
{
    // Sum of divisors
    int result = 0;
 
    // Find all divisors
    // which divides 'num'
    for (int i = 2; i <= sqrt(n); i++) {
 
        // if 'i' is divisor of 'n'
        if (n % i == 0) {
 
            // if both divisors are same
            // then add it once else add
            if (i == (n / i))
                result += i;
            else
                result += (i + n / i);
        }
    }
 
    // Add 1 and n to result as above loop
    // considers proper divisors greater
    return (result + n + 1);
}
 
// Function to check if there
// exists a proper divisor
// D' of N such that sigma(n)-2D' = 2N
bool check(int num)
{
    int sigmaN = divSum(num);
 
    // Find all divisors which divides 'num'
    for (int i = 2; i <= sqrt(num); i++) {
 
        // if 'i' is divisor of 'num'
        if (num % i == 0) {
 
            // if both divisors are same then add
            // it only once else add both
            if (i == (num / i)) {
                if (sigmaN - 2 * i == 2 * num)
                    return true;
            }
            else {
                if (sigmaN - 2 * i == 2 * num)
                    return true;
                if (sigmaN - 2 * (num / i) == 2 * num)
                    return true;
            }
        }
    }
 
    // Check 1 since 1 is also a divisor
    if (sigmaN - 2 * 1 == 2 * num)
        return true;
 
    return false;
}
 
// Function to check if N
// is an admirable number
bool isAdmirableNum(int N)
{
    return check(N);
}
 
// Driver code
int main()
{
    int n = 12;
    if (isAdmirableNum(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation to check if N
// is a admirable number
class GFG{
 
// Function to calculate the sum of
// all divisors of a given number
static int divSum(int n)
{
    // Sum of divisors
    int result = 0;
 
    // Find all divisors
    // which divides 'num'
    for (int i = 2; i <= Math.sqrt(n); i++)
    {
 
        // if 'i' is divisor of 'n'
        if (n % i == 0)
        {
 
            // if both divisors are same
            // then add it once else add
            if (i == (n / i))
                result += i;
            else
                result += (i + n / i);
        }
    }
 
    // Add 1 and n to result as above loop
    // considers proper divisors greater
    return (result + n + 1);
}
 
// Function to check if there
// exists a proper divisor
// D' of N such that sigma(n)-2D' = 2N
static boolean check(int num)
{
    int sigmaN = divSum(num);
 
    // Find all divisors which divides 'num'
    for (int i = 2; i <= Math.sqrt(num); i++)
    {
 
        // if 'i' is divisor of 'num'
        if (num % i == 0)
        {
 
            // if both divisors are same then add
            // it only once else add both
            if (i == (num / i))
            {
                if (sigmaN - 2 * i == 2 * num)
                    return true;
            }
            else
            {
                if (sigmaN - 2 * i == 2 * num)
                    return true;
                if (sigmaN - 2 * (num / i) == 2 * num)
                    return true;
            }
        }
    }
 
    // Check 1 since 1 is also a divisor
    if (sigmaN - 2 * 1 == 2 * num)
        return true;
 
    return false;
}
 
// Function to check if N
// is an admirable number
static boolean isAdmirableNum(int N)
{
    return check(N);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 12;
    if (isAdmirableNum(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by shubham


Python3
# Python3 implementation to check if
# N is an admirable number
import math
 
# Function to calculate the sum of
# all divisors of a given number
def divSum(n):
     
    # Sum of divisors
    result = 0
 
    # Find all divisors
    # which divides 'num'
    for i in range(2, int(math.sqrt(n)) + 1):
 
        # If 'i' is divisor of 'n'
        if (n % i == 0):
 
            # If both divisors are same
            # then add it once else add
            if (i == (n // i)):
                result += i
            else:
                result += (i + n // i)
 
    # Add 1 and n to result as above loop
    # considers proper divisors greater
    return (result + n + 1)
 
# Function to check if there
# exists a proper divisor
# D' of N such that sigma(n)-2D' = 2N
def check(num):
 
    sigmaN = divSum(num)
 
    # Find all divisors which divides 'num'
    for i in range(2, int(math.sqrt(num)) + 1):
 
        # If 'i' is divisor of 'num'
        if (num % i == 0):
 
            # If both divisors are same then add
            # it only once else add both
            if (i == (num // i)):
                if (sigmaN - 2 * i == 2 * num):
                    return True
             
            else:
                if (sigmaN - 2 * i == 2 * num):
                    return True
                if (sigmaN - 2 * (num // i) == 2 * num):
                    return True
 
    # Check 1 since 1 is also a divisor
    if (sigmaN - 2 * 1 == 2 * num):
        return True
 
    return False
 
# Function to check if N
# is an admirable number
def isAdmirableNum(N):
     
    return check(N)
     
# Driver code
n = 12
 
if (isAdmirableNum(n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyeshrabadiya07


C#
// C# implementation to check if N
// is a admirable number
using System;
class GFG{
 
// Function to calculate the sum of
// all divisors of a given number
static int divSum(int n)
{
     
    // Sum of divisors
    int result = 0;
 
    // Find all divisors
    // which divides 'num'
    for(int i = 2; i <= Math.Sqrt(n); i++)
    {
        
       // If 'i' is divisor of 'n'
       if (n % i == 0)
       {
            
           // If both divisors are same
           // then add it once else add
           if (i == (n / i))
               result += i;
           else
               result += (i + n / i);
       }
    }
 
    // Add 1 and n to result as above loop
    // considers proper divisors greater
    return (result + n + 1);
}
 
// Function to check if there
// exists a proper divisor
// D' of N such that sigma(n)-2D' = 2N
static bool check(int num)
{
    int sigmaN = divSum(num);
 
    // Find all divisors which divides 'num'
    for(int i = 2; i <= Math.Sqrt(num); i++)
    {
        
       // If 'i' is divisor of 'num'
       if (num % i == 0)
       {
            
           // If both divisors are same then add
           // it only once else add both
           if (i == (num / i))
           {
               if (sigmaN - 2 * i == 2 * num)
                   return true;
           }
           else
           {
               if (sigmaN - 2 * i == 2 * num)
                   return true;
               if (sigmaN - 2 * (num / i) == 2 * num)
                   return true;
           }
       }
    }
 
    // Check 1 since 1 is also a divisor
    if (sigmaN - 2 * 1 == 2 * num)
        return true;
 
    return false;
}
 
// Function to check if N
// is an admirable number
static bool isAdmirableNum(int N)
{
    return check(N);
}
 
// Driver code
public static void Main()
{
    int n = 12;
     
    if (isAdmirableNum(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
Yes

参考文献: OEIS