> The picture is even worse in fact: the version in chapter 12 cannot be proved correct! > The correctness depends on the fact that the propagator A=:B*C guarantees the
> existence of a solution when A is determined.

I'm not sure I understand what you are stating here? From the code standpoint, I see you've added B and C to the distribute function.

> Here is a constraint version that solves the same problem as the relational version in the book:

In my confusion, I was solving the wrong problem from my end as well - just generating palindromes is what my revised solution was doing. Whereas chapter 9 states the problem as generating "all four-digit palindromes that are products of two-digit numbers" - meaning the 4 digit number must be >= 1000 and the two-digit numbers >= 10. The revised solution you provide matches the result of the code in chapter 9, though I'm not sure if the authors intended to have repeats? Just for the sake of completeness, I was trying to prove to myself where the discrepancies arose. In the original code of chapter 12 (translated to 4 digits), I was getting 46 solutions. This comes from having 36 unique solutions >= 1000, and an additional 10 that are < 1000.

  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

  % 46 unique solutions (includes numbers less than 1000)
  [0000 0110 0220 0330 0440 0550 0660 0770 0880 0990
   1001 1221 1551 1771 1881 2002 2112 2332 2442 2552
   2772 2992 3003 3663 3773 4004 4224 4554 4664 4774
   4884 5005 5115 5225 5335 5445 5775 6006 6336 6776
   7007 7227 8008 8118 8448 9009]

Filtering out the results < 1000, we would get the 36 unique solutions >= 1000. (Note: limiting the domain to A::1000#9999 or using the constraint A>:1000 could be used in lieu of limiting the range of X::1#9, but then that'd be less efficient. But since the example is given twice in chapter 12, the A:>1000 is a better approximation to the original code in chapter 9 for the first version.)

  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::1#9 Y::0#9
     A=:X*1000+Y*100+Y*10+X
     {FD.distribute ff [X Y]}
  end

  % 36 unique solutions (excludes numbers less than 1000)
  [1001 1221 1551 1771 1881 2002 2112 2332 2442 2552
   2772 2992 3003 3663 3773 4004 4224 4554 4664 4774
   4884 5005 5115 5225 5335 5445 5775 6006 6336 6776
   7007 7227 8008 8118 8448 9009]


In the revised code you supplied, I get 118 solutions which matches the results of chapter 9 (36 unique solutions & 82 duplicates). Not sure I understand the distribution of repitition in the numbers? (It must favor 2772 since it gets repeated 10 times).


  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::1#9 Y::0#9
     A=:X*1000+Y*100+Y*10+X
     {FD.distribute ff [X Y B C]}
  end

  % 118 solutions - includes 82 duplicates (note: I've sorted the list)
  [1001 1001 1001 1001 1221 1221 1551 1551 1771 1771
   1881 1881 1881 1881 2002 2002 2002 2002 2112 2112
   2112 2112 2112 2112 2112 2112 2112 2112 2332 2332
   2442 2442 2442 2442 2552 2552 2552 2552 2772 2772
   2772 2772 2772 2772 2772 2772 2772 2772 2992 2992
   2992 2992 3003 3003 3003 3003 3663 3663 3773 3773
   4004 4004 4004 4004 4224 4224 4224 4224 4224 4224
   4554 4554 4554 4554 4664 4664 4774 4774 4884 4884
   5005 5005 5005 5005 5115 5115 5225 5225 5335 5335
   5445 5445 5775 5775 6006 6006 6006 6006 6336 6336
   6336 6336 6336 6336 6776 6776 7007 7007 7227 7227
   8008 8008 8118 8118 8448 8448 9009 9009]
Thanks,
Chris Rathman




_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to