**EDIT**: i made several changes (i would prefer to just spam the link to source code, but it doesnâ€™t let me post the link :\ )

```
/* A lucky number is a 10-based number, which has at least a "6" or an "8" in its digits.
* However, if it has "6" and "8" at the same time, then the number is NOT lucky.
* For example, "16", "38", "666" are lucky numbers, while "234" , "687" are not.
* Now we want to know how many lucky numbers (without leading zeroes)
* are there between L and R, inclusive?
*
* L is LowerBound
* R is UpperBound
* P is the total occurence of lucky numbers
*
* between_to_list -> generates all the numbers between L and R included
*
* digits -> convert the number in digits
*
* check_number -> check if Cs (number converted in digits) contains 6 and
* not contain 8, or contains 8 and not contain 6, if these constraints
* are satisfied increment P by 1
*
* query tested -> generate_interval(6,16)
*
* I generate all numbers and then i move backward from R to L and i check every number,
* 16 is the first element checked, indeed P is incremented to 1, then the program check
* 15 and it fails, after the fail the program quit*/
generate_interval(L,R,P):-
between_to_list(L,R,P),
write(P).
between_to_list(X,X,0) :- !.
between_to_list(X,Y,P) :-
X =< Y,
Z is X+1,
between_to_list(Z,Y,P1),
digits(Z,C),
check_number(C,P1).
digits(X,Cs):-number_chars(X, Cs).
check_number(Cs,P1):-
memberchk('6', Cs),
not(memberchk('8', Cs)),
P is P1+1;
memberchk('8', Cs),
not(memberchk('6', Cs)),
P is P1+1.
```

now iâ€™m able to work with N digits, and the counter should be implemented in the right way as well, the problem is that i donâ€™t know how to keep program iterate after the 1st fail on the check. I assume that if this problem is solved then all should works properly.

If someone could give me a feedback would be really appreciate.