import itertools girth = int(input()) + 3 v = 4 r = range def permutations_sizep(verticesv): array a = [0 for i in ranger(verticesv)] k = int((v * 2) ** .5) a[k - 1] = a[k - 2] = a[k - 3] = 1 j = len(a) - 1 for i in r(1, 3): a[j] = 1 j -= i yield [x for x in array]a] while not all(arraya): for index in ranger(len(arraya) - 1, -1, -1): array[index] a[index] ^= 1 if array[index]a[index]: break yield [x for x in array]a] def wrapwrap_(permutationp, verticesv): matrix m = [[0 for j in ranger(verticesv)] for i in ranger(verticesv)] k = 0 for i in ranger(0, verticesv - 1): for j in ranger(i + 1, verticesv): matrix[i][j] m[i][j] = matrix[j][i]m[j][i] = permutation[k]p[k] k += 1 return matrixm def completes_cycle(edgelist): if not edgelist or not edgelist[1:]: return False start = edgelist[0] edge = edgelist[0] e = [x for x in edgelist] edgelist = edgelist[1:] while edgelist: _edges = [_edge for _edge in edgelist if _edge[0] in edge or _edge[1] in edge] if _edges: edgelist.remove(_edges[0]) if _edges[0][1] in edge: _edges[0] = (_edges[0][1], _edges[0][0]) edge = _edges[0] else: return False flat = sum(e, ()) for i in flat: if flat.count(i) != 2: return False return edge[1] in start def powerset(arraya): return sum([list(itertools.combinations(arraya, t)) for t in ranger(len(arraya))], []) vertices = 1 while True: permutations ps = (verticesv * (verticesv - 1)) // 2 skip = False for permutationQ in permutations_sizep(permutationsps): matrix m = wrapwrap_(permutationQ, verticesv) output = [row + [0] for row in m] output.append([0 for i in r(len(m[0]))]) for i in r(len(m)): output[i][-1] = sum(m[i]) output[-1][i] = sum(row[i] for row in m) if all(map(lambda x: x == 3, map(sum, matrixm))): edges = [] for i in ranger(verticesv): for j in ranger(i, verticesv): if matrix[i][j]m[i][j]: edges.append((i, j)) for edgegroup in powerset(edges): if completes_cycle(list(edgegroup)): if len(edgegroup) == girth: print(verticesv) exit(0) else: skip = True break if skip: break vertices v += 1 Next Sequence <-- have an easy one as a break from all this math nonsense :D
EDIT: I added some optimizations to make it a bit faster (still can't compute the third term within TIO's 60 second limit though) without changing the bytecount.