Given a number N, the task is to find the sum of the first N decagonal Numbers.
The first few decagonal numbers are 1, 10, 27, 52, 85, 126, 175,…
Examples :
Input : N = 2 Output : 11 Explanation : 1, 10 are the first two decagonal numbers. Input : N = 3 Output : 38
Approach :
- Initially, we need to create a function which will help us to calculate the Nth decagonal number.
- Now, run a loop starting from 1 to N, to find ith decagonal numbers.
- Add all the above calculated decagonal numbers.
- Finally, display the sum of the first N decagonal numbers.
Implementation :
# Python program to find the
# sum of the first N
# decagonal numbers
# Function to find the
# N-th decagonal
# number
def decagonal_num(n):
# Formula to calculate
# nth decagonal
# number
return (8 * n * n -
6 * n) // 2
# Function to find the
# sum of the first N
# decagonal number
def sum_decagonal_num(n) :
# Variable to store
# the sum
summ = 0
# Iterating in the range
# 1 to N
for i in range(1, n + 1):
summ += decagonal_num(i)
return summ
# Driver code
if __name__ == '__main__' :
n = 2
print(sum_decagonal_num(n))
Time Complexity : O(N).