Courses/CS 460/Fall 2005/Homework/Oscar Chen/Nov 12

From CSWiki

Jump to: navigation, search

Contents

[edit] November 12th Homework

[edit] Digits to Integer

  • Well, FoldR is like FoldL but reversed right? So what if we reverse the function itself...
local
   fun {ToIntR L1}
      {FoldR {List.reverse L1} fun {$ I J} I + 10*J end 0}
   end
in
   {Browse {ToIntR [9 1 8 0 3]}}
   {Browse {ToIntR [5 4 3 2 1 0]}}
end

[edit] Free Puzzles #006

  A B 
+ C D 
+ E F 
-----
= G H

  G H 
+   I 
-----
= 100
local
   proc {SumTo100 Solution}
      A B C D E F G H I
      Vars = [A B C D E F G H I]
   in
      Solution = [A B]#[C D]#[E F]#[G H]#[I]
      Vars ::: 1#9  
      {FD.distinct Vars}
         10*A + B           
      +  10*C + D
      +  10*E + F
      =: 10*G + H

         10*G + H
      +         I
      =: 100
      {FD.distribute ff Vars}
   end
in
   {Browse {SearchOne SumTo100}}
end

[edit] Free Puzzles #081

  • Math Puzzle 081
  • Find a 3x3 Matrix where the Product of each row, col, and diag equals to 1000
local
   proc {ProdTo1000 Solution}
      A B C D E F G H I
      Vars = [A B C D E F G H I]
   in
      Solution = [A B C]#[D E F]#[G H I]
      Vars ::: 1#1000
      {FD.distinct Vars}

      A * B * C =: 1000
      D * E * F =: 1000
      G * H * I =: 1000
      A * D * G =: 1000
      B * E * H =: 1000
      C * F * I =: 1000
      A * E * I =: 1000
      C * E * G =: 1000

      {FD.distribute ff Vars}
   end
in
   {Browse {SearchOne ProdTo1000}}
end