Find the sum of the first Nth Centered Tridecagonal Numbers
Last Updated :
19 Sep, 2022
Given a number N, the task is to find the sum of first N Centered tridecagonal number.
A Centered tridecagonal number represents a dot at the center and other dots surrounding the center dot in the successive tridecagonal(13 sided polygon) layer. The first few Centered tridecagonal numbers are 1, 14, 40, 79 …
Examples:
Input: N = 3
Output: 55
Explanation:
1, 14 and 40 are the first three Centered tridecagonal number.
1 + 14 + 40 = 55.
Input: N = 5
Output: 265
Approach:
- Initially, we need to create a function which will help us to calculate the Nth Centered tridecagonal number.
- Now, Run a loop starting from 1 to N, and find the Centered tridecagonal numbers in this range.
- Add all the above calculated Centered tridecagonal numbers.
- Finally, display the sum of the first N Centered tridecagonal numbers.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int Centered_tridecagonal_num( int n)
{
return (13 * n * (n - 1) + 2) / 2;
}
int sum_Centered_tridecagonal_num( int n)
{
int summ = 0;
for ( int i = 1; i <= n; i++)
{
summ += Centered_tridecagonal_num(i);
}
return summ ;
}
int main()
{
int n = 5;
cout << sum_Centered_tridecagonal_num(n)
<< endl;
return 0;
}
|
Java
class GFG{
public static int Centered_tridecagonal_num( int n)
{
return ( 13 * n * (n - 1 ) + 2 ) / 2 ;
}
public static int sum_Centered_tridecagonal_num( int n)
{
int summ = 0 ;
for ( int i = 1 ; i <= n; i++)
{
summ += Centered_tridecagonal_num(i);
}
return summ ;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(sum_Centered_tridecagonal_num(n));
}
}
|
Python3
def Centered_tridecagonal_num(n):
return ( 13 * n *
(n - 1 ) + 2 ) / / 2
def sum_Centered_tridecagonal_num(n) :
summ = 0
for i in range ( 1 , n + 1 ):
summ + = Centered_tridecagonal_num(i)
return summ
if __name__ = = '__main__' :
n = 5
print (sum_Centered_tridecagonal_num(n))
|
C#
using System;
class GFG{
public static int Centered_tridecagonal_num( int n)
{
return (13 * n * (n - 1) + 2) / 2;
}
public static int sum_Centered_tridecagonal_num( int n)
{
int summ = 0;
for ( int i = 1; i <= n; i++)
{
summ += Centered_tridecagonal_num(i);
}
return summ;
}
public static void Main()
{
int n = 5;
Console.WriteLine(sum_Centered_tridecagonal_num(n));
}
}
|
Javascript
<script>
function Centered_tridecagonal_num(n)
{
return (13 * n * (n - 1) + 2) / 2;
}
function sum_Centered_tridecagonal_num(n)
{
let summ = 0;
for (let i = 1; i <= n; i++)
{
summ += Centered_tridecagonal_num(i);
}
return summ ;
}
let n = 5;
document.write(sum_Centered_tridecagonal_num(n));
</script>
|
Time complexity: O(N).
Auxiliary Space: O(1) as it is using constant space for variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...