hbn(N, B, L) :- coefs(N, B, Cs), length(Cs, Exp0), Exp is Exp0 - 1, notation(B, Exp, Cs, L). notation(_B, _Exp, [], []). notation(B, Exp, [0|Cs], Ls) :- Exp1 is Exp - 1, notation(B, Exp1, Cs, Ls). notation(B, Exp, [C|Cs], [L|Ls]) :- Exp1 is Exp - 1, C > 0, notation(B, Exp1, Cs, Ls), notation_element(C, B, Exp, L). notation_element(C, B, Exp, X) :- Exp < B, X = C * B ^ Exp. notation_element(C, B, Exp, X) :- Exp >= B, hbn(Exp, B, Exp0), X = C * B ^ Exp0. coefs(N, B, Cs) :- max_exponent(N, B, Exp), base_exp_num_coefs(B, Exp, N, Cs). max_exponent(N, B, Max) :- max_exponent(N, B, 0, Max). max_exponent(N, B, E0, E) :- succ(E0, E1), ( B ^ E1 > N -> E = E1 ; max_exponent(N, B, E1, E) ). base_exp_num_coefs(_Base, N, _I, []) :- N < 0. base_exp_num_coefs(Base, N, I, [C|Coefs]) :- N >= 0, NN is Base ^ N, C is I // NN, N1 is N - 1, I1 is I - (C*NN), base_exp_num_coefs(Base, N1, I1, Coefs).