#!/usr/bin/python # Range to test; don't change the low number to anything below 1 num_balls_low = 2 num_balls_high = 1000 def weigh(balls, count=0): """Weighs a set of balls according to the 8-ball algorithm""" if len(balls) == 1: return count count += 1 n = len(balls) / 3 if len(balls) % 3 != 0: n += 1 scale1 = balls[0:n] scale2 = balls[n:2*n] scale1weight = 0 scale2weight = 0 for i in range(len(scale1)): scale1weight += scale1[i] scale2weight += scale2[i] if scale1weight == scale2weight: return weigh(balls[2*n:], count) elif scale1weight > scale2weight: return weigh(scale1, count) else: return weigh(scale2, count) for i in range(num_balls_low, num_balls_high): total = 0 lowest = 1000000 highest = -1 for j in range(i): balls = [] for k in range(i): if k != j: balls.append(0) else: balls.append(1) n = weigh(balls) total += n if n < lowest: lowest = n if n > highest: highest = n print i, (float(total) / i), lowest, highest