Created
August 9, 2017 10:33
-
-
Save jvanmelckebeke/702a1a6f1f4abda90803597e9d7076dd to your computer and use it in GitHub Desktop.
prime generator for prolog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fill([], _, 0). | |
fill([X|Xs], X, N) :- succ(N0, N), fill(Xs, X, N0). | |
prime(N, 1) :- write('is prime'), nl. | |
prime(N, N) :- | |
D1 is N - 1, | |
write(D1), nl, | |
prime(N, D1). | |
prime(N, D) :- | |
write(D), nl, | |
0 < mod(N,D), | |
D1 is D - 1, | |
prime(N, D1). | |
loop(0, X, List). | |
loop(N, X, List) :- | |
append([X], List, L1), | |
N1 is N - 1, | |
loop(N1, L1, X). | |
change_element(C, I, Old, New, Val) :- | |
print(New), nl, | |
C = I | |
-> | |
append(New, [Val], N), | |
C1 = C + 1, | |
change_element(C1, I, Old, N, Val) | |
; | |
length(Old, L), | |
C < L -> | |
nth0(C, Old, E), | |
append(New, [E], N), | |
C1 is C + 1, | |
change_element(C1, I, Old, N, Val) | |
; | |
true. | |
steploop(Start, List, Step) :- | |
print(List), nl, | |
length(List, L), | |
Start < L -> | |
NList = [], | |
change_element(0, Start, List, NList, 1), | |
S1 is Start + Step, | |
steploop(S1, NList, Step) | |
; | |
true. | |
ifloop(I, List, Condition) :- | |
length(List, L), | |
I < L, | |
nth0(I, List, E), | |
E is Condition -> print(I), nl ; true, | |
I1 is I + 1, | |
ifloop(I1, List, Condition). | |
primeloop(I, List) :- | |
length(List, L), | |
I < L, | |
nth0(I, List, X), | |
% print(X), nl, | |
X = 1 -> steploop(I, List, I) ; | |
I1 is I + 1, | |
primeloop(I1, List). | |
subloop(0, X, List). | |
subloop(I, X, List) :- | |
print(List), nl, | |
append(List, [X], L), | |
I1 is I - 1, | |
subloop(I1, X, L). | |
genprime(N) :- | |
I is N, | |
% print('start'), nl, | |
List = [0,1], | |
% print(List), nl, | |
L = [], | |
subloop(I, 0, L), | |
append(List, L, List), | |
print('list '), | |
print(L), nl, | |
primeloop(0, List), | |
ifloop(0, List, 1), | |
print(List) , nl. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment