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.

Pages: 1 2

4 Responses to “Happy New Year!”

  1. matthew said

    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:

    import heapq
    
    def makediff(a,b,s,f):
        if b == len(s): s.append(f(b,s[-1]))
        return (s[b]-s[a], a, b)
    
    def find2016(n,f,c):
        s = [n]
        diff = lambda a,b: makediff(a,b,s,f)
        q = [diff(0,1)]
        while True:
            (value,a,b) = heapq.heappop(q)
            if value == 2016:
                print("%d = s[%d] - s[%d] = %d - %d (%s)"
                      %(value,b,a,s[b],s[a],c))
            if value > 2016: break
            if b == a+1: heapq.heappush(q,diff(b,b+1))
            heapq.heappush(q,diff(a,b+1))
    
    find2016(1,lambda n,m: 2*m,"powers of 2")
    find2016(0,lambda n,m: n+m,"triangular")
    find2016(0,lambda n,m: n*n*n+m,"sums of cubes")
    
    2016 = s[11] - s[5] = 2048 - 32 (powers of 2)
    2016 = s[63] - s[0] = 2016 - 0 (triangular)
    2016 = s[106] - s[85] = 5671 - 3655 (triangular)
    2016 = s[228] - s[219] = 26106 - 24090 (triangular)
    2016 = s[291] - s[284] = 42486 - 40470 (triangular)
    2016 = s[673] - s[670] = 226801 - 224785 (triangular)
    2016 = s[2016] - s[2015] = 2033136 - 2031120 (triangular)
    2016 = s[9] - s[2] = 2025 - 9 (sums of cubes)
    

    Have a good 2016…

  2. Atif Farooq said

    #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);
    }

  3. r. clayton said

    A couple of solutions in Racket.

  4. Praveen said

    The 63rd triangular number is 2016.
    for nth triangular number the formula is n*(n+1)/2;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: