如果所有m
1, 2, 4, 6, 12, 24, 36, 48…
检查N是否为超丰数
给定数字N ,任务是检查N是否为超丰裕数字。如果N是一个超级数字,则打印“是”,否则打印“否” 。
例子:
Input: N = 4
Output: Yes
Explanation:
sigma(4)/4 = 7/4 = 1.75
sigma(1)/1 = 1/1 = 1
sigma(2)/2 = 3/2 = 1.5
sigma(3)/3 = 4/3 = 1.333333
sigma(n)/n > sigma(m)/m for all m < n
Input: N = 10
Output: No
方法:对于从1到小于N的所有数字i,检查sigma(n)/ n> sigma(m)/ m,然后返回false,否则最后返回true。
例如:
If N = 4, sigma(4)/4 = 7/4 = 1.75
for i = 1 to 3
sigma(1)/1 = 1/1 = 1
sigma(2)/2 = 3/2 = 1.5
sigma(3)/3 = 4/3 = 1.333333
sigma(n)/n > sigma(m)/m for all m < n,
Hence return true at last
下面是上述方法的实现:
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