Part Three - Euler 44 Solution Groundwork

Nested C++ Recursion - Introduction (Main Article Page)

Part Two - Pentagonal Numbers described Euler 44 Pentagonal Numbers - the problem we'll solve with nested C++ recursion. In Part Three - Euler 44 Solution Groundwork here, we'll examine the nested recursion engineering of the solutions we'll build.

Even small, complete Visual Studio solutions can weigh in at almost 100 megs. Often enough, however, the "relevant" source files for those solutions will have much smaller sizes. At this GitHub resource, download source code for the solutions described throughout this article. Those source code files have detailed comments. To save space, the corresponding code samples we’ll see in this article have almost no comments. All the recursive Euler 44 C++ solutions at the GitHub resource use two “helper” functions. The is_pentnum() function tests whether or not an input parameter x is a pentagonal number. It returns a boolean value:

bool is_pentnum(int x) {
   //   Test if parameter "x" is a pentagonal number.
   //   http://www.divye.in/2012/07/how-do-you-determine-if-number-n-is.html
   float testval = (float)(sqrt(24.0f*(float)(x)+1.0f) + 1.0f) / 6.0f;
   return (((testval - trunc(testval)) == 0.0) ? true : false);
};


The gen_pentnum() function maps an input parameter n to its integer pentagonal number, and returns that pentagonal number:

int gen_pentnum(int n) {
   //   Map parameter "n" to a pentagonal number.
   //   Pentagon number definition from
   //
   //      https://projecteuler.net/problem=44
   return (int)(((3 * pow((float)n, 2)) - (float)n) / 2);
};

All the solutions use similar code to output the results. That output code measures the software speed. Some solution versions operate on arrays, and some operate on integer vectors. The output code in each version is customized for the output as arrays or vectors, as needed.

All solutions have

1.   one or two source code files, and associated header files, for the solution engineering
2.   array or integer vector parameters for certain function calls
3.   stdafx.h and stdafx.cpp files
    4.   a targetver.h file

and the solutions vary items 1 and 2. Half of the solutions have code to measure use of stack resources. For comparison, one solution has code for loop engineering. Therefore, the GitHub resource has code for nine different solutions in total.

Here, we learned about C++ nested recursion solution engineering to the Euler 44 Pentagonal Numbers problem. Part Four - Building A Visual Studio C++ Project will explain how to build a Visual C++ solution with the example code files available at the GitHub resource.