Problem 68: Magic 5-gon ring

Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.
Working clockwise, and starting from the group of three with the numerically lowest external node (4,3,2 in this example),
each solution can be described uniquely. For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.

By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.

Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings. What is the maximum 16-digit string for a "magic" 5-gon ring?

My Algorithm

I split the graph into two parts:

an "inner ring"

and an "outer ring"

In the initial example, the inner ring consists of 1,2,3 while the outer ring contains 4,5,6.

Each cell of the inner ring is part of two lines. It is indexed from 0 to size - 1 (size is 3 for a 3-gon, 5 for a 5-gon, etc.).
Each cell of the outer ring is part of only one line. It is indexed from size to 2*size - 1.

The idea is to fill the inner ring using backtracking.
Whenever two out of three cells of a line are known, the third can be directly computed.
If that third number is already used, then we have to backtrack.

My function fillLine fills the inner ring: it tries all combination of available numbers for the cell at index pos.
Whenever it succeeds, it calls itself (recursion) to fill pos + 1.

All available numbers are stored in a bitmask called used. Number x is available if (used & (1 << x)) == 0.
At the end, result contains all valid n-gons.

Project Euler asks for the maximum string while Hackerrank asks for all strings in ascending order.
My solution prints the Hackerrank result and I manually have to choose the larger one (there are just two solutions).

The correct result will be printed when size = 5 and tripletSum = 14, that means the program input is "5 14".
There are solutions for "5 16", "5 17" and "5 19" but they are either lexicographically smaller or have 17 instead of 16 digits.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

This live test is based on the Hackerrank problem.

Input data (separated by spaces or newlines):

This is equivalent toecho "3 9" | ./68

Output:

(please click 'Go !')

Note: the original problem's input 5 14cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

The code contains #ifdefs to switch between the original problem and the Hackerrank version.Enable #ifdef ORIGINAL to produce the result for the original problem (default setting for most problems).

Benchmark

The correct solution to the original Project Euler problem was found in less than 0.01 seconds on an Intel® Core™ i7-2600K CPU @ 3.40GHz.
(compiled for x86_64 / Linux, GCC flags: -O3 -march=native -fno-exceptions -fno-rtti -std=gnu++11 -DORIGINAL)

Changelog

Hackerrank

Difficulty

25%
Project Euler ranks this problem at 25% (out of 100%).

Hackerrank describes this problem as hard.

Note:Hackerrank has strict execution time limits (typically 2 seconds for C++ code) and often a much wider input range than the original problem.In my opinion, Hackerrank's modified problems are usually a lot harder to solve. As a rule thumb: brute-force is rarely an option.

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !