Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 874 Bytes

sum-of-odd-numbers.md

File metadata and controls

47 lines (35 loc) · 874 Bytes

Sum of odd numbers 7 Kyu

LINK TO THE KATA - ARRAYS LISTS MATHEMATICS FUNDAMENTALS

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

Solutions

const rowSumOddNumbers = n => {
  const firstOdd = n * (n - 1) + 1

  let sum = 0

  for (let i = 0; i < n; i++) {
    sum += firstOdd + 2 * i
  }

  return sum
}
const rowSumOddNumbers = n => n * n * n