Project Euler

Problem #112

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%.

Erlang: Running time = 8.078s
+%digit_split

p112()->
	p112(100,0).
p112(N,Total) when Total/N == 0.99 ->io:format("~w~n",[N]);
p112(N,Total)->
	case p112(bouncy,digit_split(N+1),undefined) of
		true->
			p112(N+1,Total+1);
		false->p112(N+1,Total)
	end.

% This part checks if a number is bouncy;
% It uses the last argument to keep track of
% if it is increasing or decreasing (or neither)

p112(bouncy,[_],_)->false;
p112(bouncy,[A,B|R],D)->
	if
		A>B->
			if
				D/=up->p112(bouncy,[B|R],down);
				true->true
			end;
		A<B->
			if
				D/=down->p112(bouncy,[B|R],up);
				true->true
			end;
		true->
			p112(bouncy,[B|R],D)
	end.