-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheuler_172.pl
59 lines (49 loc) · 1.47 KB
/
euler_172.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Project Euler, Problem 172:
How many 18-digit numbers n (without leading zeros) are there such
that no digit occurs more than three times in n?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- use_module(library(clpb)).
:- use_module(library(clpz)).
:- use_module(library(lists)).
:- use_module(library(format)).
:- use_module(library(time)).
len_num(L, N) :-
length(Digits, L),
maplist(length_list(10), Digits),
Digits = [First|_],
First = [0|_], % no leading zero
transpose(Digits, DigitsT),
% the order of the following 2 constraints has a huge impact
% on performance!
maplist(card([0,1,2,3]), DigitsT),
maplist(card([1]), Digits),
append(Digits, Vs),
sat_count(+[1|Vs], N).
card(C, Ls) :- sat(card(C, Ls)).
length_list(L, Ls) :- length(Ls, L).
run(L) :-
len_num(L, N),
portray_clause(len_num(L,N)).
run :-
length(_, N),
time(run(N)),
false.
%?- run.
%@ % CPU time: 0.000s
%@ len_num(1,9).
%@ % CPU time: 0.005s
%@ len_num(2,90).
%@ % CPU time: 0.015s
%@ len_num(3,900).
%@ % CPU time: 0.046s
%@ len_num(4,8991).
%@ % CPU time: 0.155s
%@ len_num(5,89586).
%@ % CPU time: 0.488s
%@ len_num(6,888570).
%@ % CPU time: 1.401s
%@ len_num(7,8754480).
%@ % CPU time: 3.751s
%@ len_num(8,85480920).
%@ % CPU time: 9.623s