Sum of the first N Pronic Numbers

Given a number N, the task is to find the sum of the first N Pronic Numbers.

The numbers that can be arranged to form a rectangle are called Rectangular Numbers (also known as Pronic numbers). The first few rectangular numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 . . . .

Examples:

Input: N = 4
Output: 20
Explanation:
0, 2, 6, 12 are the first 4 pronic numbers.

Input: N = 3
Output: 8



Approach:

    Let, the Nth term be denoted by TN. This problem can easily be solved by splitting each term as follows:

    S_N = 0 + 2 + 6 + 12 + 20 + 30......
    S_N = (0 * 1) + (1 * 2) + (2 * 3) + (3 * 4) + (4 * 5) + ......
    S_N = (0 * (0 + 1) + (1 * (1 + 1)) + (2 * (2 + 1)) + (3 * (3 + 1)) + (4 * (4 + 1)) + ......
    S_N = (0^2 + 0) + (1^2 + 1) + (2^2 + 2) + (3^2 + 3) + (4^2 + 4) +......
    S_N = (0 + 1 + 2 + 3 + 4 + ...T_N) + (0^2 + 1^2 + 2^2 + 3^2 + 4^2 + ... T_N)

    Therefore:

    SN = Sum of N Pronic Numbers
    S_N = [N*\frac{(N - 1)}{2}] + [N*\frac{(N - 1)*(2*N - 1)}{6}]

    Below is the implementation of the above approach:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ implementation to find 
    // sum of first N terms
    #include <bits/stdc++.h>
    using namespace std;
      
    // Function to calculate the sum
    int calculateSum(int N)
    {
      
        return N * (N - 1) / 2
               + N * (N - 1)
                     * (2 * N - 1) / 6;
    }
      
    // Driver code
    int main()
    {
        int N = 3;
      
        cout << calculateSum(N);
      
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java implementation implementation to find 
    // sum of first N terms 
    class GFG{ 
          
    // Function to calculate the sum 
    static int calculateSum(int N) 
          
        return N * (N - 1) / 2 + N * (N - 1) *
               (2 * N - 1) / 6
          
    // Driver code 
    public static void main (String[] args) 
        int N = 3
          
        System.out.println(calculateSum(N)); 
      
    // This code is contributed by Pratima Pandey 

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 implementation to find 
    # sum of first N terms
      
    # Function to calculate the sum
    def calculateSum(N):
      
        return (N * (N - 1) // 2 + 
                N * (N - 1) * (2 * 
                     N - 1) // 6);
      
    # Driver code
    N = 3;
    print(calculateSum(N));
      
    # This code is contributed by Code_Mech

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# implementation implementation to find 
    // sum of first N terms 
    using System;
    class GFG{ 
          
    // Function to calculate the sum 
    static int calculateSum(int N) 
          
        return N * (N - 1) / 2 + N * (N - 1) *
                            (2 * N - 1) / 6; 
          
    // Driver code 
    public static void Main() 
        int N = 3; 
          
        Console.Write(calculateSum(N)); 
      
    // This code is contributed by Code_Mech

    chevron_right

    
    

    Output:

    8
    

    Time complexity: O(1).

    Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



    Improved By : dewantipandeydp, Code_Mech

    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.