Happy New Year!
January 1, 2016
The triangular numbers (A000217) are those numbers that can be formed by identical objects arranged in a triangle. For instance, 10 bowling pins can be formed in a triangle with sides of length 4, so the fourth triangular number is 10.
Your task is to write a program that computes the n th triangular number; what is the 63rd triangular number? When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Nice. 2016 is not only triangular (and hexagonal) but is also the difference between powers of 2 (2048 and 32) (it is 11111100000 in binary). Also it is the sum of consecutive cubes (so is the difference between two square hyperpyramid numbers). Also, being triangular, it is obviously also the difference between two triangular numbers (itself and 0, as well as triangle(2016) and triangle(2015)) but there are other ways (the number of ways is apparently known as the politeness of the number).
Here’s some Python (adapted from something I wrote for the “sum of consecutive squares” problem a while back) that uses a heap/priority queue to find such differences in an ascending sequence reasonably efficiently:
Have a good 2016…
#include
using namespace std;
int NaturalSum(int X);
void main(void)
{
cout << NaturalSum(63)<<endl;
}
int NaturalSum(int X)
{
if (X == 1)
return 1;
else return X + NaturalSum(X – 1);
}
A couple of solutions in Racket.
The 63rd triangular number is 2016.
for nth triangular number the formula is n*(n+1)/2;