Courses/CS 460/Fall 2005/Homework/Xuong Tsan/Nov 5

From CSWiki

Jump to: navigation, search

Nov 5 homework solutions


Contents


[edit] Homework Page

Homework page is: | http://cs.calstatela.edu/~wiki/index.php/Courses/CS_460/Fall_2005/Homework/Xuong_Tsan

[edit] Let+There+Be=Light

local
   proc {Light Solution}
      L E T H R B I G
      Variables = [L E T H R B I G]
   in
      Solution = [L E T]#[T H E R E]#[B E]#[L I G H T]
      Variables ::: 0#9
      {FD.distinct Variables}
      L \=: 0
      T \=: 0
      B \=: 0
      100*L + 10*E + T
      +  10000*T + 1000*H + 100*E + 10*R + E
      +  10*B + E
      =: 10000*L + 1000*I + 100*G + 10*H + T
      {FD.distribute ff Variables}

   end
in
   {Browse {SearchAll Light}}
   {ExploreAll Light}
end


[edit] Rotate Digits

local
   fun {ToDigits List}
      %FoldL take a the list and apply the function to each element
      %the function first time and sum up all the resuslt
      {FoldL List fun {$ Ac Rs} {FD.plus {FD.times Ac 10} Rs} end 0}
   end

   proc {Mult L1 X L2}
      %Compare List 1 and List 2
      {ToDigits L1} * X =: {ToDigits L2}
   end
   
   proc {RotateDs Ans}
      A B C D E F
   in
      Ans = [A B C D E F]
      Ans ::: 1#9
           
      {Mult [A B C D E F] 1 [A B C D E F]}
      {Mult [A B C D E F] 3 [B C D E F A]}
      {Mult [A B C D E F] 2 [C D E F A B]}
      {Mult [A B C D E F] 6 [D E F A B C]}
      {Mult [A B C D E F] 4 [E F A B C D]}
      {Mult [A B C D E F] 5 [F A B C D E]}
      %Propagate all the distinctive Ans
      {FD.distinct Ans}
      {FD.distribute ff Ans}
   end
in
   {ExploreAll RotateDs}   
   {Browse {SearchAll RotateDs}}
   
end 

[edit] Divisible Digits

local
   fun {ToDigits List}
      %FoldL take a the list and apply the function to each element
      %the function first time and sum up all the resuslt
      {FoldL List fun {$ Ac Rs} {FD.plus {FD.times Ac 10} Rs} end 0}
   end

   proc {DivisibleDs Ans}
      A B C D E F G H I J
   in
      Ans = [A B C D E F G H I J]
      Ans ::: 0#9

      
      %divisible by 9,8,7,6, and 5
      {FD.modI {ToDigits [A B C D E F G H I J]} 9} =:0
      {FD.modI {ToDigits [A B C D E F G H I J]} 8} =:0
      {FD.modI {ToDigits [A B C D E F G H I J]} 7} =:0
      {FD.modI {ToDigits [A B C D E F G H I J]} 6} =:0
      {FD.modI {ToDigits [A B C D E F G H I J]} 5} =:0
      ToDigits [A B C D E F G H I J]}
      %Propagate all the distinctive Ans
      {FD.distinct Ans}
      {FD.distribute ff Ans}
   end
in
   %{ExploreAll DivisibleDs}
   {Browse {SearchOne DivisibleDs}}
 %  {Browse {SearchAll DivisibleDs}}
   
end