% https://open.kattis.com/problems/heirsdilemma % 1s % 1.7s % Picat using brute force.... % This is much slower than using proper cp (in heirs_dilemma.pi)): 1.97s vs 0.05s % Slightly faster with membchk/2: 1.8s import util,cp. main :- [Low,Up] = read_file_lines().first.split(" ").map(to_int), s(Low,Up,0,C), println(C). main. s(I,Up,C,C) :- I > Up. s(I,Up,C0,C) :- S = I.to_string.map(to_int), ( (not membchk(0,S),all_different(S),d(S,I)) -> C1 is C0 + 1 ; C1 is C0 ), I1 is I+1, s(I1,Up,C1,C). d([],_). d([D|Ds],N) :- D > 0, N mod D == 0, d(Ds,N).