Find the sum of the first N Centered Decagonal Numbers
Given a number N, the task is to find the sum of the first N Centered Decagonal Numbers.
The first few Centered decagonal numbers are 1, 11, 31, 61, 101, 151 …
Examples:
Input: N = 3
Output: 43
Explanation:
1, 11 and 31 are the first three Centered decagonal numbers.
Input: N = 5
Output: 205
Approach:
- Initially, we need to create a function which will help us to calculate the Nth Centered decagonal number.
- Now, run a loop starting from 1 to N, to find the ith Centered decagonal number.
- Add all the above calculated Centered decagonal numbers.
- Finally, display the sum of 1st N Centered decagonal numbers.
Below is the implementation of the above approach:
C++
// C++ program to find the sum of the // first N centered decagonal number #include <bits/stdc++.h> using namespace std; // Function to find the N-th // centered decagonal number int Centered_decagonal_num( int n) { // Formula to calculate nth // centered_decagonal number // & return it into main function. return (5 * n * n - 5 * n + 1); } // Function to find the sum of // the first N centered decagonal // numbers int sum_Centered_decagonal_num( int n) { // Variable to store // the sum int summ = 0; // Iterating through the range for ( int i = 1; i < n + 1; i++) { summ += Centered_decagonal_num(i); } return summ; } // Driver code int main() { int n = 5; // Display first Nth // centered_decagonal number cout << (sum_Centered_decagonal_num(n)); return 0; } // This code is contributed by PrinciRaj1992 |
Java
// Java program to find the sum of the // first N centered decagonal number class GFG { // Function to find the N-th // centered decagonal number static int Centered_decagonal_num( int n) { // Formula to calculate nth // centered_decagonal number // & return it into main function. return ( 5 * n * n - 5 * n + 1 ); } // Function to find the sum of // the first N centered decagonal // numbers static int sum_Centered_decagonal_num( int n) { // Variable to store // the sum int summ = 0 ; // Iterating through the range for ( int i = 1 ; i < n + 1 ; i++) { summ += Centered_decagonal_num(i); } return summ; } // Driver code public static void main(String[] args) { int n = 5 ; // Display first Nth // centered_decagonal number System.out.println(sum_Centered_decagonal_num(n)); } } // This code is contributed by sapnasingh4991 |
Python3
# Python3 program to find the sum of # the first N centered # decagonal number # Function to find the N-th # centered decagonal number def Centered_decagonal_num(n): # Formula to calculate # nth Centered_decagonal # number & return it # into main function. return ( 5 * n * n - 5 * n + 1 ) # Function to find the # sum of the first N # Centered decagonal # numbers def sum_Centered_decagonal_num(n) : # Variable to store # the sum summ = 0 # Iterating through the range for i in range ( 1 , n + 1 ): summ + = Centered_decagonal_num(i) return summ # Driver code if __name__ = = '__main__' : n = 5 # display first Nth # Centered_decagonal number print (sum_Centered_decagonal_num(n)) |
C#
// C# program to find the sum of the // first N centered decagonal number using System; class GFG { // Function to find the N-th // centered decagonal number static int Centered_decagonal_num( int n) { // Formula to calculate nth // centered_decagonal number // & return it into main function. return (5 * n * n - 5 * n + 1); } // Function to find the sum of // the first N centered decagonal // numbers static int sum_Centered_decagonal_num( int n) { // Variable to store // the sum int summ = 0; // Iterating through the range for ( int i = 1; i < n + 1; i++) { summ += Centered_decagonal_num(i); } return summ; } // Driver code public static void Main(String[] args) { int n = 5; // Display first Nth // centered_decagonal number Console.WriteLine(sum_Centered_decagonal_num(n)); } } // This code is contributed by sapnasingh4991 |
Javascript
<script> // Javascript program to find the sum of the // first N centered decagonal number // Function to find the N-th // centered decagonal number function Centered_decagonal_num(n) { // Formula to calculate nth // centered_decagonal number // & return it into main function. return (5 * n * n - 5 * n + 1); } // Function to find the sum of // the first N centered decagonal // numbers function sum_Centered_decagonal_num(n) { // Variable to store // the sum let summ = 0; // Iterating through the range for (let i = 1; i < n + 1; i++) { summ += Centered_decagonal_num(i); } return summ; } let n = 5; // Display first Nth // centered_decagonal number document.write(sum_Centered_decagonal_num(n)); </script> |
Output:
205
Time Complexity: O(N).
Auxiliary Space: O(1) as it is using constant variables