📜  成对乘积之和

📅  最后修改于: 2021-04-26 19:10:36             🧑  作者: Mango

对于给定的任何无符号int n,找到∑(i * j)的最终值,其中(1 <= i <= n)和(i <= j <= n)。
例子:

Input : n = 3
Output : 25
We get the sum as following. Note that
first term i varies from 1 to 3 and second
term values from value of first term to n.
1*1 + 1*2 + 1*3 + 2*2 + 2*3 + 3*3 = 25

Input : 5
Output : 140

方法1(简单)我们运行两个循环并计算所需的总和。

C++
// Simple CPP program to find sum
// of given series.
#include 
using namespace std;
 
long long int findSum(int n)
{
   long long int sum = 0;
   for (int i=1; i<=n; i++)
     for (int j=i; j<=n; j++)
        sum = sum + i*j;
   return sum;
}
 
int main()
{
    int n = 5;
    cout << findSum(n);
    return 0;
}


Java
// Simple Java program to find sum
// of given series.
class GFG {
     
    static int findSum(int n)
    {
        int sum = 0;
         
        for (int i=1; i<=n; i++)
            for (int j=i; j<=n; j++)
                sum = sum + i*j;
                 
        return sum;
    }
     
    // Driver code
    public static void main(String[] args)
    {
         
        int n = 5;
         
        System.out.println(findSum(n));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal.


Python3
# Simple Python3 program to  
# find sum of given series.
 
def findSum(n) :
    sm = 0
    for i in range(1, n + 1) :
        for j in range(i, n + 1) :
            sm = sm + i * j
             
    return sm
     
# Driver Code
n = 5
print(findSum(n))
 
# This code is contributed by Nikita Tiwari.


C#
// Simple C# program to find sum
// of given series.
class GFG {
     
    static int findSum(int n)
    {
        int sum = 0;
         
        for (int i=1; i<=n; i++)
            for (int j=i; j<=n; j++)
                sum = sum + i*j;
                 
        return sum;
    }
     
    // Driver code
    public static void Main()
    {
         
        int n = 5;
         
        System.Console.WriteLine(findSum(n));
    }
}
 
// This code is contributed by mits.


PHP


Javascript


C++
// Efficient CPP program to find sum
// of given series.
#include 
using namespace std;
 
long long int findSum(int n)
{
   long long int multiTerms = n * (n + 1) / 2;
 
   // Sum of multiples of 1 is 1 * (1 + 2 + ..)
   long long int sum = multiTerms;
 
   // Adding sum of multiples of numbers other
   // than 1, starting from 2.
   for (int i=2; i<=n; i++)
   {
       // Subtract previous number
       // from current multiple.
       multiTerms = multiTerms - (i - 1);
 
       // For example, for 2, we get sum
       // as (2 + 3 + 4 + ....) * 2
       sum = sum + multiTerms * i;
   }
   return sum;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << findSum(n);
    return 0;
}


Java
// Efficient Java program to find sum
// of given series.
class GFG {
     
    static int findSum(int n)
    {
         
        int multiTerms = n * (n + 1) / 2;
         
        // Sum of multiples of 1 is 1 * (1 + 2 + ..)
        int sum = multiTerms;
         
        // Adding sum of multiples of numbers other
        // than 1, starting from 2.
        for (int i = 2; i <= n; i++)
        {
             
            // Subtract previous number
            // from current multiple.
            multiTerms = multiTerms - (i - 1);
         
            // For example, for 2, we get sum
            // as (2 + 3 + 4 + ....) * 2
            sum = sum + multiTerms*i;
        }
         
        return sum;
    }
     
    // Driver code
    public static void main(String[] args)
    {
         
        int n = 5;
         
        System.out.println(findSum(n));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal.


Python3
# Efficient Python3 program
# to find sum of given series.
 
def findSum(n) :
    multiTerms = n * (n + 1) // 2
     
    # Sum of multiples of 1 is 1 * (1 + 2 + ..)
    sm = multiTerms
 
    # Adding sum of multiples of numbers
    # other than 1, starting from 2.
    for i in range(2, n+1) :
         
        # Subtract previous number
        # from current multiple.
        multiTerms = multiTerms - (i - 1)
     
        # For example, for 2, we get sum
        # as (2 + 3 + 4 + ....) * 2
        sm = sm + multiTerms * i
     
    return sm
     
# Driver code
n = 5
print(findSum(n))
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find sum
// of given series.
using System;
class GFG {
     
    static int findSum(int n)
    {
         
        int multiTerms = n * (n + 1) / 2;
         
        // Sum of multiples of 1 is 1 * (1 + 2 + ..)
        int sum = multiTerms;
         
        // Adding sum of multiples of numbers other
        // than 1, starting from 2.
        for (int i = 2; i <= n; i++)
        {
             
            // Subtract previous number
            // from current multiple.
            multiTerms = multiTerms - (i - 1);
         
            // For example, for 2, we get sum
            // as (2 + 3 + 4 + ....) * 2
            sum = sum + multiTerms*i;
        }
         
        return sum;
    }
     
    // Driver code
    public static void Main()
    {
         
        int n = 5;
         
        Console.WriteLine(findSum(n));
    }
}
 
// This code is contributed by Mukul Singh.


PHP


C++
// Efficient CPP program to find sum
// of given series.
#include 
using namespace std;
 
long long int findSum(int n)
{
   return n*(n+1)*(n+2)*(3*n+1)/24;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << findSum(n);
    return 0;
}


Java
// Efficient Java program to find sum
// of given series.
class GFG {
     
    static int findSum(int n)
    {
        return n * (n + 1) * (n + 2) *
                                 (3 * n + 1) / 24;
    }
     
    // Driver code
    public static void main(String[] args)
    {
         
        int n = 5;
         
        System.out.println(findSum(n));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal.


Python3
# Efficient Python3 program to find 
# sum of given series.
 
def findSum(n):
 
    return n * (n + 1) * (n + 2) * (3 * n + 1) / 24
 
# Driver code
n = 5
print(int(findSum(n)))
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// Efficient C# program
// to find sum of given
// series.
using System;
 
class GFG
{
static int findSum(int n)
{
    return n * (n + 1) * (n + 2) *
                 (3 * n + 1) / 24;
}
 
// Driver code
static public void Main ()
{
    int n = 5;
     
    Console.WriteLine(findSum(n));
}
}
 
// This code is contributed
// by ajit.


PHP


输出:
140

时间复杂度: O(n ^ 2)。
方法2(更好)
我们可以在给定的问题中观察以下情况。
1与从1到n的所有数字相乘。
2与2到n的所有数字相乘。
…………………………………………
…………………………………………
我乘以从i到n的所有数字。
我们计算前n个自然数之和,这是我们的第一个项。对于其余项,我们将i与i到n的数字之和相乘。我们通过在每次迭代中从初始总和中减去i来跟踪该总和。

C++

// Efficient CPP program to find sum
// of given series.
#include 
using namespace std;
 
long long int findSum(int n)
{
   long long int multiTerms = n * (n + 1) / 2;
 
   // Sum of multiples of 1 is 1 * (1 + 2 + ..)
   long long int sum = multiTerms;
 
   // Adding sum of multiples of numbers other
   // than 1, starting from 2.
   for (int i=2; i<=n; i++)
   {
       // Subtract previous number
       // from current multiple.
       multiTerms = multiTerms - (i - 1);
 
       // For example, for 2, we get sum
       // as (2 + 3 + 4 + ....) * 2
       sum = sum + multiTerms * i;
   }
   return sum;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << findSum(n);
    return 0;
}

Java

// Efficient Java program to find sum
// of given series.
class GFG {
     
    static int findSum(int n)
    {
         
        int multiTerms = n * (n + 1) / 2;
         
        // Sum of multiples of 1 is 1 * (1 + 2 + ..)
        int sum = multiTerms;
         
        // Adding sum of multiples of numbers other
        // than 1, starting from 2.
        for (int i = 2; i <= n; i++)
        {
             
            // Subtract previous number
            // from current multiple.
            multiTerms = multiTerms - (i - 1);
         
            // For example, for 2, we get sum
            // as (2 + 3 + 4 + ....) * 2
            sum = sum + multiTerms*i;
        }
         
        return sum;
    }
     
    // Driver code
    public static void main(String[] args)
    {
         
        int n = 5;
         
        System.out.println(findSum(n));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal.

Python3

# Efficient Python3 program
# to find sum of given series.
 
def findSum(n) :
    multiTerms = n * (n + 1) // 2
     
    # Sum of multiples of 1 is 1 * (1 + 2 + ..)
    sm = multiTerms
 
    # Adding sum of multiples of numbers
    # other than 1, starting from 2.
    for i in range(2, n+1) :
         
        # Subtract previous number
        # from current multiple.
        multiTerms = multiTerms - (i - 1)
     
        # For example, for 2, we get sum
        # as (2 + 3 + 4 + ....) * 2
        sm = sm + multiTerms * i
     
    return sm
     
# Driver code
n = 5
print(findSum(n))
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find sum
// of given series.
using System;
class GFG {
     
    static int findSum(int n)
    {
         
        int multiTerms = n * (n + 1) / 2;
         
        // Sum of multiples of 1 is 1 * (1 + 2 + ..)
        int sum = multiTerms;
         
        // Adding sum of multiples of numbers other
        // than 1, starting from 2.
        for (int i = 2; i <= n; i++)
        {
             
            // Subtract previous number
            // from current multiple.
            multiTerms = multiTerms - (i - 1);
         
            // For example, for 2, we get sum
            // as (2 + 3 + 4 + ....) * 2
            sum = sum + multiTerms*i;
        }
         
        return sum;
    }
     
    // Driver code
    public static void Main()
    {
         
        int n = 5;
         
        Console.WriteLine(findSum(n));
    }
}
 
// This code is contributed by Mukul Singh.

的PHP


输出:
140

时间复杂度: O(n)。
方法3(高效)
整个计算可以在O(1)时间内完成,因为总和有一个封闭形式的表达式。让i和j从1到n。我们要计算所有i和j上的S = sum(i * j),以使i <= j,否则我们将有重复项,例如2 * 3 +…+ 3 * 2;另一方面,让i = j是可以的,这给了我们2 * 2 +…这完全是问题说明。 (对不起,如果我的记法很奇怪。)
现在,总和S中有两种术语:i = j(正方形,用S1表示)和ij总是,但对称地取值相同:2 * S2 =(sum i)^ 2 –总和(i ^ 2)。注意2 * S2 = S3 – S1,因为后者之和就是S1;以前的总和(表示为S3)在这里是新的,但也有一个封闭形式的解决方案。现在我们可以完全消除混合项:S = S1 + S2 =(S1 + S3)/ 2。
由于sum(i)= n *(n + 1)/ 2,我们得到S3 = n * n *(n + 1)*(n + 1)/ 4;同样对于平方和:S1 = n *(2 * n + 1)*(n + 1)/ 6。最终表达式简化为:
S = n *(n + 1)*(n + 2)*(3 * n + 1)/ 24。 (作为练习,您可能需要证明分子确实可以被24整除。)

C++

// Efficient CPP program to find sum
// of given series.
#include 
using namespace std;
 
long long int findSum(int n)
{
   return n*(n+1)*(n+2)*(3*n+1)/24;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << findSum(n);
    return 0;
}

Java

// Efficient Java program to find sum
// of given series.
class GFG {
     
    static int findSum(int n)
    {
        return n * (n + 1) * (n + 2) *
                                 (3 * n + 1) / 24;
    }
     
    // Driver code
    public static void main(String[] args)
    {
         
        int n = 5;
         
        System.out.println(findSum(n));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal.

Python3

# Efficient Python3 program to find 
# sum of given series.
 
def findSum(n):
 
    return n * (n + 1) * (n + 2) * (3 * n + 1) / 24
 
# Driver code
n = 5
print(int(findSum(n)))
 
# This code is contributed by Smitha Dinesh Semwal.

C#

// Efficient C# program
// to find sum of given
// series.
using System;
 
class GFG
{
static int findSum(int n)
{
    return n * (n + 1) * (n + 2) *
                 (3 * n + 1) / 24;
}
 
// Driver code
static public void Main ()
{
    int n = 5;
     
    Console.WriteLine(findSum(n));
}
}
 
// This code is contributed
// by ajit.

的PHP


输出:
140

时间复杂度: O(1)。
感谢diprey1提出了这种有效的解决方案。