Triangular Number Sum

12Apr13

In the twelve months from March 2013 to March 2014, I programmed solutions to the problems posted on the Contest Coding blog run by Lewis Cornwall, solving 33 problems (out of 47) and achieving a position of 4th on the leaderboard (out of 23).

As that blog has now been discontinued, I’m posting here the solutions I programmed to those problems.

Triangular Number Sum

Triangular numbers are the sequence of numbers generated by forming objects inside a triangle.

1, 3, 6, 10, 15, 21…

Find the sum of all 3-digit triangular numbers.

Solution and answer (Triangular Number Sum.c):

Includes: printDateAndTime.

/*
Solution and answer for problem "Triangular Number Sum" (12th April 2013) of http://ContestCoding.WordPress.com/

Fri Apr 12 09:00:00 2013

The sum of all three-digit triangular numbers is: 14725.

Fri Apr 12 09:00:00 2013

Solution programmed in C using Leonardo IDE 3.4.1; solution took ~1s to run on a 80MHz PowerPC 601.
*/

#include "printDateAndTime.h"
#include <stdio.h>

void main( void )
{
  long n = 0, b = 1, sum_of_three_digit_triangular_numbers = 0;

  p_PrintDateAndTime();

  do
  {
    n += b;
    b ++;
    if(( n >= 100 ) && ( n <= 999 ))
      sum_of_three_digit_triangular_numbers += n;
  }
  while( n < 999 );
  printf( "\nThe sum of all three-digit triangular numbers is: %ld.\n\n", sum_of_three_digit_triangular_numbers );

  p_PrintDateAndTime();
}
Advertisements


No Responses Yet to “Triangular Number Sum”

  1. Leave a Comment

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: