Problem 86

completed January 20, 2012

Comments

So sweet that I got this one right! Finally, got one. I used two tools to get this question correct. First, binary search. I didn’t want to have to dig through every number one by one until I found the right one. Using this technique, I was able to find the answer after testing only 15 values for M.

Second, in order to speed up the checking of each value, I relied on the total count from the next smallest check. I started with this value, then ran through all the cuboids that weren’t yet checked, adding to the final value. This technique worked very nicely.

Code

def my_answer(low, mid, high):
	global lowestM
	count = lowestM[1]
	M = mid; print M
	for L in range(1, M+1):
		for W in range(L, M+1):
			for H in range(max([W, lowestM[0]+1]), M+1):
				one = (L**2 + (W+H)**2)**.5
				two = (H**2 + (L+W)**2)**.5
				three = (W**2 + (H+L)**2)**.5
				smallest = min([one, two, three])
				if smallest%1 == 0:
					count += 1
	
	print M, count
	if high < low:
		return 'not found'
	if count > 10**6:
		return my_answer(low, int((3*low+mid)/4), mid)
	elif count < 10**6:
		lowestM = [M, count]
		return my_answer(mid, int((3*mid+high)/4), high)
	else:
		return mid, 'found'