Example #1
0
def main():
    """Main program."""

    # This is my first, brute force solution.
    # The spiral of the required dimensions is
    # represented by a matrix and the diagonal
    # sum calculated by visiting each cell in
    # the two diagonals.
    answer = 0
    start = time.time()
    spiral = spirals.build_spiral(1001, 1001)
    answer = spirals.diagonal_sum(spiral)
    end = time.time()
    print("The answer is %d" % answer)
    print("%f seconds elapsed." % (end - start))

    # After verifying the above solution, I
    # ran matrices 3x3, 5x5, 7x7, and 9x9.
    # I noticed the patter in the corners
    # and so I came up with a second solution.
    # I used the data from OEIS sequence A114254
    # to verify that this method of calculating
    # the diagonal sums is correct.
    start = time.time()
    answer = spirals.calculate_diagonal_sum(1001)
    end = time.time()
    print("The answer is %d" % answer)
    print("%f seconds elapsed." % (end - start))

    import pyperclip
    pyperclip.copy(str(answer))
    print("The answer has been placed in the clipboard.")
Example #2
0
 def test_calculate_diagonal_sum(self):
     """Test calculate_diagonal_sum()"""
     a114254 = [1, 25, 101, 261, 537, 961, 1565, 2381, 3441, 4777, \
                6421, 8405, 10761, 13521, 16717, 20381, 24545, \
                29241, 34501, 40357, 46841, 53985, 61821, 70381, \
                79697, 89801, 100725, 112501, 125161, 138737, \
                153261, 168765, 185281, 202841, 221477, 241221]
     for index, value in enumerate(a114254):
         dim = index * 2 + 1
         self.assertEquals(value, spirals.calculate_diagonal_sum(dim))