% Allow singleton vars: :-style_check(-singleton). % Allow numbers to be changed -- shuffled :-dynamic(number/2). % ------generate sample of N lottery picks sample(N) :- for(I, N), pick, fail. sample(N). % -------- The picked numbers are the first 6 numbers. show_pick :- for(I, 6), number(I, P), (P < 10 , write(' ') ; P >= 10 , write(' ')), write(P), fail. show_pick :- nl,nl. % --------number(I,J) stores a permutation of 1..50, See Botting 74. number(33, 43). number(26, 17). number(40, 19). number(48, 49). number(24, 22). number(9, 24). number(25, 20). number(17, 5). number(41, 9). number(16, 11). number(7, 38). number(22, 45). number(12, 27). number(20, 29). number(29, 28). number(38, 6). number(11, 15). number(47, 13). number(18, 48). number(37, 2). number(46, 14). number(14, 26). number(28, 16). number(49, 21). number(34, 3). number(19, 1). number(13, 10). number(50, 23). number(21, 41). number(32, 7). number(30, 46). number(44, 31). number(39, 33). number(35, 36). number(8, 12). number(36, 32). number(42, 8). number(15, 30). number(1, 4). number(27, 25). number(2, 47). number(45, 42). number(3, 34). number(43, 44). number(4, 18). number(10, 37). number(5, 39). number(31, 35). number(6, 40). number(23, 50). % -------pick Californian ticket ------- pick :- shuffle, !, sort, !, show_pick, !. % -------setup an initial datbase, in case it got corrupted. setup :- abolish(number,2), for(I, 50), assert(number(I, I)), fail. % ---- exchange to items in number swap(I, J) :- I=J,!. swap(I,J):- retract(number(I, A)), retract(number(J, B)), assert(number(I, B)), assert(number(J, A)), !. % ---- prolog's for loop for(I, I, N) :- I =< N. for(I, M, N) :- M < N, M1 is M + 1, for(I, M1, N). for(I, N) :- for(I, 1, N). % How to shuffle the numbers shuffle :- for(I, 6), shuffle(I), fail. shuffle :- true. shuffle(I) :- J is random( 50)+1, swap(I, J), !. % How to sort the first 6 numbers sort:- for(I,5),K is 6-I, pass(I,K), fail. sort:-true. % I'th pass throught the 6 numbers in the sort. % I is lint left in for debugging purposes pass(I,K):- for(J,1,K), J1 is J+1, number(J,N),number(J1,N1), order(J,N,J1,N1), fail. pass(I,K):-true. % Put a pair of numbers into order order(J,N,J1,N1):- (N=