-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.pl
68 lines (61 loc) · 1.35 KB
/
a.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
60
61
62
63
64
65
66
67
68
% Month and Century codes %
codeM(1,6).
codeM(2,2).
codeM(3,2).
codeM(4,5).
codeM(5,0).
codeM(6,3).
codeM(7,5).
codeM(8,1).
codeM(9,4).
codeM(10,6).
codeM(11,2).
codeM(12,4).
codeY(0,0).
codeY(1,5).
codeY(2,3).
codeY(3,1).
% Helper Predicates %
sepYear(Y, Cent, Year):-
Cent is Y div 100,
Year is Y mod 100.
leapYear(Y):-
(0 is Y mod 4, Y mod 100 \= 0);
0 is Y mod 400.
leap(M, Y, 1):-
leapYear(Y), M<3.
leap(M, Y, 0):-
not((leapYear(Y), M<3)).
% Doomsday Algorithm %
doomsday(D, M, Y, W):-
sepYear(Y, Cent, Year),
ModYear is Year div 4,
ModCent is Cent mod 4,
codeM(M, Cm), codeY(ModCent, Cy),
leap(M, Y, Leap),
Day is (D + Cm + Cy + Year + ModYear - Leap),
W is Day mod 7.
% Try it!
% doomsday(25, 5, 1810, X).
% X = 5.
% doomsday(1, 1, 2000, X).
% X = 6.
% doomsday(11, 9, 2001, X).
% X = 2.
% doomsday(7, 10, 2020, X).
% X = 3.
% doomsday(23, 2, 1927, X).
% X = 3.
% doomsday(6, 9, 1969, X).
% X = 6.
% doomsday(29, 2, 2020, X).
% X = 6.
% Benchmarking in the cheapest possibe way
manyDoomsdays():-
doomsday(29,2,2020,X).
% copy and paste ^that line (with a ,) 999 more times :D
stats():-
statistics(walltime, [TimeSinceStart | [TimeSinceLastCall]]),
manyDoomsdays(),
statistics(walltime, [NewTimeSinceStart | [ExecutionTime]]),
write('Execution took '), write(ExecutionTime), write(' ms.'), nl.