Posts

Showing posts with the label Golang

Project Euler #44: A Recursive Golang Solution - Part Two

_________________________ I n Part One , I described  Project Euler #44  and an overall way to solve it. Here in Part Two, I will describe a recursive Golang solution for this problem. See that   solution    at GitHub . In an earlier article , I wrote a detailed explanation of recursion in a SQL Server 2005+ context. We can think of software that calls itself in a controlled way as recursive . Recursion involves a lot more than that, of course, but this description will work for now. A software system that repeatedly calls itself until a defined "base case" occurs means that we can substitute recursive code for a loop, because in a conventional loop, the code repeats a finite number of times. In Part One , we saw that a conventional Project Euler #44  solution would probably have two nested for-loops. Therefore, I figured that maybe I could nest two recursive structures, one to primarily handle P 0 and one to primarily handle P 1 . As we'll see, this wo...

Project Euler #44: A Recursive Golang Solution - Part One

Image
_________________________ Recursion A s I studied the Go language, I decided to build a Golang solution for Project Euler #44 - Pentagonal Numbers. A nested for-loop approach, in Go or any other language, would probably work. Then I got a bright idea: maybe I could build a strictly recursive Golang solution instead. Obviously, we should not rely on recursion as a first choice to solve problems. It has a large resource overhead and expense. A recursive solution can become tricky to build, and it can easily blow the stack . However, I wanted to push both myself and the Golang product. I built the solution with Go 1.7.4 and Webstorm 2016.3 on a Windows 7 PC. See the solution here at GitHub . For Project Euler #44, we have to find two pentagonal numbers that together, obey three rules:         The sum of the numbers must form a pentagonal number         The subtracted value (larger number - smaller number)       ...