# How to Calculate the Sum of the Numbers in the Nth row of a Triangle in Golang

## The challenge

Given the triangle of consecutive odd numbers:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```             1
3     5
7     9    11
13    15    17    19
21    23    25    27    29
...```Code language: plaintext (plaintext)```

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```Code language: plaintext (plaintext)```

## The solution in Golang

Option 1:

``````package solution

func RowSumOddNumbers(n int) int {
return n * n * n
}
```Code language: Go (go)```

Option 2:

``````package solution

import "math"

func RowSumOddNumbers(n int) int {
return int(math.Pow(float64(n), 3))
}
```Code language: Go (go)```

Option 3:

``````package solution

func RowSumOddNumbers(n int) int {
res := 0
for i := (n - 1) * n + 1; i < n * (n + 1); i = i + 2 {
res = res + i
}
return res
}
```Code language: Go (go)```

## Test cases to validate our solution

``````package solution_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Fixed tests", func() {
It("Testing for 1", func() { Expect(RowSumOddNumbers(1)).To(Equal(1)) })
It("Testing for 2", func() { Expect(RowSumOddNumbers(2)).To(Equal(8)) })
It("Testing for 13", func() { Expect(RowSumOddNumbers(13)).To(Equal(2197)) })
It("Testing for 19", func() { Expect(RowSumOddNumbers(19)).To(Equal(6859)) })
It("Testing for 41", func() { Expect(RowSumOddNumbers(41)).To(Equal(68921)) })
It("Testing for 42", func() { Expect(RowSumOddNumbers(42)).To(Equal(74088)) })
It("Testing for 74", func() { Expect(RowSumOddNumbers(74)).To(Equal(405224)) })
It("Testing for 86", func() { Expect(RowSumOddNumbers(86)).To(Equal(636056)) })
It("Testing for 93", func() { Expect(RowSumOddNumbers(93)).To(Equal(804357)) })
It("Testing for 101", func() { Expect(RowSumOddNumbers(101)).To(Equal(1030301)) })
})
```Code language: Go (go)```
Tags:
Subscribe
Notify of