For instance, it's two units from town a to town b, 17 units from town b to town j, 10 units from town j to town m, so a path might start out a, b, j, m, since at b the distance travelled will be 2 (a prime number), at j the distance travelled will be 19 (which is prime), at m it will be 29 (also prime). The problem is to figure out what path with bring you back to where you started, and also to determine where to start.
The code for primemaze is stored in the file primemaze.pl. Here it is.
node(a). node(b). node(c). node(d). node(e). node(f). node(g). node(h). node(i). node(j). node(k). node(l). node(m). node(n). node(o). node(p). node(q). node(r). node(s). node(t). node(u). node(v). node(w). link(a,b,2). link(a,c,8). link(a,d,10). link(a,e,5). link(b,a,2). link(b,d,14). link(b,j,17). link(c,a,8). link(c,d,12). link(c,g,3). link(c,h,10). link(d,a,10). link(d,b,14). link(d,c,12). link(d,h,4). link(e,a,5). link(e,f,2). link(e,n,6). link(f,e,2). link(f,g,6). link(f,o,6). link(g,c,3). link(g,f,6). link(g,o,4). link(h,c,10). link(h,d,4). link(h,t,8). link(i,j,8). link(i,k,4). link(i,l,6). link(j,b,17). link(j,i,8). link(j,s,6). link(k,i,4). link(k,p,12). link(k,q,12). link(l,i,6). link(l,m,12). link(l,q,8). link(m,j,10). link(m,l,12). link(m,r,8). link(n,e,6). link(n,o,2). link(n,v,4). link(o,f,6). link(o,g,4). link(o,n,2). link(o,v,12). link(p,k,12). link(p,t,6). link(p,u,4). link(q,k,12). link(q,l,8). link(q,r,10). link(r,m,8). link(r,q,10). link(r,s,12). link(s,j,6). link(s,r,12). link(s,w,13). link(t,h,8). link(t,p,6). link(t,u,14). link(u,p,4). link(u,t,14). link(u,w,10). link(v,n,4). link(v,o,12). link(v,w,2). link(w,s,13). link(w,u,10). link(w,v,2). prime(2). prime(N) :- N>1, N mod 2 =\=0, primetest(N,3). primetest(N,M) :- N>M, N mod M =\= 0, K is M+2, primetest(N,K). primetest(N,N). path(A,P,B,Q,[[A,P],[B,Q]]) :- R is Q-P, link(A,B,R). path(A,P,B,Q,X) :- link(A,C,L), R is P+L, R< Q, prime(R), path(C,R,B,Q,Y), X=[[A,P]|Y]. solution(X,Q) :- prime(Q), node(A), path(A,0,A,Q,X). solution(X) :- solutionsearch(X,3). solutionsearch(X,Q) :- prime(Q),solution(X,Q). solutionsearch(X,Q) :- R is Q+2, solutionsearch(X,R).And here's a run illustrating how it works. First, a couple of lines to see how prime and primetest work.
0$ gprolog GNU Prolog 1.2.9 By Daniel Diaz Copyright (C) 1999-2001 Daniel Diaz | ?- consult('primemaze.pl'). compiling /home/djoyce/public_html/cs170/primemaze.pl for byte code... /home/djoyce/public_html/cs170/primemaze.pl compiled, 41 lines read - 12423 bytes written, 23 ms yes | ?- prime(3). yes | ?- prime(4). no | ?- prime(89). yes | ?- prime(91). no | ?- primetest(9,4). yes | ?- primetest(9,3). no | ?- prime(9). no | ?- solution(X). X = [[b,0],[j,17],[s,23],[j,29],[i,37],[k,41],[q,53],[l,61],[i,67],[k,71], [p,83],[t,89],[u,103],[p,107],[t,113],[u,127],[w,137],[v,139],[o,151],[v,163], [n,167],[e,173],[n,179],[o,181],[v,193],[n,197],[o,199],[v,211],[o,223],[g,227], [f,233],[o,239],[v,251],[o,263],[f,269],[e,271],[n,277],[v,281],[w,283],[u,293], [t,307],[p,313],[u,317],[t,331],[p,337],[k,349],[i,353],[l,359],[q,367],[k,379], [i,383],[l,389],[m,401],[r,409],[q,419],[k,431],[p,443],[t,449],[h,457],[c,467], [d,479],[c,491],[a,499],[d,509],[b,523]] ?Note that it takes 50 ms just to see if there's a solution whose total length is 1009. It's not surprizing that a stack overflow occurs when searching for longer and longer solutions.$
Back to the course page.