I hate how damned finnicky project euler problems are. don't get the right answer with this:
:- module bouncy.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, float.
main(!IO) :-
io.print_line(find_bouncy_pct(0.99): int, !IO).
:- pred increasing(int::in) is semidet.
increasing(N) :-
(
N < 10
;
A = N `mod` 10, B = Rest `mod` 10, Rest = N / 10,
B =< A,
increasing(Rest)
).
:- pred decreasing(int::in) is semidet.
decreasing(N) :-
(
N < 10
;
A = N `mod` 10, B = Rest `mod` 10, Rest = N / 10,
A =< B,
decreasing(Rest)
).
:- pred bouncy(int::in) is semidet.
bouncy(N) :- not (increasing(N) ; decreasing(N)).
:- func find_bouncy_pct(float) = int.
find_bouncy_pct(Pct) = find_bouncy_pct(Pct, 10, 0, 9, 9).
:- import_module string, list.
:- func find_bouncy_pct(float, int, int, int, int) = int.
find_bouncy_pct(Pct, N, Bouncy0, Inc0, Dec0) = R :-
( if increasing(N) then
Bouncy = Bouncy0, Inc = Inc0 + 1, Dec = Dec0
else if decreasing(N) then
Bouncy = Bouncy0, Inc = Inc0, Dec = Dec0 + 1
else
Bouncy = Bouncy0 + 1, Inc = Inc0, Dec = Dec0
),
( if float(Bouncy) / float(Inc + Dec + Bouncy) >= Pct then
R = N
else
R = find_bouncy_pct(Pct, N + 1, Bouncy, Inc, Dec)
).