Skip to main content
added 341 characters in body
Source Link
hyperneutrino
  • 42.8k
  • 5
  • 72
  • 227
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 

Try it online!Try it online!

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.

import itertools girth = input() + 3 def permutations_size(vertices): 	array = [0 for i in range(vertices)] 	yield [x for x in array] 	while not all(array): 		for index in range(len(array) - 1, -1, -1): 			array[index] ^= 1 			if array[index]: break 		yield [x for x in array] def wrap(permutation, vertices): 	matrix = [[0 for j in range(vertices)] for i in range(vertices)] 	k = 0 	for i in range(0, vertices - 1): 		for j in range(i + 1, vertices): 			matrix[i][j] = matrix[j][i] = permutation[k] 			k += 1 	return matrix 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(array): 	return sum([list(itertools.combinations(array, t)) for t in range(len(array))], [])   vertices = 1 while True: 	permutations = (vertices * (vertices - 1)) // 2 	skip = False 	for permutation in permutations_size(permutations): 		matrix = wrap(permutation, vertices) 		if all(map(lambda x: x == 3, map(sum, matrix))): 			edges = [] 			for i in range(vertices): 				for j in range(i, vertices): 					if matrix[i][j]: edges.append((i, j)) 			for edgegroup in powerset(edges): 				if completes_cycle(list(edgegroup)): 					if len(edgegroup) == girth: 						print(vertices) 						exit(0) 					else: 						skip = True 						break 		if skip: break 	vertices += 1 

Try it online!

Next Sequence <-- have an easy one as a break from all this math nonsense :D

import itertools girth = int(input()) + 3 v = 4  r = range  def p(v): 	a = [0 for i in r(v)] 	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 a] 	while not all(a): 		for index in r(len(a) - 1, -1, -1): 			a[index] ^= 1 			if a[index]: break 		yield [x for x in a] def wrap_(p, v): 	m = [[0 for j in r(v)] for i in r(v)] 	k = 0 	for i in r(0, v - 1): 		for j in r(i + 1, v): 			m[i][j] = m[j][i] = p[k] 			k += 1 	return m 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(a): 	return sum([list(itertools.combinations(a, t)) for t in r(len(a))], []) while True: 	ps = (v * (v - 1)) // 2 	skip = False 	for Q in p(ps): 		m = wrap_(Q, v) 		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, m))): 			edges = [] 			for i in r(v): 				for j in r(i, v): 					if m[i][j]: edges.append((i, j)) 			for edgegroup in powerset(edges): 				if completes_cycle(list(edgegroup)): 					if len(edgegroup) == girth: 						print(v) 						exit(0) 					else: 						skip = True 						break 		if skip: break 	v += 1 

Try it online!

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.

added 435 characters in body
Source Link
hyperneutrino
  • 42.8k
  • 5
  • 72
  • 227

This starts at 1 vertex and works up, creating the adjacency matrix representation of every possible undirected graph with that many vertices. If it is trivalent, then it will look through the powerset of the edges, which will be sorted by length. If the first cycle it finds is too short, then it will move on. If the first cycle it finds matches the input (offset by 3) then it will output the correct vertex count and terminate.

Next Sequence <-- have an easy one as a break from all this math nonsense :D

Next Sequence <-- have an easy one as a break from all this math nonsense :D

This starts at 1 vertex and works up, creating the adjacency matrix representation of every possible undirected graph with that many vertices. If it is trivalent, then it will look through the powerset of the edges, which will be sorted by length. If the first cycle it finds is too short, then it will move on. If the first cycle it finds matches the input (offset by 3) then it will output the correct vertex count and terminate.

Next Sequence <-- have an easy one as a break from all this math nonsense :D

Source Link
hyperneutrino
  • 42.8k
  • 5
  • 72
  • 227

91. Python 2 (PyPy), 1733 bytes, A000066

import itertools girth = input() + 3 def permutations_size(vertices): 	array = [0 for i in range(vertices)] 	yield [x for x in array] 	while not all(array): 		for index in range(len(array) - 1, -1, -1): 			array[index] ^= 1 			if array[index]: break 		yield [x for x in array] def wrap(permutation, vertices): 	matrix = [[0 for j in range(vertices)] for i in range(vertices)] 	k = 0 	for i in range(0, vertices - 1): 		for j in range(i + 1, vertices): 			matrix[i][j] = matrix[j][i] = permutation[k] 			k += 1 	return matrix 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(array): 	return sum([list(itertools.combinations(array, t)) for t in range(len(array))], []) vertices = 1 while True: 	permutations = (vertices * (vertices - 1)) // 2 	skip = False 	for permutation in permutations_size(permutations): 		matrix = wrap(permutation, vertices) 		if all(map(lambda x: x == 3, map(sum, matrix))): 			edges = [] 			for i in range(vertices): 				for j in range(i, vertices): 					if matrix[i][j]: edges.append((i, j)) 			for edgegroup in powerset(edges): 				if completes_cycle(list(edgegroup)): 					if len(edgegroup) == girth: 						print(vertices) 						exit(0) 					else: 						skip = True 						break 		if skip: break 	vertices += 1 

Try it online!

I hope using Python 2 PyPy counts as another major version. If someone could get me a Python 0 interpreter, I could use that too, but I hope this is valid.

Next Sequence <-- have an easy one as a break from all this math nonsense :D