Project Euler 61 - Cyclical figurate numbers

Official link:

Thought Process

My idea for this problem was to create a hash map. I will explain how below.

  1. First thing to do is to create the all the triangle, square, pentagonal, hexagonal, heptagonal, octagonal, then I create one list all_shapes which is all the shape numbers

    • Note: along with generating the shape number I generate them as a tuple containing their number and shape, for example (1035, 'triangle'), (1081, 'triangle'), (1128, 'triangle'). etc

  2. Next, I initialise a dictionary, then I go through the list all_shapes with variable x

    • First I set mydict[x] = [], then I go then I go through the list all_shapes with variable y

      1. I check that x[0] != y[0] (That is the numbers are not the same number)

      2. I check that x[1] != y[1] (That is the numbers are not the same shape)

      3. Then I check that x[0] and y[0] are cyclic (Last 2 numbers of x[0] are equal to the first to y[0]

        • If all of these conditions are true, I add y to mydict[x]

    • Now mydict basically contains a map, where mydict[x] is a list containing where all elements are cyclic to x

  3. This next step is not very pretty, I go through all the octagonal numbers, and I start the pointing game. For each octogonal number I will check every number it points to, and every number that number points to and so on

    • I will point 6 times, and now its begin to test if we have the 6 numbers we need

      1. Make sure that the last number is cyclic with the first

      2. and make sure there is one of every shape

        • The question statement says there is only one, so once we have found our 6 long cyclic path where each number is a different shape number, then we are done!

Interactive Code

No interactive code for this problem, my code is given below