Given a number N, the task is to find the sum of the first N Centered nonadecagonal numbers.
The first few Centered nonadecagonal number are 1, 20, 58, 115, 191, 286 …
Examples :
Input : N = 2 Output : 21 Explanation : 1, 20 are the first two centered nonadecagonal numbers. Input : N = 3 Output : 79
Approach :
- Initially, we need to create a function which will help us to calculate the Nth centered nonadecagonal number.
- Now, run a loop starting from 1 to N, to find ith centered nonadecagonal number.
- Add all the above calculated centered nonadecagonal numbers.
- Finally, display the sum of the first N centered nonadecagonal numbers.
Implementation :
# Python program to find the sum
# of the first N centered
# nonadecagonal numbers
# Function to find N-th
# centered nonadecagonal
# number
def center_nonadecagonal_num(n):
# Formula to calculate
# nth centered nonadecagonal
# number
return (19 * n * n - 19 * n + 2) // 2
# Function to find the
# sum of the first N
# centered nonadecagonal
# numbers
def sum_center_nonadecagonal_num(n) :
# Variable to store
# the sum
summ = 0
# Iterate through the range
# 1 to N
for i in range(1, n + 1):
summ += center_nonadecagonal_num(i)
return summ
# Driver code
if __name__ == '__main__' :
n = 2
print(sum_center_nonadecagonal_num(n))
Time Complexity : O(N).