📜  从1到N的所有适当除数的总和

📅  最后修改于: 2021-04-21 23:41:54             🧑  作者: Mango

给定正整数N ,任务是找到的值\sum_{x=1}^{x=N} F(x)    其中函数F(x)可以定义为’ x ‘的所有适当除数的和。
例子:

幼稚的方法:想法是分别找到[1,N]范围内每个数字的适当除数的总和,然后将它们相加以找到所需的总和。
下面是上述方法的实现:

C++
// C++ implemenation to find sum of all
// proper divisor of number up to N
#include 
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 implemenation 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 implemenation 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# implemenation 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


C++
// C++ implementation to find sum of all
// proper divisor of numbers up to N
 
#include 
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


输出:
5
6

时间复杂度: O(N *√N)
辅助空间: O(1)
高效的方法:观察函数的模式后,可以看到“对于给定的数字N,在[1,N]范围内的每个数字’x’发生(N / x)次”
例如:

从上面的观察中,可以容易地观察到,数字x仅以其小于或等于N的倍数出现。因此,对于[1,N]中x的每个值,我们只需要查找此类倍数的计数,然后将其乘以x即可。然后将此值添加到最终总和中。
下面是上述方法的实现:

C++

// C++ implementation to find sum of all
// proper divisor of numbers up to N
 
#include 
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

Java脚本


输出:
5
6

时间复杂度: O(N)
辅助空间: O(1)