Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # Usage: First commandline argument is max.
- #
- # This script finds all possible paths that make a circle of squares
- # and prints them.
- #
- # The output still contains duplicates and mirrors.
- #
- import sys
- high = int( sys.argv[1] )
- ring = tuple([ x+1 for x in range(high) ])
- squares = set([ x**2 for x in range(1, high) if x**2 < 2*high ])
- final = []
- # Create dict of possible squares.
- possq = dict()
- for base in ring:
- possq[base] = []
- for num in ring:
- if num + base in squares and num != base:
- possq[base].append(num)
- for key in possq.keys():
- possq[key] = tuple(possq[key])
- firstkey = possq[1]
- # Print dictionary with possible squares.
- for i, j in possq.items():
- # print(i, j)
- pass
- # Find circle
- path = [1]
- a = 0
- x = 1
- error = []
- term = len(possq[1])
- while not (len(path) == 1 and a == term):
- # print(path)
- while a < len(possq[path[-1]]):
- if possq[path[-1]][a] not in path:
- path.append(possq[path[-1]][a])
- a = 0
- if len(path) == high:
- print(*path, sep=' ')
- break
- a += 1
- else:
- b = path.pop()
- a = possq[path[-1]].index(b) + 1
- # Print result
- Final = []
- [ Final.append(i) for i in final if i not in Final ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement