I've been trying to work through the Palindrome example that is given in
chapter 9 using relational programming and chapter 12 using constraint
programming. Took me a bit to figure out the obvious that the one in
chapter 9 is 4 digit palindromes, and the one in chapter 12 is 6
digits. So I thought I would verify that the two solutions are
equivalent by writing a 6 digit solution for chapter 9 and a 4 digit for
chapter 12.
Unfortunately, I can't seem to get a solution across the two paradigms
that is consistent. The first two functions should be straight copies
from the book. The next two are my guesses about changing the digit
length for the two functions. What I was wondering is why the solutions
from the two paradigms don't match? Am I missing something in my code?
fun {Digit} choice 0 [] 1 [] 2 [] 3 [] 4 [] 5 [] 6 [] 7 [] 8 [] 9
end end
%% Chapter 9 - 4 digit as transcribed from CTM - (118 solutions)
proc {Palindrome_9_4 ?X}
X=(10*{Digit}+{Digit})*(10*{Digit}+{Digit})
(X>0)=true
(X>=1000)=true
(X div 1000) mod 10 = (X div 1) mod 10
(X div 100) mod 10 = (X div 10) mod 10
end
{Browse {Length {SolveAll Palindrome_9_4}}}
%% Chapter 12 - 6 digit as transcribed from CTM - (379 solutions)
proc {Palindrome_12_6 ?Sol}
sol(A) = Sol
B C X Y Z
in
A::0#999999 B::0#999 C::0#999
A=:B*C
X::0#9 Y::0#9 Z::0#9
A=:X*100000+Y*10000+Z*1000+Z*100+Y*10+X
{FD.distribute ff [X Y Z]}
end
{Browse {Length {SolveAll Palindrome_12_6}}}
%% Chapter 9 - 6 digit as I extrapolated - (977 solutions)
proc {Palindrome_9_6 ?X}
X=(100*{Digit}+10*{Digit}+{Digit})*(100*{Digit}+10*{Digit}+{Digit})
(X>0)=true
(X>=100000)=true
(X div 100000) mod 10 = (X div 1) mod 10
(X div 10000) mod 10 = (X div 10) mod 10
(X div 1000) mod 10 = (X div 100) mod 10
end
{Browse {Length {SolveAll Palindrome_9_6}}}
%% Chapter 12 - 4 digit as I extrapolated - (46 solutions)
proc {Palindrome_12_4 ?Sol}
sol(A) = Sol
B C X Y
in
A::0#9999 B::0#99 C::0#99
A=:B*C
X::0#9 Y::0#9
A=:X*1000+Y*100+Y*10+X
{FD.distribute ff [X Y]}
end
{Browse {Length {SolveAll Palindrome_12_4}}}
Thanks,
Chris Rathman
_________________________________________________________________________________
mozart-users mailing list
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users