Suma de todos los divisores propios de 1 a N

Dado un entero positivo N , la tarea es encontrar el valor de  \sum_{x=1}^{x=N} F(x)     donde la función F(x) puede definirse como la suma de todos los divisores propios de ‘ x ‘.
Ejemplos: 
 

Entrada: N = 4 
Salida:
Explicación: 
Suma de todos los divisores propios de números: 
F(1) = 0 
F(2) = 1 
F(3) = 1 
F(4) = 1 + 2 = 3 
Suma total = F (1) + F(2) + F(3) + F(4) = 0 + 1 + 1 + 3 = 5
Entrada: N = 5 
Salida:
Explicación: 
Suma de todos los divisores propios de números: 
F(1) = 0 
F(2) = 1 
F(3) = 1 
F(4) = 1 + 2 = 3 
F(5) = 1 
Suma total = F(1) + F(2) + F(3) + F( 4) + F(5) = 0 + 1 + 1 + 3 + 1 = 6 
 

Enfoque ingenuo: la idea es encontrar la suma de los divisores propios de cada número en el rango [1, N] individualmente y luego sumarlos para encontrar la suma requerida.
A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation to find sum of all
// proper divisor of number up to N
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to find sum of
// all proper divisor of number up to N
int properDivisorSum(int n)
{
    int sum = 0;
 
    // Loop to iterate over all the
    // numbers from 1 to N
    for (int i = 1; i <= n; ++i) {
 
        // Find all divisors of
        // i and add them
        for (int j = 1; j * j <= i; ++j) {
            if (i % j == 0) {
                if (i / j == j)
                    sum += j;
                else
                    sum += j + i / j;
            }
        }
 
        // Subtracting 'i' so that the
        // number itself is not included
        sum = sum - i;
    }
    return sum;
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << properDivisorSum(n) << endl;
 
    n = 5;
    cout << properDivisorSum(n) << endl;
 
    return 0;
}

Java

// Java implementation to find sum of all
// proper divisor of number up to N
class GFG {
     
    // Utility function to find sum of
    // all proper divisor of number up to N
    static int properDivisorSum(int n)
    {
        int sum = 0;
     
        // Loop to iterate over all the
        // numbers from 1 to N
        for (int i = 1; i <= n; ++i) {
     
            // Find all divisors of
            // i and add them
            for (int j = 1; j * j <= i; ++j) {
                if (i % j == 0) {
                    if (i / j == j)
                        sum += j;
                    else
                        sum += j + i / j;
                }
            }
     
            // Subtracting 'i' so that the
            // number itself is not included
            sum = sum - i;
        }
        return sum;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 4;
        System.out.println(properDivisorSum(n));
     
        n = 5;
        System.out.println(properDivisorSum(n)) ;
     
    }
}
 
// This code is contributed by Yash_R

Python3

# Python3 implementation to find sum of all
# proper divisor of number up to N
 
