Codewars|Sum of odd numbers

105 阅读1分钟

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

思路:

             1                       ---> 1
          3     5                    ---> 8   = 2^3
       7     9    11                 ---> 27  = 3^3
   13    15    17    19              ---> 64  = 4^3 
21    23    25    27    29           ---> 125 = 5^3
...

Solution:

rowSumOddNumbers = n => n ** 3;