-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprimes.pro
51 lines (40 loc) · 1.01 KB
/
primes.pro
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
% factor a number
factor(N,Ns) :- N > 0, prime_factors(N,Ns,2).
prime_factors(1,[],_) :- !.
prime_factors(N,[F|L],F) :-
R is N // F, N =:= R * F,
!,
prime_factors(R,L,F).
prime_factors(N,L,F) :-
next_factor(N,F,NF),
prime_factors(N,L,NF).
next_factor(_,2,3) :- !.
next_factor(N,F,NF) :- F * F < N, !, NF is F + 2.
next_factor(N,_,N).
add_one(X,X-1).
add_len(X-[1],R):-!,R=X.
add_len(X-Xs,X^L):-length(Xs,L).
% canonical form as in the Fund. theor. of Arithmetic
to_canonical_factors(N,Es):-
factor(N,Ps),
to_canonical(Ps,Es).
to_canonical(Xs,Cs):-
maplist(add_one,Xs,Ps),
group_pairs_by_key(Ps,PKs),
maplist(add_len,PKs,Cs).
% turns natural number into
% list of nested horn clause formulas
to_ipc(N,Rs):-
to_canonical_factors(N,Es),
to_ipcs(Es,Rs).
to_ipcs([],[]).
to_ipcs([(B^A)|Xs],[(B:-As)|Ys]):-!,
to_ipc(A,As),
to_ipcs(Xs,Ys).
to_ipcs([A|Xs],[A|Ys]):-
to_ipcs(Xs,Ys).
nat2horn(N,(H:-Bs)):-
to_ipc(N,[H|Bs]).
nat2hard_horn(N,(H:-Bs)):-
to_ipc(N,Xs),
append(Bs,[H],Xs).