Beispiel #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.")
Beispiel #2
0
 def test_diagonal_sum(self):
     """Test diagonal_sum()"""
     spiral = spirals.build_spiral(3, 3)
     self.assertEquals(25, spirals.diagonal_sum(spiral))
     spiral = spirals.build_spiral(5, 5)
     self.assertEquals(101, spirals.diagonal_sum(spiral))