Posts

Showing posts with the label address

Updated Nested C++ Recursion - Part Two

Image
Part One - Recursion Basics explained a problem with the engineering in my earlier Nested C++ Recursion article. Part Two here describes the updated C++ engineering. You can find all of the updated Visual Studio solutions at this GitHub resource . This image shows how the original outerRecLambda function works: Say that the outerRecLambda function at line 42 from Part 1 of this article starts at solution x array values [1, 14] . The outerRecLambda function resets the array values to [14, 15] as shown in the image. Instead, it should reset the array to [1, 15] . This outerRecLambda function works correctly:  1.   std::function<int* (int[])> outerRecLambda;  2.   outerRecLambda = [&outerRecLambda, &innerRecLambda](int x[]) {  3.      bool outer_is_sum_pentnum = is_pentnum(gen_pentnum(x[1]) + gen_pentnum(x[0]));  4.      bool outer_is_diff_pentnum = is_...

Updated Nested C++ Recursion - Part One

Image
I n an earlier article - Nested C++ Recursion - I described C++ nested recursive solutions to the Euler 44 Pentagonal Numbers problem. The basic engineering for those solutions works correctly, in the sense that they return the correct results. However, I realized that the engineering has a fatal flaw. This two-part article will explain the flaw, and how to fix that flaw. This GitHub resource hosts the updated C++ solutions described in this article. As described in part six of the earlier article, this solution relies on arrays:  1.   #include "stdafx.h"  2.   #include <chrono>  3.   #include <ctime>  4.   #include <iostream>  5.   #include <functional>  6.   #include <math.h>  7.   #include <vector>  8.     9.   bool is_pentnum(int x) { 10.   ...

Part Eight - Euler 44 Nested Recursion: Classes

Image
Nested C++ Recursion - Introduction (Main Article Page) Part Seven - Euler 44 Nested Recursion: Vectors described a vector-based nested C++ recursive solution to the Euler 44 Pentagonal Numbers problem. Part Eight here will explain how C++ classes can increase recursion engineering efficiency. We might want to break out the gen_pentnum() and is_pentnum() functions into one class, in a separate file. This would increase the solution file flexibility, and make the engineering more modular. The next example shows how to do it. File pentnum_functions.cpp, available in this resource , builds a class called pentnum_functions:  1.   #include "stdafx.h"  2.   #include "pentnum_functions.h"  3.   #include <chrono>  4.   #include <ctime>  5.   #include <iostream>  6.   #include <functional>  7.   #include <math.h> ...

Part Five - Stack Configuration

Image
Nested C++ Recursion - Introduction (Main Article Page) Part Four - Building A Visual Studio C++ Project showed how to build Visual Studio C++ solutions with the code files available at the GitHub resource . Here, we'll see how to configure the stack for the software we'll build with those code files. After creating a Visual Studio solution based on the source code files, right-click the project in Solution Explorer, and drill down to Properties, as shown in this screenshot: This will open the Property Pages window. At the left, drill down to            Configuration Properties -> Linker -> System as shown here: Visual Studio defaults new projects to a Stack Reserve Size value of 1 meg, but this project needs much more. For the development of this solution on the Windows 10 laptop, the Stack Reserve Size option has a setting of 6000000 ( units ) , as shown: Different devices might need different Stack Reserve Size value...

Part Nine - Memory Consumption

Image
Nested C++ Recursion - Introduction (Main Article Page) Part Eight - Euler 44 Nested Recursion: Classes how C++ classes can increase recursion engineering efficiency. Part Eight here will show how to measure recursive C++ software memory consumption, focusing on the C++ software described in this article. As business rules change, we can easily add new features to lambdas. In this example, we’ll start with a two-file, integer vector C++ solution to Project Euler #44. We want to add code that measures the stack resources the solution uses. File pentnum_functions.cpp, available in this resource , builds a class called pentnum_functions:  1.   #include "stdafx.h"  2.   #include "pentnum_functions.h"  3.   #include <chrono>  4.   #include <ctime>  5.   #include <iostream>  6.   #include <functional>  7.   #include <mat...

Part Six - An Euler 44 Nested Recursive Solution

Image
Nested C++ Recursion - Introduction (Main Article Page) Part Five - Stack Configuration showed how to configure the stack for the nested C++ recursion solution we'll build for the Euler 44 Pentagonal Numbers problem. Here, we'll explore an actual nested C++ recursive solution to that problem. This C++ recursive solution available here calculates the Euler Project #44 solution with nested recursive lambdas:  1.   #include "stdafx.h"  2.   #include <chrono>  3.   #include <ctime>  4.   #include <iostream>  5.   #include <functional>  6.   #include <math.h>  7.   #include <vector>  8.     9.   bool is_pentnum(int x) { 10.    11.      float testval = (float)(sqrt(24.0f*(float)(x)+1.0f) + 1.0f) / 6.0f; 12.  ...

Part Seven - Euler 44 Nested Recursion: Vectors

Image
Nested C++ Recursion - Introduction (Main Article Page) Part Six - An Euler 44 Nested Recursive Solution explored an actual nested C++ recursive solution to the Euler 44 Pentagonal Numbers problem. Part Seven here will show a vector-based nested C++ recursive solution to that problem. The lambda engineering seen here can handle integer vector parameters. C++ vectors operate a lot like arrays, but they add more properties and functions. This recursive C++ Euler 44 solution uses vectors:  1.   #include "stdafx.h"  2.   #include <chrono>  3.   #include <ctime>  4.   #include <iostream>  5.   #include <functional>  6.   #include <math.h>  7.   #include <vector>  8.     9.   bool is_pentnum(int x) { 10. 11.   float testval = (float)(sqrt(24.0f*(float)(...

Part Four - Building A Visual Studio C++ Project

Image
Nested C++ Recursion - Introduction (Main Article Page) Part Three - Euler 44 Solution Groundwork described, in a very general way, the solutions we'll examine in this article. In Part Four - Building A Visual Studio C++ Project here, we'll see how to build a Visual Studio solution with the sample files available at the GitHub resource , and we'll learn how to shortcut the steps common to all of those solutions. In the GitHub resource , the solutions can have two or three CPP files, combined with two or three .h (header) files. We'll see how to build a finished Visual Studio solution with a two-CPP file, two-.h file solution. The approach extends to the other available solutions. This GitHub resource shows the component files we'll use to build a finished example C++ solution in Visual Studio. First, launch Visual Studio, and drill down to create a new project: In the New Project pane, use these values    - Visual C++ -> Win32 (project type)    -...