# Utility function to find sum of
# all proper divisor of number up to N
def properDivisorSum(n):
 
    sum = 0
 
    # Loop to iterate over all the
    # numbers from 1 to N
    for i in range(n+1):
 
        # Find all divisors of
        # i and add them
        for j in range(1, i + 1):
            if j * j > i:
                break
            if (i % j == 0):
                if (i // j == j):
                    sum += j
                else:
                    sum += j + i // j
 
        # Subtracting 'i' so that the
        # number itself is not included
        sum = sum - i
 
    return sum
 
# Driver Code
if __name__ == '__main__':
 
    n = 4
    print(properDivisorSum(n))
 
    n = 5
    print(properDivisorSum(n))
 
# This code is contributed by mohit kumar 29

C#

// C# implementation to find sum of all
// proper divisor of number up to N
using System;
 
class GFG {
     
    // Utility function to find sum of
    // all proper divisor of number up to N
    static int properDivisorSum(int n)
    {
        int sum = 0;
     
        // Loop to iterate over all the
        // numbers from 1 to N
        for (int i = 1; i <= n; ++i) {
     
            // Find all divisors of
            // i and add them
            for (int j = 1; j * j <= i; ++j) {
                if (i % j == 0) {
                    if (i / j == j)
                        sum += j;
                    else
                        sum += j + i / j;
                }
            }
     
            // Subtracting 'i' so that the
            // number itself is not included
            sum = sum - i;
        }
        return sum;
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int n = 4;
        Console.WriteLine(properDivisorSum(n));
     
        n = 5;
        Console.WriteLine(properDivisorSum(n)) ;   
    }
}
 
// This code is contributed by Yash_R

Javascript

<script>
 
//Javascript implementation to find sum of all
// proper divisor of number up to N 
 
// Utility function to find sum of
// all proper divisor of number up to N
function properDivisorSum(n)
{
    let sum = 0;
 
    // Loop to iterate over all the
    // numbers from 1 to N
    for (let i = 1; i <= n; ++i) {
 
        // Find all divisors of
        // i and add them
        for (let j = 1; j * j <= i; ++j) {
            if (i % j == 0) {
                if (i / j == j)
                    sum += j;
                else
                    sum += j + i / j;
            }
        }
 
        // Subtracting 'i' so that the
        // number itself is not included
        sum = sum - i;
    }
    return sum;
}
 
// Driver Code
  
    let n = 4;
    document.write(properDivisorSum(n) + "<br>");
 
    n = 5;
    document.write(properDivisorSum(n) + "<br>");
 
     
// This code is contributed by Mayank Tyagi
     
</script>
Producción: 

5
6

 

Complejidad temporal: O(N * √N) 
Espacio auxiliar: O(1)
Enfoque eficiente: Al observar el patrón en la función, se puede ver que “Para un número dado N, todo número ‘x’ en el rango [1 , N] ocurre (N/x) número de veces” .
Por ejemplo: 
 

Sea N = 6 => G(N) = F(1) + F(2) + F(3) + F(4) + F(5) + F(6) 
x = 1 => 1 ocurrirá 6 veces (en F(1), F(2), F(3), F(4), F(5) y F(6)) 
x = 2 => 2 ocurrirá 3 veces (en F(2), F (4) y F(6)) 
x = 3 => 3 ocurrirá 2 veces (en F(3) y F(6)) 
x = 4 => 4 ocurrirá 1 vez (en F(4)) 
x = 5 => 5 ocurrirá 1 vez (en F(5)) 
x = 6 => 6 ocurrirá 1 vez (en F(6)) 
 

De la observación anterior, se puede observar fácilmente que el número x ocurre solo en su múltiplo menor o igual que N. Por lo tanto, solo necesitamos encontrar el conteo de tales múltiplos, para cada valor de x en [1, N], y luego multiplicarlo por x . Este valor se suma luego a la suma final.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation to find sum of all
// proper divisor of numbers up to N
 
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to find sum of
// all proper divisor of number up to N
int properDivisorSum(int n)
{
    int sum = 0;
 
    // Loop to find the proper
    // divisor of every number
    // from 1 to N
    for (int i = 1; i <= n; ++i)
        sum += (n / i) * i;
 
    return sum - n * (n + 1) / 2;
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << properDivisorSum(n) << endl;
 
    n = 5;
    cout << properDivisorSum(n) << endl;
    return 0;
}

Java

// Java implementation to find sum of all
// proper divisor of numbers up to N
  
// Utility function to find sum of
// all proper divisor of number up to N
 
class GFG
{
    static int properDivisorSum(int n)
    {
        int sum = 0;
        int i;
        // Loop to find the proper
        // divisor of every number
        // from 1 to N
        for (i = 1; i <= n; ++i)
            sum += (n / i) * i;
      
        return sum - n * (n + 1) / 2;
    }
      
    // Driver Code
    public static void main(String []args)
    {
        int n = 4;
        System.out.println(properDivisorSum(n));
      
        n = 5;
        System.out.println(properDivisorSum(n));
         
    }
}

Python3

# Python3 implementation to find sum of all
# proper divisor of numbers up to N
 
# Utility function to find sum of
# all proper divisor of number up to N
def properDivisorSum(n):
     
    sum = 0
     
    # Loop to find the proper
    # divisor of every number
    # from 1 to N
    for i in range(1, n + 1):
        sum += (n // i) * i
         
    return sum - n * (n + 1) // 2
 
 
# Driver Code
n = 4
print(properDivisorSum(n))
 
n = 5
print(properDivisorSum(n))
 
# This code is contributed by shubhamsingh10

C#

// C# implementation to find sum of all
// proper divisor of numbers up to N
   
// Utility function to find sum of
// all proper divisor of number up to N
using System;
 
class GFG
{
    static int properDivisorSum(int n)
    {
        int sum = 0;
        int i;
        // Loop to find the proper
        // divisor of every number
        // from 1 to N
        for (i = 1; i <= n; ++i)
            sum += (n / i) * i;
       
        return sum - n * (n + 1) / 2;
    }
       
    // Driver Code
    public static void Main(String []args)
    {
        int n = 4;
        Console.WriteLine(properDivisorSum(n));
       
        n = 5;
        Console.WriteLine(properDivisorSum(n));
          
    }
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
// Javascript implementation to find sum of all
// proper divisor of numbers up to N
 
// Utility function to find sum of
// all proper divisor of number up to N
function properDivisorSum(n)
{
    var sum = 0;
 
    // Loop to find the proper
    // divisor of every number
    // from 1 to N
    for (var i = 1; i <= n; ++i)
        sum += parseInt(n / i) * i;
 
    return sum - n * ((n + 1) / 2);
}
 
// Driver Code
var n = 4;
document.write(properDivisorSum(n)+"<br>");
n = 5;
document.write(properDivisorSum(n)+"<br>");
 
// This code is contributed by rutvik_56.
</script>
Producción: 

5
6

 

Complejidad temporal: O(N) 
Espacio auxiliar: O(1)
 

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *