Cardboard Mountains by MathPickle


Python code

def is_sol(x):
	y = x.copy()
	for i in range(0, len(y)):
		if y[i] != round(y[i]):
			return False
	for i in range(0, len(y)):
		for j in range(0, len(y) - i - 1):
			y[j] = (y[j] + y[j + 1]) / 2
			if y[j] != round(y[j]):
				return False
	return True
	
def peak(x):
	y = x.copy()
	i = 0
	for i in range(0, len(y)):
		for j in range(0, len(y) - i - 1):
			y[j] = (y[j] + y[j + 1]) / 2
	return int(y[0])

def print_mtn(x):
	if len(x) == 1:
		print(str(int(x[0])))
	else:
		y = []
		for i in range(0, len(x) - 1):
			y.append((x[i] + x[i + 1]) / 2)
		print_mtn(y)
		print(', '.join([str(int(i)) for i in x]))
		
n = 5
def a(n):
	if n == 0:
		return 0
	else:
		return a(n - 1) + 2 + 4*(n - 1)

# Create the bottom row
x = []
for i in range(0, n):
	x.append(a(i))
	
# Print the bottom row
print(', '.join([str(int(i)) for i in x]) + '\n')

# Print the mountain if it's a solution
if not is_sol(x):
	print('Not a solution.')
else:
	print_mtn(x)

Output

0, 2, 8, 18, 32

10
6, 14
3, 9, 19
1, 5, 13, 25
0, 2, 8, 18, 32