/********************************************************* https://projecteuler.net/problem=172 How many 18-digit numbers n (without leading zeros) are there such that no digit occurs more than three times in n? *********************************************************/ main => Ts = [0 : _ in 0..9], count(18,Ts,Count), printf("%w possible numbers%n", Count). %% count(N,[T0,T1,T2,T3,T4,T5,T6,T7,T8,T9],Count) %% N digits to fill. %% Ti is the number of i's that have been used (Ti = 0,1,2) %% Symmetries are exploited. For any place N (N != 18), digits 1-9 are %% indistinguishable. table count(0,_,Count) => Count = 1. count(N,[T0,T1,T2,T3,T4,T5,T6,T7,T8,T9],Count) => (N!==18,T0<3 -> count(N-1,[T0+1,T1,T2,T3,T4,T5,T6,T7,T8,T9],C0); C0=0), (T1<3 -> count(N-1,[T0|insert_ordered([T2,T3,T4,T5,T6,T7,T8,T9],T1+1)],C1); C1=0), (T2<3 -> count(N-1,[T0|insert_ordered([T1,T3,T4,T5,T6,T7,T8,T9],T2+1)],C2); C2=0), (T3<3 -> count(N-1,[T0|insert_ordered([T1,T2,T4,T5,T6,T7,T8,T9],T3+1)],C3); C3=0), (T4<3 -> count(N-1,[T0|insert_ordered([T1,T2,T3,T5,T6,T7,T8,T9],T4+1)],C4); C4=0), (T5<3 -> count(N-1,[T0|insert_ordered([T1,T2,T3,T4,T6,T7,T8,T9],T5+1)],C5); C5=0), (T6<3 -> count(N-1,[T0|insert_ordered([T1,T2,T3,T4,T5,T7,T8,T9],T6+1)],C6); C6=0), (T7<3 -> count(N-1,[T0|insert_ordered([T1,T2,T3,T4,T5,T6,T8,T9],T7+1)],C7); C7=0), (T8<3 -> count(N-1,[T0|insert_ordered([T1,T2,T3,T4,T5,T6,T7,T9],T8+1)],C8); C8=0), (T9<3 -> count(N-1,[T0,T1,T2,T3,T4,T5,T6,T7,T8,T9+1],C9); C9=0), Count = C0+C1+C2+C3+C4+C5+C6+C7+C8+C9.