Courses/CS 460/Fall 2005/Homework/Cynthia York/Nov 12

From CSWiki

Jump to: navigation, search

[edit] Digits to Integer Conversion

  • This program uses FoldR for converting a list of digits to an integer.
local
   fun {Dig2IntFoldR Lst}
   % Converts a list of digits to a single integer using FoldR
      _#Value =
      {FoldR Lst
       % Dig = digit in Lst, Pwr#Sum is increasing power of 10 & running sum
       fun {$ Dig Pwr#Sum} (Pwr*10)#(Dig*Pwr + Sum) end 1#0}
   in
      Value
   end
in
   {Browse {Dig2IntFoldR [0]}}
   {Browse {Dig2IntFoldR [1 2 3 4 5 6 7 8 9]}}
   {Browse {Dig2IntFoldR [9 8 7 6 5 4 3 2 1 0]}}
end

  • This program uses List.foldRInd for converting a list of digits to an integer.
  • Presented this in class
local
   fun {Dig2IntFoldRInd Lst}
   % Converts a list of digits to a single integer using List.foldRInd
      Len = {List.length Lst}
   in
      {List.foldRInd Lst
       % Inx = index of Dig in Lst, Dig = digit in Lst, Sum is running sum
       fun {$ Inx Dig Sum} Sum + {Number.pow 10 Len-Inx}*Dig end 0}
   end
in
   {Browse {Dig2IntFoldRInd [0]}}
   {Browse {Dig2IntFoldRInd [1 2 3 4 5 6 7 8 9]}}
   {Browse {Dig2IntFoldRInd [9 8 7 6 5 4 3 2 1 0]}}
end


[edit] Two Puzzles from FreePuzzles.com

  • The following puzzle finds the five consecutive positive integers that will make the equation A^2 + B^2 + C^2 = D^2 + E^2 true.
local
   proc {Math004 Solution}
      % finds the consecutive values of A thru E such that when
      % plugged into the equation above the equation is true
      A B C D E 
      Vars = [A B C D E]
   in
      Solution = [a#A b#B c#C d#D e#E]
      Vars ::: 10#15                                      
      {FD.distinct Vars}

      % constrain to be consecutive
      thread
	 B - A =: 1
      end
      thread
	 C - B =: 1
      end
      thread
	 D - C =: 1
      end
      thread
	 E - D =: 1
      end
     
      % constrain values of A thru E
                 A * A 
               + B * B
               + C * C
      %---------------
      =: D * D + E * E
 
      {FD.distribute ff Vars}
    end
in
   {Browse {SearchAll Math004}}
   {ExploreAll Math004}
end
  • The following puzzle finds the value of A such that the computation 100/AA produces a result characteristic that is different from all the other values of A where A can be 1 through 9.
  • Presented this in class
local
   proc {Math013 Solution}
      % finds the value of A such that 100/AA produces a different 
      % characteristic from the other values of A.
      A B C Pairs=[B C B C B C]
      AFloat Float1 Int1 Lst SubLst  
   in
      A :: [1#9]                                      
      {FD.distribute ff [A]}
      AFloat = {Int.toFloat A}

      % calculates the result & move the decimal 8 places to the right
      Float1 = (100.0 / (AFloat*10.0 + AFloat))*100000000.0

      % converts the float value to integer
      Int1 = {Float.toInt Float1}

      % converts the integer to a list of digits
      Lst = {Int2Dig Int1 nil}

      % creates a sub list containing only  the common characteristic - repeating pairs
      % note: elements 1 and 2 do not repeat for some results and element 9 was rounded
      %       during the calculation of the result
      SubLst = {List.filterInd Lst 
		 fun{$ I B} if I > 2 andthen I < 9
			    then true
			    else false
			    end
		 end}

      % retrieves the first two elements of the sub list to create
      % the repeating Pairs list
      B = {List.nth SubLst 1}
      C = {List.nth SubLst 2}
      
      % checks that the sub list does not contain repeating pairs
      if {List.isPrefix SubLst Pairs} == false
      then
	 Solution = A
      else
	 fail
      end
   end

   proc {Int2Dig I Lst ?FnlLst}
   % converts an integer to a list of digits
      Dig
   in
      if I < 10
      then
	 FnlLst = {Append [I] Lst}
      else
	 Dig = {Int.'mod' I 10} % retrieving 1's digit from the integer
	 FnlLst = {Int2Dig {Int.'div' (I-Dig) 10} {Append [Dig] Lst}}
      end
   end
      
in
   {Browse {SearchOne Math013}}
   {ExploreOne Math013}
end