📜  大量数字

📅  最后修改于: 2021-04-24 18:05:04             🧑  作者: Mango

如果所有m sigma(m)/ m,则数字为超丰数,其中sigma是n的除数之和。

检查N是否为超丰数

给定数字N ,任务是检查N是否为超丰裕数字。如果N是一个超级数字,则打印“是”,否则打印“否”

例子:

方法:对于从1到小于N的所有数字i,检查sigma(n)/ n> sigma(m)/ m,然后返回false,否则最后返回true。

例如:

下面是上述方法的实现:

C++
// C++ implementation to check if
// a number is Superabundant
 
#include 
using namespace std;
 
// Function to calculate the sum of all
// divisors of a given number
int sigma(int n)
{
    if (n == 1)
        return 1;
 
    // 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
            // both
            if (i == (n / i))
                result += i;
            else
                result += (i + n / i);
        }
    }
 
    // Add 1 and n to result as above loop
    // considers proper divisors greater
    // than 1.
    return (result + n + 1);
}
 
// Function to check if N is a
// superabundant number
bool isSuperabundant(int N)
{
 
    // to check all numbers from 1 to N
    for (float i = 1; i < N; i++) {
        float x = sigma(i) / i;
        float y = sigma(N) / (N * 1.0);
        if (x > y)
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    int N = 4;
    isSuperabundant(N) ? cout << "Yes"
                       : cout << "No";
    return 0;
}


Java
// Java implementation to check if
// a number is Superabundant
class GFG{
 
// Function to calculate the sum of all
// divisors of a given number
static int sigma(int n)
{
    if (n == 1)
        return 1;
 
    // 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
            // both
            if (i == (n / i))
                result += i;
            else
                result += (i + n / i);
        }
    }
 
    // Add 1 and n to result as above loop
    // considers proper divisors greater
    // than 1.
    return (result + n + 1);
}
 
// Function to check if N is a
// superabundant number
static boolean isSuperabundant(int N)
{
 
    // To check all numbers from 1 to N
    for(double i = 1; i < N; i++)
    {
        double x = sigma((int)(i)) / i;
        double y = sigma((int)(N)) / (N * 1.0);
        if (x > y)
            return false;
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 4;
     
    if(isSuperabundant(N))
        System.out.print("Yes\n");
    else
        System.out.print("No\n");
}
}
 
// This code is contributed by shubham


Python3
# Python3 implementation to check if 
# a number is Superabundant
 
# Function to calculate the sum of all
# divisors of a given number
def sigma(n):
     
    if (n == 1):
        return 1
         
    # Sum of divisors
    result = 0
 
    # Find all divisors which divides 'num'
    for i in range(2, pow(n, 1 // 2)):
         
        # If 'i' is divisor of 'n'
        if (n % i == 0):
             
            # If both divisors are same
            # then add it once else add
            # both
            if (i == (n / i)):
                result += i
            else:
                result += (i + n / i)
 
    # Add 1 and n to result as above loop
    # considers proper divisors greater
    # than 1.
    return (result + n + 1)
 
# Function to check if N is a
# superabundant number
def isSuperabundant(N):
     
    # To check all numbers from 1 to N
    for i in range(1, N):
        x = sigma((int)(i)) / i
        y = sigma((int)(N)) / (N * 1.0)
         
        if (x > y):
            return False
 
    return True
 
# Driver code
if __name__ == '__main__':
     
    N = 4
 
    if (isSuperabundant(N) != True):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by shikhasingrajput


C#
// C# implementation to check if
// a number is Superabundant
using System;
 
class GFG{
 
// Function to calculate the sum of
// all divisors of a given number
static int sigma(int n)
{
    if (n == 1)
        return 1;
 
    // 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
            // both
            if (i == (n / i))
                result += i;
            else
                result += (i + n / i);
        }
    }
 
    // Add 1 and n to result as above loop
    // considers proper divisors greater
    // than 1.
    return (result + n + 1);
}
 
// Function to check if N is a
// superabundant number
static bool isSuperabundant(int N)
{
 
    // To check all numbers from 1 to N
    for(double i = 1; i < N; i++)
    {
        double x = sigma((int)(i)) / i;
        double y = sigma((int)(N)) / (N * 1.0);
        if (x > y)
            return false;
    }
    return true;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 4;
     
    if(isSuperabundant(N))
        Console.Write("Yes\n");
    else
        Console.Write("No\n");
}
}
 
// This code is contributed by amal kumar choubey


输出:
Yes





时间复杂度: O(n)
参考: https : //en.wikipedia.org/wiki/Superabundant_number