Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468.
Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420.
We shall call a positive integer that is neither increasing nor decreasing a “bouncy” number; for example, 155349.
Clearly there cannot be any bouncy numbers below one-hundred, but just over half of the numbers below one-thousand (525) are bouncy. In fact, the least number for which the proportion of bouncy numbers first reaches 50% is 538.
Surprisingly, bouncy numbers become more and more common and by the time we reach 21780 the proportion of bouncy numbers is equal to 90%.
Find the least number for which the proportion of bouncy numbers is exactly 99%.
#!/usr/bin/env python
def calc(x):
a = d = False
x = str(x)
for y in range(len(x)-1):
if x[y+1] > x[y]:
a = True
elif x[y+1] < x[y]:
d = True
if a and d:
return True
return False
x = 21780
p = 0.90
b = x * p
while p != 0.99:
x+= 1
if calc(x):
b+= 1
p = float(b)/x
print x
/home/umgeher/war/hg/euler/python> time python 112.py 1587000 Time spent in user mode (CPU seconds) : 5.449s Time spent in kernel mode (CPU seconds) : 0.007s Total time : 0:05.46s CPU utilisation (percentage) : 99.6% Times the process was swapped : 0 Times of major page faults : 0 Times of minor page faults : 649 /home/umgeher/war/hg/euler/python>

The Day Shift by pg.lost





0 Comments.