Challenge 20 - Finding the nth Triangular Number
In this challenge, you are going to implement the TriangularNumbers
function which takes in n
and returns the nth
triangular number.
n = 1
result = 1
n = 3
result = 6
See the Solution
Feel free to have a look at the forum discussion thread for this challenge and contribute with your own solutions here - Challenge 20 - Triangular Numbers
View Solution
package main
import "fmt"
func TriangularNumber(n int) int {
return (n * (n+1))/2
}
func main() {
fmt.Println("Returning the 'nth' triangular number")
number := TriangularNumber(3)
fmt.Println(number) // '6'
}
Further Reading:
If you like this challenge then you may also appreciate some of the following articles on the site: