The positive integers are arranged in increasing order in a triangle, as shown*. Each row contains one more number than the previous row. The sum of the numbers in the row that contains the number 400 is (the question gives a variety of options).
*Here is my best representation of the picture of the triangle:
1 2 3 4 5 6 7 8 9 10 ...
I used a brute force approach in C++:
#include #include using namespace std; vector< int > line; int main (int argc, const char * argv[]) { int i = 0; //The current number. int number_in_row = 1; //Number of numbers contained in a row of the triangle. int row_counter = 0; //Keeps track of what element in the row I'm on. bool row_with_400 = false; //Checks to see if I reached the row that has the number 400 in it. bool did_finish_row = false; //Checks to see if I got all the elements from the 400th row. bool check = false; //Works with did_finish_row. while (true) { i++; //Keep incrementing through the numbers if (i == 400) { //Check if i is = to 400 and then indicate it is on the row with 400. row_with_400 = true; check = true; //Used to make sure that the thing finishes the row then breaks out of the loop. } line.push_back(i); //Put the contents of the line into a vector. row_counter++; //Indicate you are moving to the next element in the row in the triangle if (row_counter == number_in_row) { //Checks to see if I got to end of row row_counter = 0; number_in_row++; if (check == true) { //If on row with 400 indicate that I finished storing all those elements. did_finish_row = true; } if (did_finish_row != true) { //Clear unless I am on the row with 400. line.clear(); } } if (row_with_400 == true && did_finish_row == true) { //Break if I finished the row with 400. break; } } int final_number = 0; for (vector::iterator i = line.begin(); i != line.end(); ++i ) { final_number = final_number + *i; //Add up all the elements of the row with 400. } cout << final_number; //Print it out. }
Basically, I kept track of which row I was on, got to the row with 400 and added up the contents with that. Look at the comments in the code to get a better sense of what I did.
First off I get the answer to be: 10990. Is that right? Second of all, I got this from a sheet that didn't allow people to program. Is there a less brute force method, something that needs only pen or pencil? Could I have a hint at least as how to do that?