Problem 112

completed January 6, 2012

Comments

This one was easy! It was refreshing to have a problem that I got on the first try. The last handful that I’ve done were all very very challenging.

For this one, I defined a function that would tell me if a number is bouncy or not. It did so by turning the number into a string, then going through the number digit by digit. Because it didn’t have to evaluate the entire number, and instead returned True at the first instance of both increase and decrease, the function ran very fast.

Code

def bouncy(x):
	x = str(x)
	up = False
	down = False
	for i in range(1, len(x)):
		if x[i] > x[i-1]:
			up = True
		elif x[i] < x[i-1]:
			down = True
		if up == True and down == True:
			return True
	return False

bouncies = float(0)
n = 1

while (bouncies * 100 / float(n)) != 99:
	n += 1
	if bouncy(n) == True:
		bouncies += 1


print n, (bouncies * 100 / float(n))