물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Sum of odd numbers

BEstyle 2022. 9. 20. 12:36

DESCRIPTION:

Given the triangle of consecutive odd numbers:

             1
          3     5
       7     9    11
   13    15    17    19
21    23    25    27    29
...

Calculate the sum of the numbers in the nth row of this triangle (starting at index 1) e.g.: (Input --> Output)

1 -->  1
2 --> 3 + 5 = 8

def row_sum_odd_numbers(n):
    print("num:" ,n)
    row_start=1-n+n**2
    print(row_start)
    ans=0
    for i in range(n):
        ans+=row_start+2*i
    print(ans)
    return ans

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Persistent Bugger  (0) 2022.09.21
String ends with?  (0) 2022.09.20
Duplicate Encoder  (0) 2022.09.20
Are they the "same"?  (0) 2022.09.20
Isograms  (1) 2022.09.19