Find the sum of the first N Centered Octadecagonal Numbers

Given a number N, the task is to find the sum of first N Centered Octadecagonal numbers.

Examples:

Input: N = 3
Output: 75
Explanation:
1, 19 and 55 are the first three centered octadecagonal numbers.

Input: N = 10
Output: 298

Approach:



  1. Initially, we need to create a function which will help us to calculate the Nth centered octadecagonal number.
  2. Now, run a loop starting from 1 to N, to find ith centered octadecagonal numbers.
  3. Add all the above calculated centered octadecagonal numbers.
  4. Finally, display the sum of the first N centered octadecagonal numbers.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the sum of the 
// first N centered octadecagonal numbers
#include<bits/stdc++.h> 
using namespace std; 
  
// Function to find the N-th centered
// octadecagonal number
int center_octadecagon_num(int n)
{
  
    // Formula to calculate
    // nth centered octadecagonal
    // number
    return (9 * n * n - 9 * n + 1);
}
  
// Function to find the sum of the first
// N centered octadecagonal numbers
int sum_center_octadecagon_num(int n)
{
  
    // Variable to store
    // the sum
    int summ = 0;
  
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
        summ += center_octadecagon_num(i);
    }
    return summ;
}
  
// Driver Code
int main()
{
    int n = 3;
  
    cout << (sum_center_octadecagon_num(n));
    return 0;
}
  
// This code is contributed by Rajput-Ji

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the sum of the  
// first N centered octadecagonal numbers
class GFG {
      
// Function to find the N-th centered
// octadecagonal number
static int center_octadecagon_num(int n)
{
  
    // Formula to calculate
    // nth centered octadecagonal
    // number
    return (9 * n * n - 9 * n + 1);
}
  
// Function to find the sum of the first
// N centered octadecagonal numbers
static int sum_center_octadecagon_num(int n)
{
  
    // Variable to store
    // the sum
    int summ = 0;
  
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
       summ += center_octadecagon_num(i);
    }
    return summ;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 3;
  
    System.out.println(sum_center_octadecagon_num(n));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the sum of 
# the first N Centered Octadecagonal 
# numbers
  
# Function to find the N-th
# Centered octadecagonal 
# number 
def center_octadecagon_num(n): 
  
    # Formula to calculate  
    # nth centered octadecagonal 
    # number
    return (9 * n * n - 9 * n + 1)
      
    
# Function to find the sum of 
# the first N Centered 
# octadecagonal numbers
def sum_center_octadecagon_num(n) : 
      
    # Variable to store 
    # the sum
    summ = 0
      
    # Iterating through the range 
    # 1 to N
    for i in range(1, n + 1):
  
        summ += center_octadecagon_num(i)
      
    return summ
    
# Driver code 
if __name__ == '__main__'
            
    n = 3
      
    print(sum_center_octadecagon_num(n)) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the sum of the 
// first N centered octadecagonal numbers
using System;
  
class GFG {
      
// Function to find the N-th centered
// octadecagonal number
static int center_octadecagon_num(int n)
{
  
    // Formula to calculate
    // nth centered octadecagonal
    // number
    return (9 * n * n - 9 * n + 1);
}
  
// Function to find the sum of the first
// N centered octadecagonal numbers
static int sum_center_octadecagon_num(int n)
{
  
    // Variable to store
    // the sum
    int summ = 0;
  
    // Iterating through the range
    // 1 to N
    for(int i = 1; i < n + 1; i++)
    {
        summ += center_octadecagon_num(i);
    }
    return summ;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 3;
  
    Console.WriteLine(sum_center_octadecagon_num(n));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Output:

75

Time Complexity: O(N).

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

Small things always make you to think big

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 : sapnasingh4991, Rajput-Ji

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.