Un número es un Número Superabundante si sigma(n)/n > sigma(m)/m para todo m < n, donde sigma es la suma de los divisores de n.
1, 2, 4, 6, 12, 24, 36, 48…
Comprobar si N es un número superabundante
Dado un número N , la tarea es comprobar si N es un Número Superabundante o no. Si N es un número superabundante, escriba «Sí» , de lo contrario, escriba «No» .
Ejemplos:
Entrada: N = 4
Salida: Sí
Explicación:
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 para todos los m < nEntrada: N = 10
Salida: No
Enfoque: para todos los números i desde 1 hasta menos que N, verifique si sigma(n)/n > sigma(m)/m y luego devuelva falso; de lo contrario, devuelva verdadero al final.
Por ejemplo:
Si N = 4, sigma(4)/4 = 7/4 = 1,75
para i = 1 a 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 para todos los m < n,
por lo tanto regresa verdadero al fin
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check if // a number is Superabundant #include <bits/stdc++.h> 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
Javascript
<script> // Javascript implementation to check if // a number is Superabundant // Function to calculate the sum of all // divisors of a given number function sigma(n) { if (n == 1) return 1; // Sum of divisors var result = 0; // find all divisors which divides 'num' for (var 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 function isSuperabundant(N) { // to check all numbers from 1 to N for (var i = 1; i < N; i++) { var x = sigma(i) / i; var y = sigma(N) / (N * 1.0); if (x > y) return false; } return true; } // Driver code var N = 4; isSuperabundant(N) ? document.write( "Yes") : document.write("No"); </script>
Yes
Complejidad de tiempo: O(n)