Anda di halaman 1dari 178




  
 

     


  
    


  

Contents
1 Introduction
 
                                   

   
                             
2 Invariants

             
 "          

"  # 
  

%
& '            
 "  "
  
      
( "
             
)  %'         
*    
       




























3 Crossing a River
  
                 

 +                 
  ,    -.     
 
/  %'      
                
 0               
 - 1 "   
2      

 
         
 3   "
(  
3 
     
)  4             
(  . 5 
     
) 

&                 












































































































































































1


5
!
!
$


)
$

21
















  
    






























































































































































































































































)
*

!
!
$

(
)
$


  


4 Games
( #   ,
         
(
-          
(  
        
( 
6       
(  +
 5
 
(   / ,
       
(( 
 . ,
           
(( &

&          
((
7 8 &

 9   
(( # 
 
     
((( "  #%: +     
(() ;   #%: +  
() 

&              
(*    
       























































































































































5 Knights and Knaves


) 6 <<                      
)
  6                   
)                    
) 
7   7              
)   %5&                
) ( = "                
) ) %5 . %5                
) %5    %5        
) %'
 .    & . %5 
)
> ? 6             
)( ?                         
))
                     
)* =   
                  
) 85                       
)! 

&                        
6 Induction
* %'
  
 
*
 "   
* "
     
*( 6 + 
*) "  ? + .










































































  
    





















































































































































































































































































































































































































































41
(
(
(
((
()
(!
)
)
))
)*
)
)$
*
*

















63
*
*(
*(
*)
**
*!
*$



(
*
$
!
!







85
!)
!$
$
$
$)


  


**
*

*!
*$

+
@A    
+/  3         
*  
+
   
*
 
      


&              
   ?        

7 The Towers of Hanoi


  A     
 "  % .   -9

8    
 -=B2         

8        
 "  8     
( 

&           
)    
    
8 The
!
!
!
!(
!)
!*
!
!!










Torch Problem
64  ;    
> &        
 5        
5  +4 "   
   ?

"  
        
            
   
     

9 Knights Circuit
$  /#  
$
5     
$    
$( 3        
$)  . >  <
$*    



























































































































































































































































































































































































































































































































































































































































































































$)
$$


)
)










107


!
$
$

)
*










117

!
 

 *
 $










133


(
(*
(!
(!

Solutions to Exercises

149

Bibliography

170


  
    


  




  
    


  

Chapter 1
Introduction
8   
    
  & & 4 "     . 
 
& 4 
     C
 
 C 4     . &
   &    
&  . 
 B 
     &
 
D E     
/  
      
 . F  
  .    .  G4 
    

 
4      .   
H     
 . 


 
 
          
/    &
A  "                4&  
 
 
   "    .
     
/     
      
   

1.1

Algorithms

  

  
  .
  /       /

  4/A    .  
 .     
'            

   '

&        . 
   
   
    .4  
 4   &  . 
 .
  '  4   B
& 4       
.  . 
+  4     8       &     4 
      & &    4  
 "    4
 & 4       &  
 "  .   F 

. 
     I 4  4     &
       .
  4 "  A   1
       2
    
5
    . 10
 "  
  .    .  

  
    


  

 
 
        4&  4      
 4    .      4  17

"       
C4  4 41   D &9C  &  /
5  .     4    .        &  /
  4 J K x  y    L  K z    L
"  5  .       
.    
   '   
   G
  H 17

 
 & &
      ?
&  
4 
   I .     
  
    4   
    &   /         8    .   /
  
  

   . 
    
 . 
      
     .      + '

.       
 1  3  19  20        30  .   
   
 1  4  5  6       17  "       
  .   

+
  

  
/  &       /
&  .
 & &   &     .     
 " 

     


      G+     /   

   & 4   


 .     H "  
4  
   .    "    . .
 


  .  .    
  
"  &  F   
/   
& .     . ' C  
  .  &   
'& " 
& . 
'&   &

  
      .     < . 
 
J
 &  
 
4      
  . 
 4   .  .  
 4  &    
   &
  & K  L           
 . 
    

 
     
/

"   
  
   4          
      8 
  
   
  .
 
 
      '
  & & 
 
 I    
/   
   &   
    &  &  &
 4 & 
&
 
  4 "    
  I

 .    
  5          & 
 

  
    


  

   


1.2

Bibliographic Remarks

8 A .     


 M6N  M N   
   /
 & "   
  4    KE  L  
   K; L  
I
      &   
D .4 
&  4 . 4

&


  
    


  


  
    

 
 


  

Chapter 2
Invariants
K8L
 K L   . 
      
   
& .       1  >  
 . KL  K L
K4L  KL
"    .    
  
/   &  


 "        .       

. '
 .  
-   4
    - A   
 .  
 . & 
  
 &
& A &    &
& A     
  
 8. & 1  
     ' "  . A 4  

   & &    
 A .  . 
-      .    
 & "  A  
4  
   4 4          
    4& 4
  
     
 

 C    . 




  4     
 "     
 4   4
4  5   4  .4 -    &       
 5  4 4   5  .  4    &    
"     
 5 &    4   4  4    
"    &   +
   .       
 
 &          '  .  
 "    
     
  I 4 
 4     
 
   &   

 B   A  

   
 Chocolate Bars
         5 & <   
     4& 8       5   
 &
         .    G"    
    4  H

  
    


  

  
+   4  43          A   "  
   &  

+ J

 /  


=4
&       
&        
 2
 Empty Boxes
%  
& '         4 
 .   '
        

'     4 
 .
 

'          
 '   
    .        102 
& ' =4
& '   
 2
 Tumblers
 
           
 
   4

      4&  G A  H 8  5      
  
  4&  =4   

&    &I  4

    &  


 
&

+  J "
  

+
4    .   
          
  
  4& 2
( Black and White Balls
   A 4  
 .   . 4        4 
"           &   .4 
 - 4

  
    


  


     
 .        &    .4  
 .   &
" & 4   .    8.     
   4  
4&
          I .  &  F    
  4         4      4&
% '  .           
 .      & I
4  &    .   
   -  . &      
   .   A            
 .  
  4  2
) Dominoes
      /   
/. 5 
     
 62 5 
 G A H  
 & . 
 

+ J #

 

 I  
 4  ' & 4 5 .     8
      62 5 .     4   
 4 
& 
    
     &    .  
2
* Tetrominoes
    A
 .
4 5 .   
 < "   A
F 
    >/ O/ 6/ "/  8/
 G A (H
"  .4 '          4 


      
  . 5 .   
 <     

   
 > 
  
     
.
   .      4
GH        4 
  4    
  .        
 . 5

  
    


  

  

+ (J >/ O/ 6/ "/  8/



G H         4 "/
  4  
  
 . 5  
 . 8 
G H         4 6/
  4  
  
 . 5  
 . 8 
GH  88     4  >/
  A. 6/

- & 2

2.1

Chocolate Bars

     

J
         5 & < 
       4& 8       5 
 
 &          .   
G"         4  H
=4
&       
&      
   2

2.1.1

The Solution

"       /   


  .4 -    
  

 .    &     
 .     &  "   

 .     
 .     -     4
     
4   
 .     
 .   "   
KL   K L .     .      
?4 4  4     <     F  4   

.      
 .        8   
  
 4 4&   
 4
&   
 "     

.   4 4&  
     
 .  %5&   
 .
 4 4&        
 .  

  
    


  

  
 

-                    



.          
 .  

2.1.2

The Mathematical Solution

>     . .&   


     &  
 
+    4   %            .
  
  
 
   +
 
'  

 
  
  &    
    
   6      

  4  4

Abstraction " 
 
    &   4   - 
  p    
 .    4    c    
 . 
"   .           .     
"  A     
 - K  L .
   
  
.   G K
LH   
&      
 .  
 
 8       

> .           
 &    4  
9 "   &    &   
 "   

 5& 4       


 .
   . 

"   
  
  K
 
 L  
        .

      K/4L  
 /4  
  &  . 

   I    
    

 
  & 
& &
>        
    5  .    

       < .      "       p
 c   
&      .         5  .
    
      74   & .   


 A     4        < .   
  "         

"       .     
 8  & &  . 
   .    & 
    
   /

  &   4     . .
& C    /

.   9C  "    
/    &   
  
  
G"'   
/ .  4  A " 
&   .&
   

 C. '
 4   A     .& 4  

 &  C        9 "   4  "  A   
'
   .       A "   
  4  


  
    


  



  

 "       A . 


  &   '
A
& A  >      A 4   
 I
 
  .


 . .
 F H
Assignments "  '      
1   
     . 
     -   &
 .        
p , c := p+1 , c+1 .

 
 
  4         "  4  
 &       K := L   K  
L "  .   


/  .   G    p , c H ?  


&  
  
     .  "       

/  . ' G   


p+1 , c+1 H "  
   5    
 .      . 
 
 F    .   
  '  & 
      '       "       &
     .       .  &    .   
'       8  '
    C  
 .   
  
 . C   &  p+1  c+1        
 . p  c &     & 8 4 p K  
L p+1   c
K  
L c+1  "   4   
 

     .
 
  .      
  .  
  
 .  .    4   

  '  .   
 + '
 pc    .  

 p , c := p+1 , c+1 
 E   '      .      G+
'
 pc   '     p  c H -      E
   
& &   . 5& 4    . E      .
E .          &   
 + '
   5&
A word of warning

   


 
  
 


  

 


 
    
  
 


    
  



 

       
  
 
       


 

 

 

 


  
  

  

 

       

 


 
  !


 

  
  
" 
  

    

#   
  "
   $ = %   
 
   





 
   

&   
  
   

 "


   " '  #   
 "  
  
  
  
     
   
   (()  !  
 

  


   


 $   

becomes

  %

  $    "


   %     
    


  
    


  

  
 



pc = (p+1) (c+1) ,

 4     . p  c  "      pc    .  



 p , c := p+1 , c+1  "  .  .   5&    ' E
         ' E .          &  


   '
  4  4   m  n   4   


m , n := m+3 , n1

-     m + 3n   &    


m + 3n = (m+3) + 3(n1) .


    4         m & 3  
&  
n & 1        . m + 3n 
,  ' E    
 ls := rs 
E[ls := rs]

      '   &       .     
E   ls &    '     . ' rs  = 

 '
J
(pc)[p , c := p+1 , c+1] = (p+1) (c+1)

(m + 3n)[m , n := m+3 , n1] = (m+3) + 3(n1)

(m+n+p)[m , n , p := 3n , m+3 , n1] = (3n) + (m+3) + (n1)

"    . 


      .4
E    .   
 ls := rs . .    .      E 

E[ls := rs] = E .


  
    


  

  

Induction "  A      .      


  '  
  . pc 
8& p = 1  c = 0   & pc = 1   pc    pc = 1

 4
&   
 -           5
p = s  4  s    
 . 5     
   
 .  c  A
sc = 1  "   c = s1  "  
 .         
 . 5
 
          
     


 "     & 
 8    .    .  '  /
 & 
 
        4    
 4

& 
   
   "   .   
   < 

   .   '    G &   &   

< 

   H 8.   
   ' &     
.   '    & 
 &   
 4 

&           
    .   ' 

              .   
 . 

 
?      .   
     8  & 
   .
< 8    .    /   
         
  
         ' &  5 G 4   <   5H
Summary "  
    .    /   
  
 .

  
/       C    
   - 4               
Exercise 2.1
 
      . 
 "4 & 
 
 
I        G 1 & &
H   4  
"  4 .   
    &    . .    &  
  
    1234 &   
 =4
& 
  & /
.   
 4   2 G=J        
H
2

2.2

Empty Boxes

"&     
&/ '  
    

%  
& '         4 

.   '        

'    

  
    


  

    



4 
 .  

'         

'   
    .        102 
& ' =4
& '
    2
"  .4      
   
 8      e  f .   
 . 
&    
 . .
'  &
 8.&     . e  f  8.&   A  . e 
 #     .    '   '   
  e 
f
( 8.&   .   

)


          A  . f  =      A
 . e+f 

?        &     


 .

'   

 . 
 '  4   .  4   
&  .    
            5  & /

 
"    &  F   
/J   
9

2.3

The Tumbler Problem

6  4   4     


  
     
 .  
 

 
           
 
  
4 
      4&  8  5      
  
  4&  =4   

&    &I 
4
    &  
 
& +
4  
 .   
          
     4& 2
"   
   4   D         
 .

     4 6    u 
"       F  .  4 .   
 "4 
  
      4&     4 "  
 &   


  
    


  

(

  
u := u+2 .

" 4 
      4     F  C u  
& 4 "  
 &   

u := u2 .

+&  4 


       4&  G     4
        4& H   F   u  8 

 
   

&  /   
 KL
 K  L  K   F L 8  
'
   5    

u := u ,

       
 .   
       &  
-    
 skip        &   ' 
skip .

"    . 4  .     


  '   .  A  
.     
  .    .  .    
%&     . skip   4    skip  -  .  
 .   4 
 u := u+2  u := u2  -    
. 4      4 .
u 2
"  4 J   /    . u  "  & . u     J  
  true  false  8  true . u   G< 4 .    H    false .
u   G   A   H 6  4 even.u .    5&
" 
(even.u)[u := u+2] = even.(u+2) = even.u .

"   even.u    .   


 u := u+2  
(even.u)[u := u2] = even.(u2) = even.u .

"   even.u     .   


 u := u2 
-     
 4
& 
 4  4 
    &
.   
 . /4 
 4   8.      
 
     4 4&    
I .      
    
  4 4&    

"               < /4 

O    
    4    5     
   

 . /4 
    

  
    


  

  

)

B   4          


.      4  
G 
(     &  H &  
  .   
         .        "  '     .

 P    &    
  
 . 
 

8.&    4      "     
 
         
 4& G=J     .   5
    H  
*GH       81  
&  
*G H 4  4  C  4 *GHC    '   =   . & 4


2.4

Tetrominoes

8     4     .  


*G H "      &
    & .
 
      
 &
     

        4 "/
  4  
  
 . 5  
 . 8 
 . & .    
      c   

 .  5 "     
     
 &
c := c+4 .

"  c mod 4   G c mod 4    


 .  c & 4  +
'
 7 mod 4  3   16 mod 4  0 H 8& c  0   c mod 4  0 mod 4 
4   0   c mod 4  4& 0  8 4 4 & K c  
 . 4  
 &L # .   4 K   &L  
 
4 & K c  
 . 4 L
?4    
   mn  G"     
 . 5
    m    
        n H "  c = mn 
 mn  
 . 4  +     mn . 4 
 m  n   

 . 4  
         m  n G  H  
 . 2 
?    .   
     
    
 &   "/
 -  4  D  4   .     
 
*GHJ .       & 
    .   
.    
    
"   & .     
*GH    4&     /

    
  "    . 
    
 & &
 .
   
   
 8 .    &   

  
    


  

*

  

K L  


             

 
 6    4& K L  
  
  

  0
   
  
  &    & 9
    4 4     & .       
    

' . ' 4 
 
  ' " /
    & 4    
 D  =      4 &J
 mn    4 


J c  
 . 4 
c = mn

mn  
 . 4

& .


m  
 . 2 n  
 . 2 .

"    4/   "  A    /  K


 L    
&   K L &
 "     

  mn    4 


 mn  
 . 4 
G& K mn    4 
   mn  

. 4 L  K mn    4 
    mn  
 .
4 LH
"  ' 4 &   .4   K L &
    4 &   /

   =       


 .   .        

.  5  4& 
 . 4 G4      .    H
  4   .    .  mn       
 . 
5  mn 
"       J
8. mn  
 . 4  m  
 . 2  n  
 . 2 
   K L &
 A  
  "  &
 K L
 KL ?
  & KL 4
 /  K  L C    &    m  n 

 . 2     /  K'  L 4


   m  

. 2  n  
 . 2        4 '     &
"          A  "  &         
4 ' 
 
 . 
 
 B
& 
&   .
/
 4     
  &      4 .
&

 & 4          
"    .        K.L 
 8 J

  
    


  

  



8.  mn    4 


 m  
 . 2  n 

 . 2 
"  & .  
 
       
 &J
 /

  '   4 '         4&  8/
  4  &   4 4 '
     
&
    4 I  &
&         8    &

K L
     4   '    # 
&
 4       6 4     4  K L   
  &
    4  4 .
   4   
 4   .  
 F  4&  
6  4    
*G H   &   
   
.     .  "/
 G8 1  . 8/
  41   
 4 1 8/
  4   
 . 8 H
-     "/
       5    D    
    5     5 F& .
      
   5 .        4&     
 4       4    4  5    /
 "  "/
        
 4& "    4 &
 4     5   4  5   4   4  5 
   5 -   
   "/
 G A )H  
  
     4      &     
 .    5
    .   


+ )J 3    "/



-   .          .    "    b 
  
 .    5 4  w    
 .  4 
5 8  d    
 .  "/
     
 l    
 .   

    
     
 &   

d , b , w := d+1 , b+3 , w+1 .

     


     
 &   

l , b , w := l+1 , b+1 , w+3 .

  .  
 

  
    


  

!

  
b 3d l ,

 
(b 3d l)[d , b , w := d+1 , b+3 , w+1]
=

A .  

(b+3) 3(d+1) l
=




b 3d l


(b 3d l)[l , b , w := l+1 , b+1 , w+3]
=

A .  

(b+1) 3d (l+1)
=




b 3d l .


&    .  
 
w 3l d .

?4     . b 3d l  <   4 4&  < 


 4

& "/
        
&    . w 3l d 4
4&  <
-  4      

      & "/


.
 
*GH 4 4     
 .       
 . 5
4 
     
 .   5
5   
 . 4  5

b=w

b 3d l = 0
w 3l d = 0

(b = w) (3d + l = 3l + d)


  
    


  



   !"





$
}

(b = w) (l = d)

b 3d l = 0
w 3l d = 0

b = w = 4d = 4l




b+w = 8d

b+w    
 .  5

  
 .  5  
 . 8 .
-    
8.       & "/
   
 . 
5    & 8 
B  4   *G H "   
 &
  *G H 4    
      
 4& 8   6    4& .   
5    4  = .    4& &         

 
     .        
 
G=4 & &         
       

 .   F  . & 
  8  1    
  *G H       *G H      & .&   
4   &   . "/
   
&    
& A 4    .            ' &
    
  4        
 &       4  
 H
 
*GH  & &   *G H    ,  9

2.5

Additional Exercises

Exercise 2.2 ,    .    .  D  "   


 .  D  
  & & 
 4  D  . F      
& 
 D  .     
8.& '    4       
     D  ' 

2


  
    


  

2.6

  

Bibliographic Remarks

"  
&/ '  
4  
 & -
+D "   
 .     
4    .
M,!N "  
  
 8 .    $$$   
   G JPP4444P<<PH @
@ - C.5 .
 
 C   .   

 /

   3   "     
& '
 .
 
 
<<  4   8 
 .  .  
     '
"    0
& -
 .   4  
    .
  
  
 
 . 4  8 
  . "  
  

 
    8   4   
%'   4  
 & 3
   8 4  G   & F
.
H      # 
  >&
  $)     
& @  KO  @&<&& #
  >&
L    
# 4 $!! "    .    
 &  


  
    


  

Chapter 3
Crossing a River
"  '
         
 .      
     -   
 
  . K /. L
   
 

 

 &
 & &    81   5  
1 5 &    5   . .    4 ; 
.    
  
    I  . .   4  
/
 
   
   .  1     . 


  
 "  
 . 4  '     
<  

  
   .   &  
 ?    .  4 4 
 .            .  
/
  
     
  4 
 
    
/

  '     .   
   4  



   
 >    
   4    4& 
.     8   .      
&  .     &

   4 1  4  =4  
   

F   
   
     .  /.   .  
  
 /   
       & 
&   &

&
4   4  .    "  ' . &

&   & 

 
/  5   .   & 4    . 
B
& &      
  
  4    .
/.   C4   4     . C  &  '
&
9 =4   .    . &

& 
 4  

 
  &    &
 
    
         
 .   

3   4   4 
             -
   4    & 
      
'& . 

  
    


  

     
 

  
     4    .  & 4. 


3.1

Problems

 Goat, Cabbage and Wolf


 .
 4   .&        4.     =4
   &      .  
  

  
      &         .  4    
G 4    4     H    4.     . 
4    G 4   4. 4    H
=4    .
     2
 The Jealous Couples
"   G    4.H 4     " &     
 & 
 4 
         & " 
    D .        4  4   4.  
4  
 .  &    
 
=4           2
 Adults and Children
  .        .   " &     
 &     

    4 


=4              2 #  & 
/
 &    

( Overweight
     3     .   " &   4   
 & 
     46    49    52  
3  100   1 4
=4   &        2

3.2
3.2.1

Brute Force
Goat, Cabbage and Wolf

"  /  /  4./ 


 .    /.    >

    4   /.        . . 



  
    


  

    #


 
/  & 4   
 
&    . 4 
      & .        

 .
 4   .&        4.    
=4    &      .  
  


        &         . 
4     G 4    4     H    4.
    .  4    G 4   4. 4   
H
=4    .
     2
"   
 .       .   4  
" 
   4     & .    . 4    .
4  -      f G. .
H g G. H c G.  H  w
G. 4.H  4       L G. .H  R G.  H   . R

 K     L   . L


    .  ?     
 4& 4    .
   4            

  

             
 
     4& .  .
     C       
8   /  / 4./ 
       4    .   .
   .       .     4 &
   
 A  + '
 4  4 4       
 J
  .     . 
 "  .
          4       4.    
. 
+   & 
  
  
  4    /
  "    4          4  
      "    4   
. 
   
  I .    
   
   8.    
    4    &    
   
  
-    4  /   
   /.   
   

8. .       . 4     24 G 'H F

 .  =4     

 .   
  '/
 "  5
        .  4      '
&     

  
    


  

     
f = g = c g = c .

"       .


              
  G f = g = c H
         F  G g = c H "  '   4  g
 c  5  F .
f  
&   5
      
 .  4   4.  ' &   &

f = g = w g = w .

8. 4    


        4       
"    4  4    F 
 G?    4  f  g 
5  
 . c  w  4I 4  f  g  F c  w
 5   5H
f
6
6
6
6
6






g
6
6
6
6

6





c
6
6


6

6
6



w
6

6

6

6

6


?4 4 
       4    "   
A   D   "   .    C  'C     
 .    C       'C   "  
 4           
   & .   '   K6666L     4   .  
  .  "  & 4  .
        4    .

               4.     .  "  
 &        K6666L '    K66L '
+
            4      
 % 
  &         .
K6666L '    KL ' "  
    .4 J
 "  .
              "     
.
6666  666

  
    


  

    #


RRRL
LLLL

RRLL

LLRL

LRLL

RLRR
RRLR

+ J ,/

LLRR

RRRR

LLLR

 / -./  

 "  .
              4    " 
    .
666  666
 "  .
    4.          "     
.
666  66
( "  .
           "      .
66  
"     &   4     K  L K4.L
        

3.2.2

State-Space Explosion

"   .   &  &  .  4    4  .  4  4
 
 =4    &   4       .   &
. . & 
  
 +    
      5 &  


   8   D  & 


    .    K 
L    
 "  /  /  4./ 
  I  
  C  9C   
  
 .   
 

 . 
-   4 5 &       4 & & 4    
  .      
  
    
8   K4 L  
    . 
     
         
  .        16   I
    /  /  4./ 
       < .   
      .
       . F I .

         4      


 . 
     .  
   
/
"       .   KD/ L  
  4
=    '    . 4 
        .  

  
    


  

     

  8. 4       


   
 .   26  
64 9 " 1  
  
 . & 
    4   4 1 
     
 .  8       D/ 
 
    A             
 " 

 .   


     210   1024  F    & 

 .  " J    K&L  
    

"  K// L  
   . .  .  
&
    &     A         "  
 .
      A     
 B    .     .  

"   .  
  .
 /.    .  
 . 4 
  K
L  
 4    KL  
   
  
 
'  "  /  K/  '  
L     4& "  / 
 
  K  'L & 4 8.    n   
  
       2n F       .
5 
 n  2n   &  
 -  .  K'L 4   

 .  G n    '  2n H -       .   .
 
 4 '& 
        .  

 &   5 
  
34 / 
  5& F   
  &
       .  
 4/   
   
 
 & .   
/ 8 
 5 &  
 
 
  
 C  .
  < .   4      
           
 
  2

3.2.3

Abstraction

"  /  '  .  &  .  & &   
I 
 & .5    &    "  /  /
/4.  
   '

8   /  //4.  
   
      K.
L  
KL   K  L    K4.L   4 &    4 
.2 8     .      4 
   K
&L 4  
4.       A &      . 4     4.   
  "  K
&L  
    J 4  4  
&

       . K4.L  K  L - &      K4.L 
  K  L   &   
F 
2
6      
   
 4  
    
   /
 &   4   4.      8     
 4 

  
    


  

 $   

    K L         4. K L


 .
 4   .&     4      =4
   &      .  
  

 
       &         .  4
 
=4    .
     2
?4    
 

    8    &  J
"               4     "  
       .4 &          &    
&        &     / 
.  
 

"   
/     4  .
  '
  & 

Avoid unnecessary or inappropriate naming.

-  
 .   
   
     
.
 

 .    
      < .      "    . 

 &       
      
 
   
     .  K  L 5&
    

 
     &  -     
  .  


      
    
  & 

3.3

Jealous Couples

@& .   


        .   
    

 
  
 "  
  
     .   
   &
 
  &
      &  
    & 
.  "          .
       

"  D/   
  '  '
 8    &  . 

   &      



 F &
  .
  
/  
   
J
"   G    4.H 4     " &  
    & 
 4 
       
 & "      D .        4 
4   4.   4  
 .  &    
 
=4           2

  
    


  

3.3.1

     

Whats The Problem?

. 4        


   &  
 4      .  
 

         & 4 K L         /
 "  &       & 
 . K L     J 
    .  4  .
.    .4 &   .
 
 .  
 4 
    . 
?4 &   K L & K L 4 .        & 4
        .&   & 
 .     
 G.     D .   9 H      4   

        & .      .&   & 



.      
"  
 &    F      
      
   
  4       
 
& 4    



 &   .&        2 # & 4    



 &   .& n      2 > &  



 &    4 G        .&


       
       & &     
H  4  D  4  
  
 .
& 4   .&    & .      4    
'



 .      .   4    & 8.    &  
G H  
'


 .   <  .    &  .    

'

    5  4
&    . 4   .  &
4  4
&    . 4   .  &  
"  4  
 
        8     
 4
    4 C    .      4  & 4  C
 4  5      .             .
    4   4 C   4  

    &
    A  
   .     


3.3.2

Problem Structure

"    .    


  4&  4  
    

+       "          
    "   4  
    
     
J    A
                   
   
      4     
'
K
. 
L 4  & A     4   .4 &   

  
    


  

 $   

  &  K  . &L 4  & A    
    .4 &    4
,    4   4      
    .   
  & "      .      &          &
 .   4  .
 
 ,        A 4 
  4 &    .  

  >        
   D   4
&          4  D  
  6 . 
   . &I   1 

 
 4  

"   4  &   & .    
  4   &
 8      4   .     "    . 
  .  .
.     4&  I        .
   
  .  .
   .    &

  
 9 8.       4 &    .   4     
D
 "    4  4 
G"  / 
.   /  / 4./ 
'     ./
  &

& & 4 "  


    &

& 4    /
  4.  &

 4   '  .


   

I &
  
  /.  4    & .     FH

3.3.3

Denoting States and Transitions

-  &   


    
    &   " 
  .      
 .    
  4 4 
              A  
= 4   H  W  C 
   4.    &
"     &  
I . '
 2H
 4   3C

    1C,2H
    4   - '   
  4   I . '
 1H,1W
   
 4. 4    .
  4  1C
     4. 4   .

 
?   4   
       . '
   
   8  &     .   4         
 
1  ?
   '
& 

 
    
-  4   
 
       4    G    4.H    .  
     & 4 5   &   '
  3H || 3W 
4       4           .     
4           '
 .    1C,2H || 2W  4  

  
    


  



     

    4     4       .   4 4  
     "       3C ||    5 A    || 3C 
 
  4  
          
'
  3H |2W| 1W I        .  4 4    
         .    4.      
?      .        .&      
.       . '   
      .  

  4   .          
&  &

"   4    P    & A + '/

 1C,1W || 1C,1H   G       4. 4      
  .
    
        4         .   H 
3H |3W|         & & 
 4 
8   
       
  5  
4    3C ||   4    || 3C     4  
 
       .  G8   
& . / /

    F    4 H     '
   .   8. p  q    S   5  .  
{

p }

S
{

   &   .   5  .   S  .


    p  
4    q   . '

{

2C,1H || 1W

3H |2W| 1W
{

3H || 3W

   &        4  4       
  .   4 4  4      4      
    .  4     4       
>.  4   4&     & .   8  & 


 
   
           .4

  
    


  

 $   

3.3.4



Problem Decomposition

;    4  '  & .  


    
 "  
     5  .   S0 .&
{ 3C || }

S0 { || 3C } .

> &   

  ' 4  .    




 "  ./  &

&
 "  .      &     4 .
       4   
  
   

"  &   &  


 S0    5  S1  S2  S3 
 
{ 3C || } S1 { 3H || 3W } ,
{ 3H || 3W } S2 { 3W || 3H } ,

{ 3W || 3H } S3 { || 3C } .

"  5  S1     .


        4     4 
                .  "  5  S2   
  . S1     4     .   4     
+&   5  S3      . S2     4  & 
        S1 .4 & S2 .4 & S3  4  4  &
S1 ; S2 ; S3  4      D  .     .
    G&
    . H    A  G&       H
"   
    
   4 4  ' &

&
 &   
 .  4   & &

&   &

   .  . S3 & &

     .  . S1  8. 4   


 . S3      .    4 .
        . 
 . 4   S1     
     S3  & .

- 4        
.   S1  S2 

      4           
  .   & G8    
     &  .  .  &H
=  4    

  
    


  

     
{

3C ||

1C,2H |2W|
;

1C,2H || 2W

1C,2H |1W| 1W
;

2C,1H || 1W

3H |2W| 1W
{

3H || 3W

} .

"  
{ 3C || } 1C,2H |2W| ; 1C,2H |1W| 1W ; 3H |2W| 1W

{ 3H || 3W } .

       5  S3     . S1 J


{

3W || 3H

1W |2W| 3H
;

1W || 2C,1H }

1W |1W| 1C,2H
;

2W || 1C,2H }

|2W| 1C,2H
{

|| 3C

-  4 .  4     .   S2  -      


&

     

?   . S2      .            A 
    .  "    5
 . S2  .4 S1   .4 &
S3  "   . S2
   
 4  .   
     5  8.     

 &

  
 &    .4 .
J
{

3H || 3W

T1
;

1C |1C| 1C

T2
{

3W || 3H

} .


  
    


  

 $   



?  &  


   C 1C |1C| 1C C  " 
&   .// 
    //. I 4    

&  "    4 


    &

 5  .   T1  T2 


8.  
    .
   .    
    &   
1C || 2C       2C || 1C  @ / .  
    .
.
      
    &    2C || 1C      
1C || 2C  "        /.    &  

4     
+& T1     8  . D 4  J
{

3H || 3W

3H |1W| 2W
;

1C,2H || 2W

1C |2H| 2W
{

1C || 2C }

&

 & . T2 4 J


{

2C || 1C }

2W |2H| 1C
;

2W || 1C,2H }

2W |1W| 3H
{

3W || 3H

} .

+&  &    4    


     D/ 
 
J
{

3C ||

1C,2H |2W| ; 1C,2H |1W| 1W ; 3H |2W| 1W


;

3H || 3W

3H |1W| 2W ; 1C |2H| 2W
;

1C || 2C }

1C |1C| 1C
;

2C || 1C }

2W |2H| 1C ; 2W |1W| 3H


  
    


  

(

     
;

3W || 3H

1W |2W| 3H ; 1W |1W| 1C,2H ; |2W| 1C,2H


{

|| 3C

G8      


    4 "     
  
 &    
    &  "
   

 
     H

3.3.5

A Review

  4   4  


       D/   
   
&  .&   4

 F      /.   
"         '   &

& 4   . 


       
 .  4  &     
/
     5  4    A    K
 
L  

 ?
   4 5   .
&  .&   .    
{ p } S { q }    .& 4          
"  A     " 1 
& . &  




&          4     &  



     .    & 


 #      
 

      '    
 
 +   .
.       .4   
J
Exercise 3.1 (Five-couple Problem) "   A D     
 & 
'

.    3
 4      
    
2
Exercise 3.2 (Four-couple Problem) ;.&   &

& 4   .


         & /   
  &


 "        . D       & 
'/


.          &

  3
     
 

"  .4 
&  . +     A  & 4 & 4 
    
.  A       G'  H  & 

.&    . A       . . B      
A 4     4&     .
    &  . 
 


  
    


  

   % &'     

)

G8       &

      . 


 &

  "       .


 .
    
 .       &

 .   .

   
 .H
2
Exercise 3.3  4   .      
'

. 4    
 
  . 
      
 4   .      
'

.      
  
 ' 
      
=J   
      "     J

 
 . .        
 "    &   
2

3.4

Rule of Sequential Composition

"  { p } S { q }  4  .    D/   


  
  .  .&    
 
 8    !
  G "& =    
   4    5 .
.
& .&     . 
 
I  4  .   A  
  H
 
 
  A &   4        
 "  4     A &  /  
  p    
    A &  
   q      
  .   
 
8. S   
  p  q   .   
 

pSq

   .   


  .& & p . '  . 

S  '  . S    
  .4   
  4
.& & q  + '
  
 
   
 r  
d .  
 M & 
 N 4   
N = 0 ,


  
    


  

*

     

G   & 0   4H   


M = Nd + r 0 r < N .

8.   
 S     A  .   
  
{ N = 0 } S { M = Nd + r 0 r < N } .


  . 
 & 5 I    
  '  
.      
   &    5  "  . S1  S2 
S3  
 S1 ; S2 ; S3    
   '  & A '  S1 
  '  S2     '  S3  "      "  
   
. S1  S2  S3 
 5 
      
4     
 
  
    
 8    .   
  4 

    p     q   
  r & 
 "   
.    
S .&    A 

pSq
    &  S  S1 ; S2    S1  S2  .&  
 A 

pS r
1



rS q
2

"  
  r        . S1    . S2 
8.    
  
      
 4 
  
 "   4  4      D/   
 "    


    3C ||    || 3C  "  
 
3H || 3W  3W || 3H 4       
   A  

"   F 4& .     . 5 
 "    .
    

&    
  &  
 

&     
 S1 I        .&
  
     A 
 S2  &    

&
   A 
 S2 I       
  . .&
  
  r     
 S1 
         
       
J

  
    


  

   % &'     



+  4     8       &   


 4        & &    4  

"    4  & 4       &  
 " 
.   F 
 . 
     I 4  4 
   &
       .   4 "  A  
1
       2
     5
    .
10
 "  
  .    .      
    4&  4      
 4    .      4  17

8  &     A       .      
"  A  
    4  4      4 4
 4     4          & 4   
   .    4  
    &   
    
   4 4   
   D& "  5 
4    &       &
"  
       '
        
   
4    4 4    G8 1 4&         & 
    4 4     '  (H ;   
  A .
      4  .   . 
 p  q    .  
.

p |5,10| q

     ' &

&     
  . 
 5 
.  S1  S2   p  q   
{ 1,2,5,10 || } S1 { p,5,10 || q }
{ p,5,10 || q } p |5,10| q { p || q,5,10 }


{ p || q,5,10 } S2 { || 1,2,5,10 } .

-  &  
   
Exercise 3.4 (The Torch Problem)
     
       
 
 .   .  .
   
    A 
 t1
       t2
     t3
    .
t4
 
   t1 t2 t3 t4  + 
  .   .    



     
 B
& 
     
  &
.4   4   &     
& &     .4 4 J


  
    


  

!

     

(a) "  
   1
 1
 3
  3

(b) "  
   1
 4
 4
  5


=J 8    A      G4  t1  t2  t3  t4  1  2  5 
10   &H     
    &    4 4   
=4   1 4&    &   &      .
  
&  .         B 4      

 .     4  
    .   
 8 
    
  & 4    .4  G?    GH
 G H 4 &    H
(a) =4
& 

                .
  2 G8          H =4
& .
    2
(b)


   
 . 
   D&
 
 4   
 .
 4   & &     
 .  4   
  
2 -  .   .  4 &   
   2 G,
   
   &  H

(c) ?4 .   4      4   


       
 -     F 2 %   
  .   = 
   .
 .  



     .    
   
(d) ,        
 ;   
    4 
&  

?     '  


       A      "   
D      
  
  
&   &
  
4      &     "    
4 .&
4      4  5
2
Exercise 3.5   /.           
G' 
(  H "  4
 
    F 4& .  . 
     =4
& 4&   2
2


  
    


  

 & (

3.5

$

Summary

8    4   /.   4  


 

/.     &       # 
  &

   
  
      . 
      
 /
     /  '
  
  
   .   

 4  
 4. 
 
'&  & 4  & 4 &  
  
   .     
I 
 4

  . 
  
  &   
 4      < .
      &
     
<
 
 
   '       .    


8  /   
  
     &

& 4   .   


 "        
 &  
  
   

  A    &

   
 4& ;.&   &  
 .    .    
       4&  &  



   .   & 



  &   
# 
&   
     &     
 4   &       

   4&    .   4   
 G 4    
H 

   
  
/      A  
    

'& .    
 "       
 C .
 
& .
    & /4   .   
 4      .4  
     & A
 
     
   
1 


  
    


  

(


  
    

     


  

Chapter 4
Games
"      4  4 
 
 4/ 
 ,
  &
 '
 . 
  
   & 
     4/
 "      

  G K
LH .   4      
    
   4
"  &  4      .         
  .   "       &  .&  '
    
 8         
  .     


"  '      
 . 
 4
      
  E .   
   4  +4  4  
  . &/

 & .& 4      
 G
  
 .

.&      .   
H        4  4
 K
  L K#  L   
  5 


 =  4
        4&  
   .
4     

4.1

Matchstick Games

 
 
   & 4  
  .
  "4 &  
  
 
 #  
  

  .
 .  
       "  
  4        


 "  & 4     
           &   
 

   
   '
 .         
 4

  K8
L
    .
 & 5&   &
G  . '
   
   4   &
 4    
   &
    H K 
 .
L
    &

  
    

(


  

 )

4   
  .   
 8    
     
   &   4     &     &I   & 
 
 .
      .   

       .
4   .  &  4&  .  4 
       .
4   &   4 4  &   . 
&       
. 
 .
4 
    4
  '
       .
    4
   

1  2
  "        4    
 .
 
 
 . 3 G     
 .
   0  3  6  9  H "  

    4  8. m    
 .
     
G m   
 . 3 H   &   
 m mod 3
   "  
  1  2     
   "          4 
  
 .
   
 . 3  " 
        0
 
.  4        &
  &
 4     
  
 .
  
     
 . 3 
8  
 
      
 
 4   &
  
 G    & . 
  ' H   4&   
        4    "  .4 ' 
 &       A 
 "     .
  % &  4  
 1
  -  
  4 2
 "     .
  % &  4  
 0
  - 
   4 2


 &        4  


    '
    
G 4   &  4  
 1  2
 H2 8   4  &
 4  4  
  4   4
   
     

 N
  4  N  
 
 A'   2

( "     .


  % &  4  
 1  3  4
 
-     4   4     4 &2
) "     .
  % &  4  
 1  3  4
 
'       4     1 
 G . & &
 
 1
  & '

   
 3  4
 H
-     4   4     4 &2
*



m mod 3

  


 
   


  
    



3


  

  *   & 

(

* "   4  .


  
       .
  

 1  2  3
  -     4   4    
4 &2
 "   4  .
  
     I .
  .  1 
2  3
 
&  
  .
     1   7
 
&
 
 -     4   4     4 &2
! "   4  .
  
     I .
  .  1 
3  4
 
&  
  .
     1  2
 
&
 
 -     4   4     4 &2

4.2

Winning Strategies

8     4 .
 4   5 .  4 & -  4  


   
 4  
   
   4
  .
 
 .
 I 4  4 4   &
 &       .  

        4        /. 
         .
 
  
   
  
  . 4           . 

      

4.2.1

Assumptions

-
  
 . 
     
        4 4

 - 
     
 .   A
 - 
     
    
 
 4   &
  


"  A 


   &    / &/  .    
  
 .   
 .   A "    
 
 &     
          
    4I 4 '     &      
 

    .
4    &     
 A&
     4  
 +  $%  
  
    
   , 
  $ 1 

  


1 2 3

4

 -    $ 1 

4%

4%



  

     


  
    


  

((

 )

4.2.2

Labelling Positions

"  A    4  


        
   

 + (    .  
   
        .  
(

+ (J #   ,


 &
&    4
    
       .     .   %     
    -     4     &     
  & 4  .
        

"    A (    &  


   
 .
  
 
   +
     0       8  
  
 .

    4  
  
 +
     1     ' & 
       0  +
    4  
 
   
& 
    
 
&  
   

  +
  
    4  +
     n  4  n    2    
        n1          n2  "   .

   4    
 . 

     2    4 

   4
 
= 4     4            
 4   & 4  A  
.     4  &
 . &   .    & 4  A  
.   4
    4        .
 
   
"      4   .       . 4
J

        .    .


      4 
       .      .
      

 A   
& 
    
     &   I .
   A  A    
 . 4  4     
     8 
      9 =4 4   &  
       4    "     .    
 .
    
 K&  .
      4 L 
 8         .   G/'H     4 

  
    


  

  *   & 

()

"      .    .    


 .   .
K& x 
& p L  4      " #
       " #
 
  
     
   4        .   K x L
   5A  8     
  K L G 
&H     

    
  A (    0    KL       
.
 8           .   
  .&    &
4  
 
 
?'  1  2    K4L   .
       
0  4  4 4      ?      4  A  
 
     
 .
   .   
    4
?4  3    KL     .
 3    G 1
 2 H   4  &   K4L +
   4     3

  
 &
      .
4   4  
 &   A  
.     4 & 
"    4      .        ?
4  5    K4L    6    KL    7  8
   K4L   
+ (  4    .              7  
     8  "         4 4   
I       4      .
4      
4 4      "       4
 .
  

+ ( J 6   -     &    
&    
 .
    "        
     4    
 .
   
 . 3  "  4
    
 I   4 &   
   4

            4    


 .
    
 
 . 3 

4.2.3

Formulating Requirements

"  
& 4        4 &   K
 L
  &     
 .
   
 . 3  8 

 


  
    


  

(*

 )

4 '   &  =  6 n    


 .
  
   "      .   4 &  ' &   .4
 

J
{

n  
 . 3   n = 0

if 1 n n := n1
;

2 n n := n2 fi

n   
 . 3 }

n := n (n mod 3)
{

n  
 . 3 }

"   A 


 .   

      "  A
      "  '   
   4  .
  
4    
 .
   
 . 3   /<
"       / 
       
 
  & K if / fi L   -        /
   C
  &   K 2 L &
C 
  
 . /   
  
 

    .
b S  4  b   / ' 
    S   
           

  '  &    

 4     true 


   '   & 8.     true    &   .


 
 8.  .      true  '     
8   4&   if / fi 
      
   &
 
/
 
  & 4 . 1 n I     
 n := n1  KL
&    
& 
 4
  C
 &   

n := n2 C  KL &    2 n     .    
 &       .   
   n = 0 
"    .    

     K n   



. 3 L "   
   A         .   
 .

   
 . 3    
 
       
 .
 
&   4    
 .  
   
 .
  4   

 . 3 
"  .  .   5     

 .   4 &I  ./
 & 
 n mod 3
  "  A.     A   4  
  . '  .   4 &   
 .
  4   

 . 3 
 ! 


 
 
 
 



    
 
 

    



 . (
 ((  
 !  
  
    

            
  



  !
(  
           


  
    


  

  *   & 

(

8 

&  .


   4  n  
 . 3  

  &
      4  n   
 . 3.  5&

 n mod 3
       4  n   
 . 3 
8   4 &    &       &

 & losing & "    G   4    
  H

 .&   & losing  "  4          


 .& losing  +  4     .&  4& .  
    4  
I   
         
# .
&     4     4 &
 .&
  .4  A 
{        }

   & GH



;

{ 4       }

& 4 &


{   }

8 

&  4 &   4& . 


      
 4 &        4     4&    
.4    J

 %    


 +
          &
    4


 +
 4    4&    &   4 & /
    

8.  &  .    4    &     8.  


          &    4 @ /
 .       4    A &    4
 .
     &    1    . 
 4
 

+
&  4 &
     5&
G  
 .
 
 .   
   H 5 G 
    H
       & 
   4    

.
 
  < 4     
   5& 4 4&  
  
 & & .  &

  
    


  

(!

 )

-  

   & 4 &    


  /
  
4   
    & 
 .
  .
1   M
&  
    " 

 M    
 A'    -  

   & &  


    
& A       M  0  "     & &
          & .   ?'       M
 1  "      &    &
 
  ?4 

  4  4      M  2  4      4  D 
3 &       2 8. & 1    

& &  
     & 4       M  3  G31 4  

       

  
 H "     
   M  0  1  2 G     '
  0  1 H   
     &  A +& .
     .   &
&  5  .   
  4    .      M  2 
Exercise 4.1 (31st December Game)
"4 & & 
  " 
4    & 4  
  3 
        0&
%  .   '    F  .       &  4 

 +     4 &  4  &   4  
.    4     &    &  
=J    &   
 .  . *) & G **     . 
 &H 4   
   &      .  & 8   
    
     &  
     
 4   &  & .&   &  3 
     
 


H G%&H  &  
    .   '
       & .  

&   & 
 G+ '
   A &  & 
  + &
    0&       H
H G=H  &      & &    
    

   .   '
 
2

4.3

Subtraction-Set Games

  .
   
       .
    GAH  .

I 
   
 m
  4  m   
 .     

         
       . 
    
 
 

  
    


  

 & 


 +& )

($

"  
 4  D    '
 .
   
I .      
1   M
&  
           {1..M}  # 
'
    &       4    
+ &        4     4&  
/
 - '
.&          &     4  
 4    4
 J

 
 
 
 
  
 
 
 .
 

8   4        {1 , 3 , 4} 


    
   &   
 .
      - . 
      
  K 0 L
     4    

  
     K 1 L
     4     D

       
 4  0   4 / &/      4 .&
4       4       

       . &


 .
    4  
     4  .    
 .
    

"         "  (  4    4    < .   
 
 6  "   4       
 4  4 4      
 4 G-H    G6H 8           4 
  
4  4   
 .
       
   

.
       + '
 2        
&
 .
2   1 I  3  4  4    .
 

  
  0  ?    
&     . 4
 + '

.
 3    4 4
 C 
 3
  
  
0   
 1
 
   2  8     D 
   

4 .    
 0
"&
6
#

1
1

2
6

3
3

4
4

5
3

6
4

"  (J - G-H  6 G6H  .     {1 , 3 , 4}

  
    


  

)

 )
 7
"&
6
#

8
1

9
6

10
3

11
4

12
3

13
4

"  ( J - G-H  6 G6H  .     {1 , 3 , 4}
     4    '       J    ( 

   (  (  4        . 4  
  . >          4&  4  
  . -
&  .     .       {1 , 3 , 4}  4  
       4    
 & 
   

r & .    
 .
  & 7  8. r  0  2       
 > 4    4  "  4 &   
 1

. r  1  
 3
  . r  3  5   
 4
  . r  4  6 
"       . 4          
'
    & .   / 
 4   5    . 
       4&    
 .   &  4  
     4  G .   4   4
H " 
.4 
     4 &
              
   A 

   
 6    M  "  .
     

M
 +   k   W.k  true . k   4   false
 4 -  k    M  W.k  
& 
 &   5 
W.(k1)  W.(k2)  . . .  W.(kM)     5  s.k  ?4    & 2M
F 5  .  .  M    5    5  s.(M+1) 
s.(M+2)  s.(M+3)  . . .
 &   
   4  
 2M
 "   . 
 j  k  4 M j < k < M+2M  4
  s.j = s.k  8
.4   W.j = W.k    5  W  .
  k   4
+   '
     &       -/6  4  .

  20   4 8 .    


  ,& 4
 &      . 4/  4    2M+M     
" 
 4     &    4    

    .   k  .    . k     
s.k    8.        R    .   & 
k  W.k 5 W.(k mod R) 
Exercise 4.2       .
  8 
 2  5  6
 

&  
 G"          {2 , 5 , 6} H


  
    


  

 & 


 +& )

)

(a) +  n  0 n < 22  


 4     . n
    4 
 
(b) 8.&      4     .&    & 
   .   .  . n   
 4     . n

    4   

@.&    &       4 4   . 1  
4  
 

2

+ (  4   .    8   


  
Exercise 4.3
 D   "  4 &     
    
 .
  .4 "    5 1    A  5 25 I   4    A
    A       
 .    .     
   .    . 4     .   I .        .
.    
     .   

21

22

23

24

25

20

19

18

17

16

11

12

13

14

15

10

+ (J   6 &     


    

.   .4


  
    


  

 )

(a) 6       


 G"      
    5 " 
.&   5  &     H
(b) 8.&   4    ;          
.
4  &
    4    4    .

4     
    
(c) 
 .      A  4      4& %'
4 &
2

4.4

Sums of Games

8     4   4  '     .  


    
 
4 &
 F &
"   '
 .
   
    ( 
     .

  -  
 
  .   
 A  I  
 

&  
 .
       
     4 
& F
.
   "  
     
 . 4 
I     4& .

 
      

8   4 
  4  4  .
 
    .
  
    
     .4 + & 4    4 
   
    
      

    
 .     
. 
         
 
    

  
  
.   

+ ((   '
 .   
. 4 
 %    

4       &      
   &  
 8
        
           
.        "      .        

&    
   &    4  .   

8   K
L .   
 4            
 .   4   
       .        
      "       K
L .   
   &  
:' 4  K:L 
      .    K'L 
         I

    F  .  ' &  . K:L  K'L
   .    
  A ((   I 5&   /
.           (     4  
  
4    =4   . 
  A ((  15 F 

  
    


  

 &  % )

)

+ ((J  
,
 "  .    
   &   4  
     :' 4  K:L    
 .      .    K'L 
  
 .          
  ' &  . :  '
     
  11 I     
.   4 
  1511 F 
+   
  . 
 . 
    /.     &  
8     4 & 4  
  4 & .   
. 4 
 -
A     
 F    
G     .  . 
H
.   F 5  
 4    .   
 

        - A 4         4 D  
4 & .    
 3     .
  
 .   &

4.4.1

Symmetry

A Simple Sum Game

-  4  & 


 '
 .   
. 4 
     4
 .
   4
    &  .     
  

  
    


  

)(

 )


 .
    > 4          
 .

   
&  
  4&   
   4   & 



"  
    K
L . 4   .   
 & & 
 
J 
 GH  .
  
   
   
 .
   8  

 
   4    &     4      
 
     4 &   
   
  "   
4     
  
    &  
8 5 &  
    4   4 & .    

    4   
.   
 8.  & 
   
  .

  C      4 & .    


C    
4 & 
   

       
"  &

& 4 .    4   &   4 &
 4  m  n    
 .
     4  8   
     5 
 .
     
& 0  "     
  m = n = 0  "           & m = n 
"       +
   4  m = n   
    G 
   1 m  1 n H &
 4     4  m = n   5&
    4    
 .
   
   ' 
 
.
   4    &   m = n 
+
&     .   4 &  ' &   .4 /
5  .   


{
if

m = n (m = 0 n = 0)

1 m   m

2 1 n   n
fi
;

{
if

m = n

m < n n := n (nm)

2 n < m m := m (mn)
fi
{

m=n

"  /
   4   m        1 m    
n        1 n 
   &   .
    

 " 
.      m     n        
m = n . 
 .  


  
    


  

 &  % )

))

"  & m = n      .   4 &   


%5& m < n  n < m  8      m < n  4 .   1 nm n  
  nm
    
 .
   4 n
    n(nm)

A  m       .   
 n := n(nm)    &
m = n 4  "   n < m  &

 
"  .4 5  .   

 

   /

 D  .   & .   4 & ? 4   4 

   4        "    '/
   
 & .  
I        losing &    
&  5    
{

m = n

m<n n<m

if

}
}

m < n { 1 nm n } n := n (nm) { m = n }

2 n < m { 1 mn m } m := m (mn) { m = n }
fi
{

4.4.2

m=n

Keep It Symmetrical!

"  
    ((    '
 .   
  . &

&I   4
&          4& .    . &

& 4
  4  
 .   
/
 -   &      4  4
 
/
 
 4     
 
"  
& '
 . 
 4  &

&    &  4 =  


 "     .     .   
The Daisy Problem   &  16   &

 & 


  "   4 & 
  
    4 D 
 "  4     4  
     -    4  4 
   4 &2 , &           & n
  
  . 
 4 1  M D   G4  M 
A'    .   
H
The Coin Problem "4 &         4  & 
 " &    
 & .    . & 
 " 
&                      &


  
    


  

)*

 )

+ ()J  16 / &


 &      "  4     4           
-    4  4     4 &2 G!  H -  . &   &

         D.& & 42

4.4.3

More Simple Sums

6    


   
      

    (( 
     
 .
      
        

 K
    
 .
   G4  K  A'   H
"  F  .       4 
 4
 8.  . m 
n    
 .
     4     4  
 mn

  4  K < mn  5&   & m = n      


  + '
 . K  A'  1      4     4

  4        


     J     
&  .  
     4  
 
I    
  
  
  4   


 A  F  .     
       & .   
&

&       -  . 4  &

& .  & 



F      4 J  . '
 4 
   
 M 
  
 .
   
&  
 .
  .   N    
 .

   
&  
 .
     4  M = N  & 
  .    
  
& F . '
 .   
   

        & 
 8.       4     
 &
/

&2 ?   .


. K&

&L   &    4 &J &

&

  
    


  

 &  % )

)

  
     &9
- 4    (      4&  4   / 
 4    
  
 M
    
   &     &  
  
 .    
 .
  & M+1  0  "  .   .
m
    
 m mod (M+1) 
 4        4
   "        4/ 
 K&

&L 4   


 .
    &  
m mod (M+1) = n mod (N+1) .

G M   
'


 .
      
 .
  .   N
  
'


     
 .
    H
"         8     4     0

    &  A    &  4& 


 .4
  &
 &      &   .4  


{
if

m mod (M+1) = n mod (N+1) (m = 0 n = 0) }


1 m   m & 
 M

2 1 n   n & 
 N
fi
;

m mod (M+1) = n mod (N+1) }

if m mod (M+1) < n mod (N+1) n := n (n mod (N+1) m mod (M+1))


2 n mod (N+1) < m mod (M+1) m := m (m mod (M+1) n mod (N+1))
fi
{

m mod (M+1) = n mod (N+1) }

G?J 4      .  . 4      


   


H

4.4.4

The MEX Function

"   . A K&

 L   K    


  5L  
    & 


  
      
. 4 
      

   
G l,r H 4  l       . 
  r         
 

 F  D  


I  
 
 &    GH 


  
    


  

)!

 )

l := l  G. 
 l  H    . 
   GH 
 r := r  G. 

r  H      

"     A 4 .  L  R  &  .      /
&    4&     G l,r H     ' & 4  L.l = R.r 
"  5 J 4       .  .&2 8   4 4 
4  .&   .  L  R 2
"  &   .  4 & 4       A 
+   G l,r H     .   

 ' & 4  l    
.   . 
  r     .     
 
       L
 R  5    
  & 4
 .
   C  G l,r H .&
L.l = R.r C              4  C /
 G l,r H .& L.l = R.r C  "  
{
if

L.l = R.r (l      r      ) }


l       l

2 r       r


fi
{

L.l = R.r }

"  &   4 & .


 4  C  G l,r H .&/
 L.l = R.r C        C  G l,r H .& L.l = R.r C
"  
{

L.l = R.r }

& 4 &


{

L.l = R.r }

-  .&   A    5


 . 4 A L  R   . 
4     .  
  5  J

 +   l  r .     


 L.l = 0 = R.r 
 + & l       
 .
l  l    . 
 L.l = L.l 



& . & r        
 .
r  r       

R.r = R.r  

?       .    
   .   .      
. 0    . 1      5  & "   .  
   .
    5
 8. L.l  R.r  F  


  
    


  

 &  % )

)$

  L.l < R.r  R.r < L.l  "  4   A     . &   4
& &  
      
 4  L.l < R.r   
 
  . 
 4  R.r < L.l  G 4H
{

L.l = R.r }

if

L.l < R.r  r

2 R.r < L.l  l


fi
{

L.l = R.r }

+    4 4 5  J

 + & 
 m    R.r      
 .
r    r

   R.r  = m  
& . & 
 n    L.l  
   

 .
l    l     L.l  = n 

"   5


  A . 4 A   .  L  R    
/  K
'L .  "    A .   .    .4
6 p      
 G  "  mex  . p   mexG.p 
 A     
  
 n    

 "    


    
 G .
p    q .&
mexG.q = n 

 + &  


 m    n      
   

 G .
p    q .& mexG.q = m 

K#'L    . K



 ' L  . .
   .  
'

 .   p   



    '  .
 
' 

.  q  4  
  
 .
p 

4.4.5

Using the MEX Function

-    
    A ((       .
' 
 +
(*  4  
' 
 .  .         

"       & &
  I 5&   & 4& 

  
' 
  &  /.   .   "   & 
&  "      
' 
 0   5& 
' 

      4       &   
' 

G   .   p    q         .
p  q H " 

  
    


  

*

 )

+ (*J #' ?


 " 
' 
 .      
  

   
  
' 
 .   

  & A   
 
         
' 

.    + (  4  &   "       .   A
  
' 
 4        
' 
 8  
  4  
' 
     2    .    
    
       4  
' 
 0  1 
?4  4 &   
 6        K>L " 
  4     
' 
 . K>L  F .
 
' 

. KL "     G 3  2 H    4 &  
   
       KL 4     

' 
  K>L "  
     
      .         4
' 

F .
2  "  A &      & .     
'

  5  &    
  . 
?     .     .   .    
 4   
   15  .   . 
   11  .     
  
    
' 
 .   8   . 4   

  
    


  

 &  % )

*

+ (J



' 
 "        
' 
 2 

  26 F      D   


G     .   4H
. 15  11   
        165  "      
 
 F #    4    < .   
 

 
Exercise 4.4 H      / 
 4       .
 
.
4  
 2  5  6
 
&  
    
' 
 .
   &   
H   
 4     
. 4 
 8   . 
 1  2
 

&  
    8     
 2  5  6
 
&  

8   

 
 
 &   &    . 
   
&      

"    4  4  
 . F     
   
 &   . 
J   
 .
     .     
 .

       

6. ,
   ,
 KL  4



2


2
)
)
2
*
$
2

(
2
"  (J +  
 K2L
+    4      4     + 4/
     4
    .
: m 4  K:L   . K6L G.
K. 
LH  KL G.   
H  m    
 .
    /


2


  
    


  

4.5

 )

Summary

"       


 4   
 4/ 

"  &  
 .       

#
  -   4
4      A    .
  A     
.
  /.      
 4    4  
/.    &   . 
    
 "  &
.   K
L . 4 
 '
A   4&    '   

   .  4   
 A  & .
   . K&

&L
4   .    
 4 4    
   A  .   K
'L
.   
  "   .
' .   &     F
  
 4       K
L . 4 
 

  /.   
,
  &     4/'  . # 
  4  4  &  
     8    &     
  & 
  
/
    >  .       
    .4     
 & .  &
  .
   
 4    .   .4   
&       
 .  '  '
 . K 
/
 
'&LI   &  .
 
  & 
   . 4 
   


   4 &  4
#' 
 4   &   ,&     K?
L  


' 
  

  K/,&L 
 .   /
 ?
  4/4
   
     .
  -  
    &  &       4 4 ?
  
 .

  4 
   
' 
 G-  
  4  
  

' 
 .   
. 4 
H

4.6

Bibliographic Remarks

"  4/
  K- -&L M ,! N & 
 4&  ,&   
  . 
  &
"   3 
 
 G'  (H   .
M3-N


  
    


  

Chapter 5
Knights and Knaves
"   .       A      .    1
 &    & "    4 & .  K L 4  4&
      KL 4  4&  6 <<    .   
   .


 &   4  4 4      

 
 &      
"  
      
 &  & C   
 n
    2n F    & 
      
    C   &   
& 4& .      
 8
    
  <<  & '      .

  

 4  4      

5.1

Logic Puzzles

=   &    .  // <<


 8  
            B   .   
4           "   J K"      
    
  8 
  L "   

(a)

   
 4            2

(b)

   
 4          2

  & 
   4 .    B   .  
4    
       - &    
 4   2
 "            & K 
-    .     
 .  2

  
    

*

   
 &L


  

*(

 ,   ,


(  C & K       4    L -  5  
&     
 4          2
) 3  5   4 &  
 4        
* -  5   &    
 4       2
 -  5   &    
 4         
 &
G      H2
! B 4   
 4     
 .       
B
&   &P 5  &  .  
 -     5 &
  2
$   
   .     4        
 1   .         . +
  
&P 5             4 4  & .   .
.        4   4 4  

5.2
5.2.1

Calculational Logic
Propositions

"    4          4 ' 4   

 -  . '
 4 
  '  m2n2  
  4        
     . (m+n)(mn)  & .  
 . m  n  - &   m2n2  (m+n)(mn)  "   4
m2n2 = (m+n)(mn) .

"   .        .   64  & & 
  
5 4 ' " &  K
L        &  
 4  
 4   &  KL        & 
& .    . &       ' -   

  "4 '


 . '
   < J
n+0 = n ,


nn = 0 ,


  
    


  

  
  -

*)

 . 4    4     .     n  - &  &   K.
 n L "  4  .  
    4  

  

 & +
'
 K & . L    
     5&J
(m+n)+p = m+(n+p) ,

4    .  m  n  p 


G  H       4 ' 4    / 
K L C      true  false  %'
 .  '  K 
&L G4     true  false    4   4  KL .H 
n = 0 G4     true  false      . n H  n < n+1 G4 
 true .  
 n H / '        $ 

        4  


  " 
  '
     
   /
  4  . '

m < n < p  4     4    / 
%
 . m < n 
n<p
6     4      4 . 
 I     
  .    

    6     .

  


 C     KL KL  K.L     

 
 
     
   "   .      
'
 .   4  
     
  G. . . .   . . . H %5&
  
    .  C .  A   & ,. - 
6 <
4   .
*(*  *  4  4   A  &  .
   C
 5& .    '  -   &   5& . 
  &     . 4   &      &  4
.  4 

5.2.2

Knights and Knaves

%5& .      <<        
     4&         4&  8.     .   
  
 K    L    true  false         


 &       
  K    
  .L      .  A     K    L 
  
  
 S. "             
.   4     
 "  
A=S .


  
    


  

**

 ,   ,

+ '
 .  & K       .L  
A=L ,

4  L      .   


 K       .L 8
4              .          
      .
;    .  & K8 
  L 4  
A=A .

"  1   & 9 



1    A
      4   4
'       4 
   &   
8.      &P 5 Q        5    
 . A = Q  "      4  K&L .         4 
& &         4  &  > 4    4 
KL + '
    5 K &   L   4 4 K&L
 A = A     5 K    2L  4  K&L .  &    

 & G A = B H  4 KL "   1   K&L  KL 
     .& . A = B 
      5      . 5& &  
     .  << .
       
 A '

 .    4            4          
  1   A = B      .    1   B = A 
 5&  &

 I  .   4  4 4&    


 ?
    
    &  &    . F  . A
 B 
"     . 5& .        '  
. 4         

5.2.3

Boolean Equality

%5& C & 


 . C   
 .    + 
  &   "   x = x 4     G &H . x       

"   x = y    
  y = x  "        "   . x = y  y = z
  x = z  +& . x = y  f  & .    f.x = f.y G4    A' 
 .   H "           "  "
 '   
%5&   &  -  &  E'& &

& 
&     4   . %5&    .  8  
.  4      true  false  -  4 & .   

  
    


  

  
  -

*

 .  4   .   &  &

& + '


 

     J .  x  y  z 
x + (y + z) = (x + y) + z


x (y z) = (x y) z .

" &    &

 J .  x  y 


x+y = y+x


xy = yx .

&

& . 5& 4   .   D   


  &

& . 5&
4     4     & . 5&2 8 5&   
2
"  4           5 1
   &
.  & .  &
  .   
 .  4 
    
.       
 "  ' (p = q) = r D 1
  4 
p  q  r  
     5    "   '   5&
.   -  p  q  r    
   
  
 p = q 4 r . 5& "   (p = q) = r  
.  

&    p = (q = r)  8 
   
   4  .
5& 8   4 
    4   5& .   
  C    &    "   .   p  q  r 
G)H

[Associativity]

((p = q) = r) = (p = (q = r)) .

B       & &      . (p = q) = r  .
p = (q = r)  
    B          . 4 
(p = q) = r  true    . 4    
 . p  q  r  true  8. 4 .
            false  (p = q) = r   false 
"   & . 5&   & 4. & .       
 
& . ' - 4   '
I  
& '
   
.4
"  E'& . 5&  ' &   
(p = p) = true .


  
    


  

*!

 ,   ,

"   .  p  4   & G


    H  . 
p  4  &    & . 5&  J
p = (p = true) .

"   
 

&   
.& ' & 
 K true L .

' .   .
p = true  -    
 4

5.2.4

Hidden Treasures

-  4      .           
6     A  
    ) -   4   .  
& K8 
   5        L2 6 A  . K   
  L  G  . K       L "    1 
 
A = G   4    
A = (A = G)

  
true
=

1 


A = (A = G)
=

5& .    

(A = A) = G
=

(A = A) = true 

  . 5 . 5

true = G
=

5&  &



G = true
=

G = (G = true)

G .

-                    


 4  
         
 4         .      & 4  

4        . & .4   .    . B 4  .


  
    


  

  
  -

*$

 5      & 4  K&L .   . .    .4  KL
.     .    .4
  4    4  
 6 Q    5    " 
 4 4        5 4  A = Q  6 L  K  
  . & .4   . .L "  5
    L    
   
    5 "   4 5   L = (A = Q)  
L = (A = Q)
=

5&   

(L = A) = Q .

   5 Q     L = A  "      5 K8    
. Q     . & .4   . .1 5      . Q& 
  1 L
?     &   & . 4  L  8
    
& 4  
 4             4     
&       4  8  .   5  
 4  

  P    .   5     P = A  8    .

 
'  P    5
&  
A

5.2.5

Equals for Equals

%5&    .


      & 6 <1 J . 4 '/
  5  '     .     = 4  

 '
 .    . 6 <1 
       .         & K
      
 &L +
  5   4   
 
 4        
"     
 4  A  B  C      G &
  H     -   Q    4 5
"   4 4  C   &   &    ) ( Q = (A = C)  
1 
  A = B   4 4   C = (A = B)    5 . 5
Q = (A = (A = B))   A = (A = B) 
A  B     5    
K    2L =    
         . Q  4
  4       
Q
=

 . .


 5


  
    


  



 ,   ,


A=C
=

.

1 
 C = (A = B) 

  . 5 . 5

A = (A = B)
=

 & . 5&

(A = A) = B
=

(A = A) = true

(true = B) = B

true = B
=

{
B .

5.3

Equivalence and Continued Equalities

  .   &  & A'   "  A   
 

 8.  &     G   (xy)z = x(yz) .


 x  y  z H 4  4 xyz 4  . . 
& "  '
 

 
    .   
 .   # 
&  
'   I 4
&   
.& xy  yz   4  
 
  8.      &

 G   xy = yx .  x


 y H           .       
 
 ' 4    
.& uw . &  .  ' u 
w
8A'    .  . &  - 4 . '
 0 m n 
=      
%
  J  
  0 m  m n 
8   4&   .
 
 
  G  m   4 4 H # 
/
& 4      .    0 n  "    &    
     & .   /
  8.    4 m  n
 m < n     m n  4 4 0 m < n  4
& .   0 < n  =  
.  
 
'      4       . 
    .
         .    
8    . 5& .   4   

 3 4 
/ 
 
 
   
   
 
     
 
    $ + %
 $ %
  ) 
 

   

  


  
    


  

 !' 


     !'   



5&        ' .   .

x=y=z

    5& .  . x  y  z 2 >  4   K &L 


(x = y) = z ,

 5& 
x = (y = z) ,

 D   
 4&  4 4  x+y+z 2 "  4   .&
   
 G. '
 true = false = false  false      A  
true           H "      
   
D 4         . .    
8 4  & .      x = y = z  &   4&
  x = y  y = z I  4  
 .  5  . '  &
5& &
 4     & .   '    D 
 G.   &H   &   C.  C    4
 5     &  
  F 
"      

    4 F &


   5& .
  C   &
 K = L 4    & .   5&    

     &
 K L 4    &    '  &
4 4  p = q  p q  -  p  q  '  
   
   
    '
pqr ,



   4  '   &   K L &
  
   
   C  (p q) r  p (q r)  4     

C 4    '
p=q=r

   
%
  C  p = q  q = r C  # & 
 "   .   .

p1 = p2 = . . . = pn

    . p1  p2  . . .  pn  5 4    " 


.  
.

p1 p2 . . . pn


  
    


  

 ,   ,

  
  & .&     ' G & 4& 4 
     
   F H       '    &  
  
# 4  

     K L &
     K5LI 
 .
 4   4   

 & 4    
 . 4   5& " &  &
   5   A '
   E'&
G) H

[Reflexivity]

5.3.1

true p p .

Examples of the Associativity of Equivalence

"       . . '


    F  .  
 & . 5 
Even and Odd Numbers "  A '
    .4 & .    
even  
 G 
   ' & 4    
 . 4H
m+n   m   n   .

8 4  . 4 .  4      


          .  

 "  . 4     
 
m+n   (m   n  ) ,

      
 m+n   ' & 4     . m  n   

    
(m+n   m  ) n   ,

       .   


 n   
 m     
& . m ' & 4  n  
  4& .    
      .        5/
  p q r   ' & 4    
 . p  q  r     
&  . F J





GG m+n
GG m+n
GG m+n
GG m+n






H
H
H
H






Gm
Gm
Gm
Gm






H
H
H
H







  
    

Gn
Gn
Gn
Gn






HH
HH
HH
HH 

  

 !' 


     !'   



"  & .   '


       .  & "   .
  
 .   4  K m  L  K n  L ;  
 & . 5     . K m+n  L  '   

.
 4  &  .   
 '        .
F    .  &    F  
Sign of Non-Zero Numbers "   .  
 & 4       

  + /< 
 x  y      xy   .    .
x  y  5 8.    . x  y  F     xy  

   x  y  /<     ' 
xy   x   y   .

0  .         


 &   
 . F
      &   8  DA  .    
  

xy   (x   y  ) .

"      C4        . x    4   

 & y ' & 4  y  C    K. .L .


   &
.  5&

5.3.2

On Natural Language

#&
 
       4   5& .    I
      . 4  
 . 4 F        
 
           .  .  .

  
5& #       5&  K.  & .L 8  
   5& . 
 & A     /
 G H  /
G H     A  K/
  /L 
" 
    '     .   
&   4   
.   .
 KL  KL    KL  &
     
 . 5    
 GK8.    8 4

& 
LH "  5& &
 4 A   
 
  &
     )) 4      & .
 
   5  I 4
5& KL  4    
  ?  
     5  "     '
   
5  K 
     4 & 5  
    
 & 5  
      &L
& 
&  . 
        & 9

  
    


  

(

 ,   ,

"  .            .  5   



 G
&  &  
 H   4   & 
     
  .   
  "      
    4  & + '
  
 

 4&  ' 
 
 4J  K5  L  K  L   5
 G
4  
 4    .       H  E 
     4&  

 & . '
 $J()  J 
&&    4  1 A      & J9 B    4  4
 &  4  4 4      
 F  4 $()  J 8
.   4 . 
     & .   .
 
            4    
   .  
 
   4 4&    4&   " 
 .
 
        
   
 . 
 "     

 KL     
 F 


5.4

Negation

   .4  //  


 "   4    
?  & K     5 8 
   L -   & 
  
  2
"   
  /     J    . KL ?   &
 G
      .  4 ' &  
H
  
      &   &
 K L 4   A'   
 8.
p    ' K p L    K p L
;       . 
  
 S  4 4   A S  4 
.    
J
A B A .

G- 4 .


K = L  K L     '  &H "    

.&   '
8        
    &   & .
  
 .  +  &  p    4  p J
G)H

[Negation]

p p false .

   
p = (p false) ,


  
    


  

 . 

)

 .    A .        4&J


(p p) = false

   4& . 
.&  ' 8    &

&
. 5 
   4     
    5   &
 4   4     &J
p = (p false) .

     //  


 4    J
A B A .

"  
A  B  .4J
A B A
=

 


A A B
=

4 G)H 4 p := A

false B
=

4 G)H 4 p := B  

B .

               ? 4 G)H    4


F 4&
"  4 G)H  D  4   &

&   & . 5 


  4& . 
.&  5   4   
 
 
 P   . '
 4 4  
.&
p p q p r q .

-  &     


        . K p L  K q L 
   "  4 
p p p q q r .

?4 4   G) H  G)H      


 .    . K p L  K q L 

  G & H 8     '
 4  
true p false r .


  
    


  

*

 ,   ,

+& 4  G) H  G)H  "         .
  
A

p r .

0  .       


 4   
A  .  

'    4  4  

&  
"  '
p + (p) + q + (p) + r + q + (q) + r + p

 
A 
q + 2r

&        . p  q  r      . p    


   . p       F       &  
"  4 4 G) H  G)H        A   4&   
  4 5 I    4 4 4      4 

 '
 . 4   4 4  
   .   false = true J
false
=

4 p p false 4 p := false

false false
=

4 true p p 4 p := false

true .

5.5

Contraposition

      4     4   & .     4 

      
G)(H

[Contraposition]

p q p q .

"  
 .     .       .
(p q) = (p q) 
-     .  
 &    /   
 G
 H       
    .  .
  .
        8. 4  n    
 .   l
 0
   
 $
% 
 
   
   
(



  
    


  

   



    K       .  .   L   .    

 &   


J
n , l := n+1 , l .

8 4   
 .    &        "  
.     
even.n l

     


 "    
(even.n l)[n , l := n+1 , l]
=

 .  

even.(n+1) l
=

even.(n+1) (even.n)

(even.n) l
=



even.n l .

-     &        .    <    

4     even.n l  & true  8 4        . 
5   
 .   
  '
    .4    5 
  5 

4& &    5   4 4  G A )H =4    
 &     & 
 &     90   .  .
 8    
     2 8.  42 8.  4 & 2

+ )J #  & 



"  4      
     
  &  
  / '       E   & 4   
4  5     4  .   5    
 <   

 G + ) H    
  &     5 " 
5
  
   
 .
  /      5  
 /    4  5

  
    


  

!

 ,   ,

?4   col     .   5     


   G&
true .    false . 4 H  dir          
 
.  G& true .  /  false . /4H "     

  &    &   
J
col , dir := col , dir

"   .       .   


 
col dir .

    .   ' 4 


 5     
 4
&

   
   
   
 4&  5 
           "        . col dir 
  
     & &    
    
8 4   .    
   90     
        5     .   /
4   . 4        4  5  .   / 

90

+ ) J 8 4 
  & 


Exercise 5.5 (Knights Move)


8   
 .    1
  4  
  4     .        4   .     
    4      88  . 5


  
    


  

/ 0 

$

 4     
  
    .
  
/.  .  /
    /      4&   & 5      
' &  
=J =4
&
   
2 # 
  
 .   F    

 .
     .   5  4       I .&
  4   4      

2

5.6

Handshake Problems

6   .   .


   /      
"  
 '
 .     
  J      & 

     
 1       
 . 

 &     4     4     
 
   4 4     
    .    "  
         I    
      &   8   K &
L  
   . & 4  C0   0 &C 0   
 4 0      . G8       & /
.  &
      .  . 4 
H 8   K&

 L

   . & 4  C0   0 &C 0     4
0 5 0    4 0  +&   K/E'L
  
/    4  

-  5   4   G H 4       
 

. 
 6  '   5  .    .    /  
4      
 . 
       
    n  "  &    4 
 n 
=4   /E'& &        4  
 -
    &    4 4 0  n1 
"   n 
     0  n1  "   . K4   
   
 
 . 
L  K&       
 . 
L
8   
    0 
  
    n1 

"  &

& .    /  


   
 
 4   K  L  S    4  x  y  .
  8   4& xSy    K x    4  y L  D
x    4 y  "    &

& . K  L      . 


x  y 
xSy ySx .


  
    


  

!

 ,   ,

"   .       .  x  y 


(xSy) (ySx) .

8 4 x 1    4 y 5 y 1    4 x  ?4
  a    4    b    4 &
"     a      4 b   (aSb)   b   
4 a   bSa       5 . 5 4   (aSb) 
aSb  4   .
"  
   &    4    
 .  
       4     4 
      


 . 

? .& 4   &

&  /E'& .    /  


   - 4    
  
  F   
 

   F + '
 . 4   K  L & 
  
.
.   K 4  L 4    &

    &  


 G    4    4          
 H =4 . K  L    & K L   & 
 6 K  L K L   &

  /E' 


Exercise 5.6 =       
 81  
    
     .    

   
J K  L   &

 
  K1   L
  
 .  G    4.H   & 
 
       =   4.     >   
K L  &  4
& 
  &        ./
. 4 & 
 =4
& 
        1   
2
2

5.7

Inequivalence

8    //  



     .   )( 
 
  K  F .

&.L "  
  .
  B = A  
(B = A) "    .    
  & K     5 8 
   L
   .4    4 ?   4 4 .
K = L  K L   
   '  &
 /    
   1 2  "     
 
2     
   


  
    


  

1 ' 




!

(B A)
=

  4 p p false 4 p := (B A)

B A false
=

  4 p p false 4 p := A

B A .

-     .   p  q 


G)H

[Inequivalence]

(p q) p q .

? 4  & . 5     &      ?


 4  & . 5     

& .       4


 .     .  "  A       &J
(p q) = (p q) ,

    
 . 4  &J
((p q) p) = q .

"   (p q)  & 4 p  q  "      " 



G 
       H 85     J
(p  q)  r
=

A . K  L  4 

((p q) r)
=

G)H 4 p,q := (p q) , r

(p q) r
=

 G)(H 4 p,q := p q , r

p q r
=

 G)(H 4 p,q := p , q r

p (q r)
=

G)H 4 p,q := p , q r

(p (q r))
=

A . K  L  4 

p  (q  r) .


  
    


  

 ,   ,

   4  4  
    " 
p  q  r 4  . . 
/
& ?     &  4   4   p  q  r  p q r 
5
  A 4 '
 4  4   5    4 5 J
(p  q) r
=

'   A . p  q

(p q) r
=

(p q) p q

p q r
=

 &

& . 5    4 G)H


     .
(p q) q p
4 p,q := q,r

p (q r)
=

A . q  r

p (q  r) .
Exercise 5.8

.&   .4 G?        
 
4   &     '       P
 1
 & F H
(a) false  false  false
(b) true  true  true  true
(c) false  true  false  true
(d) p p p p p
(e) p  q q p
(f) p  q r p
(g) p p  p  p p
(h) p p  p  p p  p
     


  
     

p = q = r 

 




  !"
   
       
  


  
    


  

2 & (

!

2
Exercise 5.9

   true = false

2
Exercise 5.10 (Double Negation)

    .   

p = p .
2
Exercise 5.11 (Encryption)

"  .    5       

(p  (q  r)) ((p  q)  r) ,

    &  "  &    b .   & a     
 & .
. b    
  a  b  "     &    
 c     
  "          
 & a  

a  c   4   .  b   &     &    4&     b
& .   & a 
2

>    .     &   4  


Exercise 5.12
  -  5   &    
 4       F
&2
2

5.8

Summary

8    4   


  <<      5  C
  5& .  C   
 .
    %5 
   
  & .          
. 5& %' .    & . 5  
    
F   &    .      <<
  
  
 
 $   (%    

3       

    (
 "

  
 !"
 
  
  "
  

  

1   


 


  



  "

          4


  
    


  

!(

 ,   ,

"   & . 5           & . 
  '      =4       
  '  .   "  . /   .   <  

      &
 K 0 L     
      
    KL
 .   
D 

  F  
"   .      

 KL  KL   
 
 4. 
"  .    5& .        4      
$ 1   
 & . "   3   4    & 
   0 64 < G    K>   
 
.  L M")*NI
"   .
  H ?   . 4    
     . & %- 3D   4  

  
 
 

  G  M3$NH


"   .    <<  &
 
&1  K-  8 "  ?
 >.
"  2L M
!N "      4    .

 
<<     .     '  ,R1   &  

 
&1 .  &    & "  ' .  
 & . 5      .  <<    , -
M-!N +  
   .     4      .
D  GKLH D  GKLH .4/.
GK.LH  
  GK& .LH
 M N  M,$N


  
    


  

Chapter 6
Induction
K8 L    
     
/  5      
 
   .   
     .    

"      4 
 4
   K<L .   .   
 +
'
    

     .
   4    
 .
 
  
I    .    
       .
   
<    
 .
       5
      <   /
4  
 C   0  1  2  3    -    
    .  /
 4  
  ;& 4   < .    .   


 5   .
   
 
=   4 
 < 4       
 4  + 4
  
 . < 0  "        .     
  &
  
  & &   G" &  . 
  KLH  
4  4 4   .   &  
 n    
. < n+1  
     
. < n  "      
 
&     4    
 . < 0  -  4 4    

. < 1 I 4 &             
 . < 1 .

  4    


 . < 0  "  4 4 4    
 . <
2 I 4 &              
 . < 2 .

  4    


 . < 1       -  4   

. < 3     
 . < 4   

6.1

Example Problems

   .4  


    &   8   A   <    

.  8         
   ' &  &   
 n  
Warning 

#


       

     


 

  








      


  
 
 


  
    

!)


  

!*

/ 
 

   .      
 .  8         & B  
        -     
   
  
 Cutting the Plane
 
 .     4      .    '
   4& .
.
      A * 8   4&   
    
 .   4          
   4      4 D      
  G   
    4     . &  
  F H

+ *J    - 



 Triominoes
 5   .       . < 2n2n  4  n   

 "   5    "  >  5 
       .    
   6/ 
 .  
 5 + *  4    .  88  4  5 
>       

 4           
 5 4 G/H

 G+ *  4     H
?J "   n = 0       & 
 Trapeziums
 5  4  .  2n . 
  
 n  

 . 
 5  "  
 5   
  /  <

 .
  5   A *(
 *

  


  

0 1 2


  
    




  

/  !" 

!

+ * J "
  


+ *J "


  
   A * 
 4           
  4 G/H
<
  A *) .          n  2 
? 8     n = 0  & 
( Towers of Hanoi
"  "4 . =  

 .
 <<
  !! &   +
S

 
  %
6     &
# 
"  <<         4      
  

4         A'     >   A .   
   
 .   41  ,   '& .    .
F <     . < G A **H "  

 4
    .
      & .
      
       
&      
  " 
1  4
 
 4   &    
     .
  A .  

  
    


  

!!

/ 
 

+ *(J  &


 . %5 "

+ *)J   A *(


          &    & 
      4 4

   9
          
 "     4    
   

G-           &         


  =4       .    /    
     
H


  
    


  

/      

!$


+ **J "4 . =  

6.2

Cutting The Plane

    
 .    
J
 
 .     4      .   
'    4& .
.
      A * 8  
4&        
 .   4      
       4      4 D     

  G        4     . & 

  F H
+    
   
 .     
 .   K<L .  
 
 "           
K &      
 . L
" 
   4    4 4      
4     < 
C     K L .    C  4    4 4      

4     n+1  4  n    & 


 
   4  
   
4     n  C       C 
+ & 4    .    4   &    4 D 
    
     

 

  
    


  

$

/ 
 

"   4     <   & "    .     
          4    
   
.   G        . D  H
+      4 
    
 .    4   
  .     F        4     
4 D      
  "  
     

     - 4        4     "  4
 
 .   '   4I   .  4    

     '    . & + *   '
 " 
     4  & A        
 4   5     4   . &    " 
    F  .  . .     4       

.  & . >    .           
 
%4  D    F  "      4 4 
.&
          
  . & 

+ *J

       4  

"  &             . & . & /
 G         4    /H    . &
 ?4          .   4  6 
          G&     . 
 4   
&
     
  .
  
.    8  D  
& 

 4& . 
   H ?     
   
. &  .   .   .      8   
     .           &  
.            "    . & 
.   .  G       .  . &    /
. & H 8    . &  .      G    

  
    


  

/      

$

 1   4 . & &H     . D 
    & .   .      . &    & 
 .
   
   F
+ *!  4   F    '
      .   
 
    .   
 

+ *!J     "        .    
G     4   
  4    H
"  
      8   &        
.    
4  &   4  &    4    . 
"       / &/ % 
       ' 

A                 
"  
 /
   4& "    .    
.  G    4 H   A "   .    G4    A
4    '  H    A +& 4        K.L
  4    K L    A " 
     A 

&      . F 4&    1


 "  A 
    K. &L  5     
 A 
Exercise 6.1
  &  &       

- &   5        2 =4
    
  '
4     
"   

     4     .  8    
 .     4    .  .       .  .   2
- 
     
.      /
  =4

& F     2


2


  
    


  

6.3

/ 
 

Triominoes

    '
 .             

   *     
 .    

 5   .       . < 2n2n  4  n 
  
 "   5    "  > 
5         .    
   6/ 

 .    5  4           



5 4 G/H 

"   
 .   K<L .   .    
       

 n  -     
&    n.
"     4  n 5 0  "      < 2020   11  " 
    ' &  5 "   5   &       
  5   8  0 
    59 "    
4      
?4  4    . < 2n+12n+1  -
   
 
           &  . < 2n2n 4 
 . A 
 &  5    -    4 4  '   & 
      . < 2n+12n+1 . 4   5   
  . < 2n+12n+1      4   . < 2n2n  
&
& 4 <         
 .   6
    .                   >
 5  &  "  5 4    .   .  / -
&

        
/.  G8.          
         
   H
"  
/.       . < 2n2n . 4   5  
 &     &    
 5    
/.  
  4 
 "    4    .    
/ 
/.  /   4 

? .   5          & -  &    
&    
. D  5   .       "    &
   
    D  .        4  A *$
?4     &        
 5 .   
/
  /.  /   4 
 > 
 .      
 2n+12n+1     4 

Exercise 6.2

   <
 
    *


  
    


  

/ -  # 

$

+ *$J "


  
 8   "      .  /
"   5  4    A   /  
  4  
      D  .        "    &     
   .  / 4 


6.4

Looking For Patterns

8   *  * 4   4        
 . 
 K<L "  &     4      K
 
   LI
K L    
&  
 
K8 L    . '
   '
    .     .
 4  &  4  . .
   .    .

'
 .            34  .
   &
.  &         .   
 . 

  .   4 8 
 
          
64 .
 &    .    &   4  4   &
       & 4 4 8   '
   
4  4  &   I  &   &      &

 
&      .         . 8 
 
      . . 4 .
' 4 4  &    

           4  4   &    
8   4   .
' 4        . 4  
 &    
& .
 . ' 4
(   
     
 .      81  /
 .  .     .   .
    
%

      4     D     
 .
' 4
,//.&   . 4& . 


 
    G,   
.
 .  D I A       .   4      

  
    


  

$(

/ 
 

 H
 .  
   
    (   '
 .
 /

      . '
   
      (  J   
  .
  .
4    4  
   4
  %'
  
 4       4 0  3  6
      
 4 1  2  4  5  7  8
   4  "  
  
     
J        4    
 .

   
 . 3   4     
  "   
D     
 .
   D   =4
4  .&     D    & 
 
      
 4 &
8      4
   K<L .   .
   &  

 .
   &   
 .
   & 3   4   
  
    K<L .   . 0  1 
   0    K<L
.   . 3  4  5
   1     "    &      
. 3n
         . 3n + 1  3n + 2
    4

"   .      4  n 5 0    . 0
    
    & A   
   4        

   . 1  2
    4      &  

 
         
?4 .      4 
     . 3n
     
   . 3n + 1  3n + 2
    4  -    4  
  . 3(n+1)
         . 3(n+1) + 1  3(n+1) + 2

    4 
    3(n+1)
  "  & 4    
 
 1 
2
     3(n+1) 1  3(n+1) 2   "      
. 4   3n + 2  3n + 1
   &     &    
     4  =      4     3(n+1)  
 
?4     3(n+1) + 1  3(n+1) + 2
  &  1
 
  A   2
          &      
 4     3(n+1)
  "  4 4 4     
=      4     3(n+1) + 1  3(n+1) + 2   4

"  
       .   4
    A  
D          ' & 4    
 .
   

 . 3 

  
    


  

/  . # %

6.5

$)

The Need For Proof

-          & D   & A 8   & 
' .
 .4   
  
     #& D 
    .I & &  D   
    . .  4   .
  & "        / '
 .  . D 
 n  
     
.  .         
       D  
 "        4&   
   .  .     "  5   4
& 
      2
+ *  4    4  n  1  2  3  4 

+ *J

   

"  
 .    & 1  2  4  8  "      

 .  .  & n   2n1  8   D    &
     n = 5  G-      4   AH 8      

.   16  4   251  =4 . n = 6    
 .   31 9
G A *H ?   n = 6    A   4       4 
    5      

= 4  &       n = 0        D 
4 &    C  1
   &      201 /
          
 9 "  &  5
4&    
     
   5
   n  F .
0 
"   .     .
 .   
 .    
  
             n 5 0 9

6.6

From Verification to Construction

8
 
  '    .     4 .
 @A  

   
D 4   C  
      
 . & 

  
    


  

$*

/ 
 

+ *J

    "   n = 6  "  


 .   31   261

   
  4   .
      A  . 1 D  
4 A      
     .

8    
  
      A     /
    .
     
 
 . 
 
 " 
       .     
 
 .
 
  .

"   
4   4  
   .
 .   
.  
k  4 .   A n  

 4/4 .
    
.    
 .
1   n :
1+2+ ... +n =

1
n(n+1) .
2

"4   '  .  


 
  '   .&  
12 + 22 + . . . + n2 =

1
n(n+1)(2n + 1)
6

13 + 23 + . . . + n3 =

1 2
n (n+1)2 .
4



 4    '


 .    .     .
 
   
  '
     4 . A J    5 4 .   4
 4  4   .   4   & 49  . '

  &  4   
   .
 .   
.   4  4 .
  A n 

14 + 24 + . . . + n4 = ? .


  
    


  

// # 3 4


   
 

$

=4 4 &  2 @A       .
 
   
  
      4 & 4      .   5
 &  4       4     2  &  4     
 4          
 

  & k27 2 


 &
9
    /  
      " 

    
 . 4   &  //   
&

     4  


    &
 
   /
    &   
8         .
 .  

 "  


      .
      
 
  
   
.&    "  &     
 & 31  /
 6  
4 
 
   
 
     .
 &      . m 5  1  2 
3    
.   m  4 .   A n 
   &
  n . 
m+1  G"  
.   A n 
   5 .  . n    
.  
A n 5     .  . n     
.   A n     5
.  . n H "     A
    G./.H    m  0 J
10 + 20 + . . . + n0 = n .

 & . 


   .
 . &   
.   . 4   
       A.  &
  n     
 

 


   "         5         
&  4 4      .
 . 1 + 2 + . . . + n  G
  4
& 4  
 4& .    .
       8.     
   4  " 
      
 H
- D      5 .
      &
  n  &
a + bn + cn2          a  b  c  =  4    

+ &    S.n  
1+2+ ... +n .

-   P.n     


S.n = a + bn + cn2 .

" 
P.0
=

A . P


  
    


  

$!

/ 
 
S.0 = a + b0 + c02
=

S.0 = 0 G  
.  
&  . 


 <H  




0=a .

    .      4       a      . n0  


0  ?4 4   b  c  "   4
     &    0 n
 P.n   " 
P.(n+1)
=

A . P  a = 0

S.(n+1) = b(n+1) + c(n+1)2


=

 .  .     & 

S.(n+1) = S.n + n + 1

S.n + n + 1 = b(n+1) + c(n+1)2


=


J P.n   a = 0 

"   S.n = bn + cn2

bn + cn2 + n + 1 = b(n+1) + c(n+1)2


=




cn2 + (b+1)n + 1 = cn2 + (b + 2c)n + b + c


   . 4 . n

c = c b+1 = b + 2c 1 = b + c
=




{
1
2

=c

1
2

=b .

+
  D      
.   A n 
   5  n  4 
     
1+2+ ... +n =

1
1
n + n2 .
2
2

%' .
          
  
 '
1m + 2m + . . . + nm   &
 .  . &   
 m  "  
   
J      

   &
  n 4 
m+1  ;     .
 
      4   .    S.0  0
 S.(n+1)  S.n + (n+1)m G4  S.n  1m + 2m + . . . + nm H  


  
    


  

/1 #+  5


 

$$

 &
. 
 5      +&    &
.
5
)  J 8    .   
1 + 2 + . . . + n      4&    
  .
 
& 4 4   5 



n 

n1 T



1 



n+1 

 

& 4 
n

"     4 4  J


n+1 T

n+1 T

+
  .       n    . n+1 4       

1
n(n+1)  =4  
     . 
 1m + 2m + . . . + nm
2
. m    1  *   
Exercise 6.3

;    5 D 


     .
 .

10 + 20 + . . . + n0  12 + 22 + . . . + n2 .
2
Exercise 6.4
 
   
 4   .
  .
4  m
  n
    
 &   .4 
 '
 G. '
 m
 1  n   &  m  2  n  3 H .
    . 

4     4   4        4    4
& 
    
  "&  .&  
     4&
4      8        
      .    
2

6.7

Fake-Coin Detection

" 
 .   **   

  K31 9


        

  
    

 L -   


  



/ 
 

 4    


 .   .   
 <    - 
   
  
   
  K.L        KL
 KL     
 4  4   K.L    F 4 
  KL  "   
 4      .   
&   
A   .  .    
?   
 .      

I 4 1 & 4  4

&      K
&L "     >.   
 .  

   & .&    


. > .
 .    
 
        . KL  
 
     
 K   L  

6.7.1

Problem Formulation

-  4    .    


 4 4  C    4  

   C    3    


J    
&     .  &
&
    &
&       " 
   4 n 
   

 3n F  
  "       4     
   .  
?4  4   m  . 4  
   .     
 "     1 + 2m F         4  
.  J K 1 L  &       I  4    K 2 L 4&
   .   K m L 
&  . G &         
H " 
   4 n 
   
 .  
 4  

 .       
 m  4  1 + 2m = 3n  #  & .  

 m  .      (3n1)/2    
       .
   . 4 n 

-  
       4  4     
 & -
D     (3n1)/2  . 4  
   .     
        .&   .  G 4       
     H  
 n 

+ n 5  0    D   & I           .
4    + n 5  1  4 4     
 "  

        G  (311)/2 = 1 H  4  4  4      
 .   .         
  4 2 > D  
 4   
-  
.&   D  & 
   4     
        4    "  4   (3n1)/2
   4  4 4   '    
   .  4   
 5        


  
    


  

/1 #+  5


 



       4    "   


     

  4 .&   .  .  '  


       

 n 


6.7.2

Problem Solution

> .
 .    
    .      
 . 

n    
The Basis - < 
 4   

&       /


  . (301)/2   "    n 5  0     
Induction Step ?4 4         n    < +
&    c.n   (3n1)/2  &   4
& 
    .
 .  '   . 
 c.n   
 n 
 - 
  4 4  A  .  .  ' 
 c.(n+1)   
 n+1


   A 
 8   
 
 .     .
  
          
      "     4 &
  .
  
   
 .     4  
  5
>   5  .   
          .
4  
.    .          . "  
4     
&  A  .  
    .     

 4     &        c.n 
  .
     "     c.n   
'


 .  
 4   .
   . 4 n 

8    4
&           C      F 
4 c.(n+1)  c.n  ?4
c.(n+1) = (3n+11)/2 = 3((3n1)/2) + 1 = 3 c.n + 1 .


c.(n+1) c.n = 2 c.n + 1 = 3n .

"     
I   
  &   .    4 4  
 G    
   4         4  
      c.(n+1)  4   4
 
H -  
     A 
 c.n + 1        .   4  
"  '    
 4    .   A 
 
 "  
     
 . 4  4  &    8.      

  
    


  



/ 
 

  .       


   c.n  .      "   

4    .           .      
    4        &  1  8   49 8.
        4          .       
   
 .
  4   . 4 3n   .
4  4 4      & 3n     c.n  -    
&     &     
 . 
"  
    
            8.     
   4 4                     
                       &
K &  L 4
   .    & K & L 4

  .   .   
 4 
       
   4&     
The Marked Coin Problem 8   4&              
   
4  4       F  
 "  4  

     
 .     . 4  
   K &
 L  K & &L %' &  .     .       
   
  4 
 4 
 n 
   . 

 3n
 
      & 8. n 5 0       4 
    .
 "   0 G n H 
    
   . 
+      4    .     
  4  
4 3n+1
  8   A 
 
       .  

       
  .      8   &    
&                
   5&J 3n

  .         3n     .    3n     
 
"   
  4 F 4&  4   
 4   
       
 -     
  .4
 l1  &          .    l2  &  
        
&  h1  & &       
.    h2  & &        
" 4 &   .
  
 4 5     
 . 
   .   5   
      "   l1+h1  l2+h2   
5 + 
  & 
  &   5 3n 
?4 .   
           . 4      
   .     & &               &
  
   4  
 4       l1  &   


  
    


  

/1 #+  5


 



   .      h2  & &           .  
G   & &   &   5 HI    h1+l2   
 .  & .             h1  & &
    .      l2  & &          
 l1+h2     . 
    &     &  4 5     
 .
  
  5  3n  4     
 .   
 " 

   5
   h1+l2 = l1+h2 = 3n  "  4 l1+h1 = l2+h2  4
.   l1 = l2  h1 = h2  -
           
5 
 .  .   
 
"  5
  
 
&           4   

    .              . 

 . 3n
 4&  4  4   

 "     4& 

     4&      .
4   I &  &  
   4 .  
4    


The Complete Solution "  
      
/   

      
/   
 "  .   A .
  
. 3n+1
  &   3n         4&      
5 
 .  &      .           
 .
  
  .   .4  ' 

 8.                    4   
.     

 8.         .          

    
 &       .       & &      
    4    & &     .       &  
       

 8.                    

    
 &               & &     .  
  4    & &             &   
   .  

"      
/   
4    
 .   (3n+11)/2
  .4
3        . < (3n1)/2  (3n1)/2 + 1  (3n1)/2 
    A     .     4         
                      3
  
 
 .   
     .4J

  
    


  

(

/ 
 

 8.                  &   


   
/   
G &H         

 8.         .           #     

  .   4   '  .       K & &L
#            K &  L &      

/   


   3n
 

 8.                     #    

   .   4   '  .       K &  L
#            K & &L &      

/   


   3n
 

-      4   


 .   
 ?     
  
     &       
  
  &  U   J  &   &
 &

  
.
    .
 "     
 .    . F  
Exercise 6.5
 &    
 .  D      D    

 4  4   '  .     "  D  4    F
4  8         D     B  5  

4     5  D  +    &   4   .  
 4 &    m    
 2m 
    .&
  5  D  4     
 .  D   3m  G=J .     
3m+1  D      3  . 3m  D H
 & .& 4     5  D             
 D 2
2
Exercise 6.6
,  n  D  4  1 n   . F 4    .
            
 . & 4 .    D  4  
     4     
H =4
& 
    A      D 2
H  4 &    n         
 4       
4       D   2n 3 
 
   2 n 
H     4  D  4 4  A  B  C  D    A < B
 C < D   4 4  A       .  . 4 4 

 ;     4 4  A       . 4  D  
4 
 G  5    &    G HH


  
    


  

/2 & (

)

H     2m  D  4  1 m   4 &    m    


    A        D   3m 2 
 G=J

  . G HH


2

6.8

Summary

8    .  


 
  
/   "    .

 
         .   
.  & K<L   
.  K<L .
(a)   . K<L 0   
(b)  
  .    . K<L n  .  & n      
  
      . K<L n+1 

;  


  .  "   
&  
 
4 4      D      .
 
    "  &
         4  


 &  . 
 &  

 
     A  
  

6.9

Bibliographic Notes

"      ./   


  
 . 4  & % -
3D M3D$ 3D$N


  
    


  

*


  
    

/ 
 


  

Chapter 7
The Towers of Hanoi
"        "4 . =  
 "   
   
&

 
  '   .   
     A    
  . K L    
/ &
"  "4 . =  
  <<    5     4  
&
  
/ & 8    &
 4& .  
 A  =4             &
    .      8   4&      
 
 .    5   J  &     
    .   5 
+    
 4  4    .    
 >  . 
  
  4  4  I   "4 . =  
     
   I     .  . &   .  
    4             
 . .  .
     
   

7.1
7.1.1

Specification and Solution


The End of the World!

"  "4 . =  



 .
 <<
  !! &   +
S

 
  %
6     &
# 
"  <<         4      
  

4         A'     >   A .      

 .   41  ,   '&/.    . F <
    . < G A H "  

 4     .

      & .


             

&      
  " 
1  4  
 4   &

  
    




  

!

1  6 % 0

   
     .
  A .          
  &    & 
      4 4 
   9

+ J "4 . =  

7.1.2

Iterative Solution

"    & &     "4 . =  


   &  


 &  '  " .
    4 
        
     .   #
 .          &   
    4    4

 - 
      
 

    .
     '    4   -  
   &
 
 .
0 4 > & 0            
  
 
     & 1  -   
   
   .4
> &  &     A &   
  
 " 
 .
   
       

    " 
  .       
 .  8.    
 .
     
    &     4   > 4 
  &      4  

  
    


  

1  
  &  

$

> &   &         


  
 C  D    
    
&      
  8  &      
.       ' & 
            
 
"  

 4   . 
         /

 & 4        // /
 
"& '    
&.  &  4 / << "   &   

     /

    &          
 .   
   .    /

 8. &  & 4 A
    
4 3  4
   &  &  ' 
  
     
 C 5 / 6 /   

7.1.3

WHY?

    
         4   /
 4       8. &   &   
 C
 4  8 
9C       4& # 4 
  4
. 4 4      .

 
  A  .     .  

 "    4 4    9 8 4 A    
 .    
 "  &    
 .  4  4 4 
   
  .
    

7.2

Inductive Solution

    &      


 .      &
6   4  
   
/
      
    
 M  .
  A      A  6 
              G6 4    
    
 H
 .      & -    
 .   0   
  
    +      4 
   4 
 n
 .
       
   4 4 
 n+1  .
  
= 4    9 "   &   . 4&       & 
       4 J
 #    n  .
   .    4  '   /
 .      &    n   4    
4   &   
  .
  

  
    


  



1  6 % 0


 #   
  .
   " 
   
 n  .
 
 >   4  '     .      & 
  n   4     4   &   
 
.
  

" 
 4 
      A       &  " 
4&     &    
 
 
    
A  .   
    4
          
    G  
 &H 4        
'       &

& "  


&

&    4            .  5 


 4   A  G"   &

&     &       


   . H "   
   4  4      
     .   
"  
  .        &   
  
   
&     .

 "       .& 4 
     
 4  & & 4      
  4 
  4 .
       .   "4 . =
 
 
 4 
 n  .
     '      d 
4  d     4    4 "   . 
    
     . 4  
     A  .  
 "  

 
       &  
?4 4          -      .
/
     &  8      
&     
 A 
   &  "        
 A  
    
 ' & M      
 -        & 

n       . Mn    8. 
 .   Mn   

    n  
   5
     
&     
 .  
 
&   -      & 
"    &  4         
   n  
.
           d   .
&  
 G       4         & 
         .   
   .H
8      n  0    5  .
    
& 5  8    .
n+1  4 
   4  
  .
   n 
  .

    .  4   -
  4 4 
 n+1  .

        d  4  d     4    4 +
  4 
       
 .
1 4 4   

   
 1 

  
    


  

1  
  &  



,    . '     &        . 4  


 -   &
   n 
       d       
d  &      .
 4      .     & 

 .     G. &  &   & 
 .    
H 
      
   n 
       d  "   n+1
 
      d. G"   
&    n+1   .  
 =4  
       n  
    n+1   
    4   n+1 
H +& 4      &  

   n 
       d  "     
   n+1 
  n+1 
   4 
 .
      
      d 
"  .4  

          


 "  
A Hn.d    5  .  k , d   4  n    
 .  k 
  
  d  d     3  
 .
1 4
 1    
 3     true    4


  false  /  4

 "   k , d  

  
 
 k .
        d   "  
  
  5    [ ]   
& 5   [x]   5 
4 ' &  
 x  "      .
.      

5  Hn.d    4 
   n 
  / &/ .
 
         d  .4    .       
 .  
 
H0.d = [ ]
Hn+1.d = Hn . d ; [n+1 , d] ; Hn . d

?       


 H         .   5 . Hn+1.d 
  .   4  4     
       
  
  & 4.  
/  5     .    
  "  .
.      
I        
K L 
    
     .  
"         4&         "4 .
=  
. &   . n C 4 
&      .// 
4       . H   
 + '
   4
4 
 H2.cw  G-  cw  aw 
  4    4  
  true  false    
  &H
H2.cw
=

 5 n,d := 1,cw


  
    


  



1  6 % 0


H1.aw ; [2,cw] ; H1.aw
=

 5 n,d := 0,aw

H0.cw ; [1,aw] ; H0.cw ; [2,cw] ; H0.cw ; [1,aw] ; H0.cw


=

 5

[ ] ; [1,aw] ; [ ] ; [2,cw] ; [ ] ; [1,aw] ; [ ]


=

  . 5 

[1,aw , 2,cw , 1,aw] .

  '  &   


 H3.aw    
 4& 8. &  & 4
5 &              
   . F  
    "  
 ' .   5     . n = 3  16
     . n = 4  32     "      &     


  9 "              
   
 "          & G &H ' 
 
    &      "  

 .  
         
    .   5  .

 &   '  " 



& . 

  &    
   9
Exercise 7.1 "  
 . &  
   
       
.   5  H64.cw  6 Tn.d     .   5  Hn.d  3
   A . T .
    A . H  GB   A  
Tn.d   . d H ;   A   To  T1  T2  =  
 4 .
  D  ' Tn   
 .  . n  
& D  &    n 
2

;      .


 .   
 . F  
Exercise 7.2
  "4 . =  

;     4 4     / 
   4 
   . n         4
 4 
;       4        
   
 . 

   

2


  
    


  

1    &  

7.3



The Iterative Solution

          


       8  4


J   A      
  &      G     
.

  &  4  &   4H       
    
  4   
   
    8  
  4  4 4      .
    
Cyclic Movement of the Disks

8     4  4     
  4& &      8 .  4

     -  4       &       4  
    .

 .  
"  &    .   k , d      5  Hn+1.d    
even.k d    G   4&   4& .H "    
 /
5  .    .       )) -    .
 .
Hn+1.d     
 K n+1 L    & K n L  K d L    &
K d L   even.(n+1) (even.n)     . even.(n+1) d 
 
   

-   even.k d     . G.   k , d      5  Hn+1.d H
4       . n  d  6      N  D  " 
. 
 k , d  4 
even.k d even.N D .

"  .
 4   
     .

 d .  k   A &
.   5 
   
 .     4    /

    &     4     /
   
&      4   @ / .   5 
   
 .
    4    /
    &      4
    /
    &     4   8  
  
  G4   /
H   &        D .
N      
    D . N  
Exercise 7.3  '       
 
  4     &
   
 .
       
 .   &  
 
 
 4&  
          4      
"   4           .      41
 4    4  4  4    4 =4    & . 
  4       

   
  .
 
 


  
    


  

(

1  6 % 0

    
  >   ' &  4  

   .
 

    .
  3    
    
   

2
2
Alternate Disks

- 4      


D 
 .    
&        

  4   


   
   
& '
   << .           
    
.  4  
 .   
   4.   &  4& 

    4  
 .         
  
F      .   << - 4 4    .
 .     5 
Hn.d A   &
6    5  . 
    .   4  "  A
&      
  4       
I     &    .   5   /
&      
4     - 4 alt.ks .   5  ks    4 
"  5  . 
    & 4  4  & diskn.d  
  &    A 
 .  .     Hn.d    
  6   5          4&   & diskn.d  " 
.
  A . H 4 J
disk0.d = [ ]
diskn+1.d = diskn . d ; [n+1] ; diskn . d .

>     alt.(diskn.d)  "  .  &    n  "   
n = 0   &     
& 5      
 +  
     & .  5   4    .    
.  5  ks  
 k 
alt.(ks ; [k] ; ks)

alt.ks ((ks = [ ]) (k = 1)) .

"  .   J


alt.(diskn+1.d)
=

A

alt . (diskn . d ; [n+1] ; diskn . d)

  & .  5 


  
    


  

1 & (

)

alt.(diskn . d) ((diskn . d = [ ]) (n+1 = 1))


=

  &      A D 


 .4 & . diskn .    

true .

"  ' .          & 
Exercise 7.4
>       
    .   8   
 .  
 
 
  & .   '1 A    
    
 .  &     
 "   .   
4     
       & 4  
 
4   I  &     / 9
+&   ' 4      
 4       
     // /
  4    &     4   

   & .   41  " & 4           . 
-  4   
  '    
2 & 4 &   

   G=J "   


 4   4   
 

    B &      4      
   

   .  
1
   4    #  .  
.         4  n       
 

    
even.n d even.k d 

.     d 


 &  k 4
 n       d H
2

      4   


Exercise 7.5 (Coloured Disks)
"   .   
I F 
&   F&
3  
  4                
   4                     B

& 
   &      
2

7.4

Summary

8    4   4             "4 .


=  
          "   

  
    


  

*

1  6 % 0

4      .      


 .   
    n /  
 "   4   D.&     
   
. "   4   .
      
. .  .   5  .

 .        
    .    
"      4 
  J    
.   0 /  
    .     G      1 /  
H
     .  &  &  
     .   
  .

 .    G  4    4H 

7.5

Bibliographic Remarks

8.
    & .   "4 . =  
  .
M$N  .
.     .     4     M6!N "  .
 
.      M+N


  
    


  

Chapter 8
The Torch Problem
8    4     
   .     

'  ( "        & 
 . I    
             
 

 A &    
4      .4
N  4     8       &   
 4        & &    4  

"    4  
 2       &  

"    
 .
1   N.  i  
 t.i  
  I 4  4     &
       .  
4

   
  4   N         

+ 
 & 4 
   t.i < t.j 4  i < j  G" 
   4 

         
       
 
  
      
   
   
 
 
   8.    
     t.i = t.j . 
 i  j  4  i < j  4
 4&   (t.i , i)  4  i     '   &

    K
L     4       
 4
   &H

8.1

Lower and Upper Bounds

"     .4  5   &    &  
/
    A 
 4   5 
 81 
    4 
 &      "     4   F  4    
K L   K4 L     


  
    




  

!

2  
 

8     
        .  4  

. 1
 2
 5
  10
  &   5 
4   4    .        17
 8   4  
5  .  /        
  8    
    & '    5  .       5 


   
   4   17
         

  4      4 
  4     
   

-     
   .     
 .     F 
4 4    # .  4  . 4     

  4 A '     4    .   


    
           
 . 2+1+5+1+10   19

& '      5  4       19
   
     
I 4           4  G8 
 H 
& & '      5      .    
17
        
    3  
 
  D     5 
8            
.   N   
   "  
4   5 
    4  &   
      
  
 "   F    4
    

&     4      
 " 

 . 5 4       
I    4 

 &  


 
8   !* 4  4 
 .    
 5  " 

   



  4 . 

    &
 
    

8.2

Outline Strategy

>    
  4    
      .  & 
"   
 .  " 
.       
 
 . .

     4      8


&  . '
     

       
&      / &/      

  4    .   '  8.  
  5  
4   4           '
& F  "  
 .
F   (N1)!  4    &  
  . 5 
 
. N 
"  4&    &    .   4  4    K.4 L

  
    


  

2  7   &(

$

    4        


 4&   " 

    4 K.4L KL  4      
                    
8.
&   .4     4 4          .4 
"      . 4  
   
   . .4    
   5   
 &   &  
8         F   4    
 .
&
+ &   K  L . .4  4
  K L .  .  " 

 
   .  K L G K
L    

 H  
  
  4     A & & 4         
 .  
    A &   
 . 
         + '
 
 . 
  4   A & & 4
& 
     
 4
& 
     - 4 4 . '
 {1a , 2b , 0c} 
   . a  b   c   4  a     b   4   c  
 
 + & 4  4 {1a , 2b}     
 
8  
        F .
 5  %   4 
4 4 4  '    4  .      
   
 G      {1a , 2b , 0c}  . '
H      A  
"  ' {1a , 2b , 0c}  {2b , 1a , 0c}     
 
    &    .        . '
 {1,3}  
  4   1  3  8. 4      4 .4
   4 A'    4   K + L G. .4H  K L G. H
 +{1,3}   .4 
 &  1  3  {2}   

 &  2 
 4     .  4   
    . .4   

 5  .  -  &     
 .  . 5 
.    4     
-   5  .     &       4    
  " 
 - 4 &     5       5 
.   
  &   A  
   
  .     ?    
 
   E' G&  5   
 .H   G.
 5  a  
  5  b   5  b  
 
5  c    5  a  
  5  c H "   
  A
  5     
   5 
+
&    " 
   . 
  4   .4 4 /
J

 "    I      4 


    
   
       5  G4  
  .
1 H


  
    


  

 

2  
 

 >/
     5    .4 I /
 
    "   .   5   

 "  
 &     4 .4  
    4 .4  G  T 
 &  i .
iT H

 5  .   A 4  &      A 



 5  J

 "  
 . .4   
     
 .  
 "  
 . .4 
 &     
    

 .  
 &   

     
  .4           
  
      
        " 
 
 5     & .  .4   
"  A  G

 !H    4   &  5    


 & 
 4      "  A   .     .

 "  
 . .4  

N1    
 .    N2 
G    N    
 . H

 "  
  &   5     4 & 4  .4

 
I       4   & 
    4
G74    . .4    &  
 4
& 
 

    "      
  .4 
   .4  8   4&   
  .      
 H

 # 
& 4 D    . .4     5  

         5      5    



  
 4         
. A  
 5  .
.4    &    
. A  
  . .4 

+  
  . .4       & .   4  
 
    -      &     
 
  4 4     -        
  

 

  
    


  

2   &' 




8.3

 

Regular Sequences

     KL 5    5   4   .4   4


        -  4&    
 5    .   .4 


Lemma 8.1 %&  5       
 &   &
.  5  4   
Proof     5     -  4 J
  A    .4    A     4
8.   A     4    &  p &
  
 8.&   .4 
 & p      4   
 p
.
  # .
&    5     .

u +{p,q} v {p,r} w

4  q  r   u  v  w   5   p   4   v 
G?     .4 
 & p  4      
  
  A     4H     5  &
u +{q} v {r} w

"      5    


 . 4         
5  G"       5  
  4         

 &      4 .4   "   
.      p       4   &  
  
    . p     
 & p   &   
 .
  .4    "  
       . & x 
y  t.p x + t.p y x+y H "  
 .         
4  .4          
 . / 
 
?4    A    .4 "   4   J  
     & A    5        & A
8.   A     5     
      
  

&  G- 


   N    2 H "  4  
 
 &   
      
       

 . /   
8.   A     .4      & A    
  p 4     q     4   

& .

  
    


  

2  
 

  .4  G"   &      .   


   p 1
.4     A  H "      5     .

u {q} +{p} v

         q  s     p  q. " 
 4 J    .4       
8.    .4  
  q   p  4 p 4 q     

 q 1    p 1   "   .

u +{q,r} w {q} +{p} v

G4  w    p  q H 


u +{p,r} w v .

"      5  #     


    G  .
& x  t.q x + t.q + t.p > t.p x H    
 . /    
8.       
 &   & G"     4 

  p  s .4     A      5 H "  
 
p  4 p 4 q       
 q 1    p 1 
 "   .

u {p} w {q} +{p} v

G4  w    p  q H 


u {q} w v .

"      5  #     


    G 
t.p + t.q + t.p > t.q H    
 . /    
-  4    4  .
  5        /
 I   .
    F  .  &      
 
     4          .  

 4   5       
     5  
  
 / 
2

8.4

Sequencing Forward Trips

6

 !    A   + 


     
 . .4
   
 5   N1    
 .    N2  "  
  &  5  
  .4  .4 &     

  
    


  

2 &' 
 #6  

 

  
 .      &           

& 4 "  .   A 2(N2)  N2     2  I
 . 2(N2) + 1  &  
  
      
   
 &  5   
 . &    . .4     5   4I      
4     
   "        . .4    
 
 4
& 
  
  .4  =    

. 
      
 .
4     
 .  
    

+ '
    .4     5    .4J
3{1,2} , 1{1,6} , 1{3,5} , 1{3,4} , 1{7,8}

G"     & 

I     3{1,2}


    1  2

 3 .4    1{1,6}


    1  6
  .4
     ?        .     4    .4
      5 H "     
 .    .  
     1
 4 .4     3  I 
& 
2
 3 .4     2   4   3
 2 .4
    1   "  
  G 4  5  6  7  8 H 
 1
.4        "   
    J
3 (t.1t.2) + 1 (t.1t.6) + 1 (t.3t.5) + 1 (t.3t.4) + 1 (t.7t.8)
+

3 t.1 + 2 t.2 + 1 t.3

G"       


  &   .4     
   

  &    H ?      
 . .4   7 G
     
 . H     
 .    6 
"    
 & . 

 !     D    . .4


     5           5 
  &   4   
   . .4 
"    /  & .   .4           
 4  
 
    
 F    . .4    
  5  " 
 F     .   4 ' & 4 
     

 & "  


 .     F   4  4 .& 4 

    1   N    
    
       " 

 . 
        
 . .4 
 &   
- 4      .  &
 &  .4 I 4   
  .  &

    .4  3 .     4

  
    


  

 (

2  
 

&      F       &     


 .
     8.        4 &      I . 
.             
 4 &     # I A& . 
  
 4 &      
?4  4  Vnomad  Vhard  Vfirm  Vsoft     

. 
   
 .     
 . A
    
 . .
  &      F  "    
 .   F 
Vhard + Vfirm + Vsoft .
"  
 .   5    
 . .4 
 & 

    
 . 
    

 
 .4   
     .    2    
 . .4 
 &

   A
   1    
 .     
2 Vsoft + Vfirm Vnomad .

   
 . .4   
     
 .   "  
Vhard + Vfirm + Vsoft = 2 Vsoft + Vfirm Vnomad + 1 .
%5&
Vhard + Vnomad = Vsoft + 1 .
- 

       .4 A


Definition 8.2 (Regular Bag)
 N    2    .   .
{i | 1 i N}       N  .     .4 J

 % 
 . F  < 4 G8.
&    F  4 H

i : 1 i N : T : T F : iT 
G8.
& &    
 .      F H

 8. Vhard.F  Vsoft.F  Vnomad.F   &   


 .  
F    4    
 .   F      
  
 . 
  F   

G!H

Vhard.F + Vnomad.F = Vsoft.F + 1 .

+
& 4  4      .4

  
    


  

2 &' 
 #6  

 )

Lemma 8.4 ,    5  .    N    4  N
   2     . .4  F      .
  5  & .
  
 .       N  #   
  &   5 
.     .
F  6 VFT   
 & . T  F. "   

  

G!)H

T : T F : i : iT : t.i VFT  + i :: t.i rF.i

4 
G!*H

rF.i = T : T F iT : VFT  1 .

G rF.i    
 . 
    i H
2

 5  . G!H J


G!H

Vnomad.F = 0 Vhard.F = 1 Vfirm.F = 0 = Vsoft.F .

8. Vnomad.F  <     


    & A  .  A

  & G!H Vhard.F  1 


G!!H

Vnomad.F = 1 Vhard.F = 0 = Vsoft.F .

8.    &  


       4 
 "   Vsoft.F
 < 8 .4 .
G!H   Vhard.F  5 < "    


.
G!H
G!$H

N > 2 Vhard.F 1 Vsoft.F 1 .

8. N G  
 . H     2          
Vnomad.F    1  8 .4 .
G!H   Vsoft.F    1 
?4 4   4 4      5  .
F 
Lemma 8.10 ,  N G  2 H    N     5  .
     .
F   N    "  
  &   5 
  & G!)H
Proof "  .  &      < .    F 
-       "     4  F  . ' & 
 G4
 & H "  5     D    
"       & 8       Vnomad.F = 1  8    &
  F  A
 "        .
{n,s} 4  n    
  s  
 "  5   
&   &     
 . F    &


  
    


  

 *

2  
 

      &   


 n  4   . .4 
. 4    A     {n,s} . 
 s 
"        Vhard.F  /<  F 
     8  
 & G!$H Vsoft.F    1  8 .4 & A . soft   Vnomad.F
   2   & .   F     {n,m} 4  n  m  

     5  4   4    {n,m}   .4 &
   . n      &         . m     

 & .          .   F & 1  G"   

    .  .
F H -   4  F   4    
 . 

 &  . n  m     & 1    


 .   
  & 2  &        5    F  4  
  
   
2
Optimisation Problem 6

 !(  !   A  


   4 
     .     
 8 .   5  . 
. 
  4      A  A !   
  

  & G!)H 8     
  4 4 
8     
   .    
 
& 4   
  
    & G!)H "   4 

    .
 "   .
  A 

 4  F 1         .     


F 1       ,   F    T  F  4  i : iT : t.i VFT
     T  F  G

 K F L  
 .     .
 
'H +   i  4     . t.i rF.i         i
G  i 1  
H

8.5

Choosing Settlers and Nomads

"       4     


 -       
   4     
    . #  A & 4  
    
     
 2 
  1  4&  
 .
N     2 I &  2
&    

Lemma 8.11
%&     
 &    . 4   
 4    

Proof     N  F      .  (p, q)    
. 4  F  p    q   
  p  .   q 


  
    


  

2   &  .

 

 &  (p, q)   q  p    &4  
F  -    N  #    
  &   &  
t.q t.p 
"  .4 
 .      p  q  .    " 
.4 
 .    &  q     G   t.p < t.q H
"  .4 
 .     &  p    &  
  
 
 t.q t.p  "   A &  4  "  A   4   
  p  {p,q}  8    4 p  q   F      
     
   0  8          p  {p,r} 4 
r = q  8           
t.p t.r + (t.q t.p)
=

 & . 

' 


t.q (t.r + (t.q t.p))

t.p t.q 
 & .
'

t.q t.r .

+&   
 .    .4    
"   F        
      "     .

  
        
 .     &  
 "  &      . .&  
    F
      
    
2
Corollary 8.12
%&  N    
 &   N  F 4  
.4 J

 8 & A
  F    
   1 
 %& .   F  {1,2}  G?J  
 & .         
  & 
   0 H

 " 
 & . {1,2}  F  j  . 
 j 4  1 j N2     

  {k: 0 k < j1: {N 2k , N 2k 1}}  G?        


&
 4  j 5 1 H

Proof  F    N    


     
 +
!
4
& 
     
  4     
     A
  F  4    
   i 4  i   1 
  i      .    &  1  "    F     .4


  
    


  

 !

2  
 


   i  4            =4    

    G & t.i t.1 H - 
        N  4 
  F  
 G  .  

 !( .    5 . 


 H
>.    < .    F    "  
 . 
 &
i   &   
    i   
  F  "  
 .

 &       
        .  1 
    
    & G!H 8.  i    
 .  
 
 & G!H 
     & G  A
 .H . 
 
   =4  i
&  
   "     

. 

&    &    
 8.   i   
 . 4
  F. "         A
  F   
     
  .   F   
 A
 8     &      G!H 


?4      .   F F .
{1,2}   
 
   
    4        & {1,2}      4   &

   
   F  

-
& 4   .
G!H         .    
 &
. {1,2}  F  j  . 
 j 4  j    2      j1   - 
    .       A
    4   4  1
   & 
  A
      
   I  .4  
 1    & 
  F  .
G!H         " 
 1  2    
 .  GA
H   F 
8 
   4   4  j    2      .
  
{k: 0 k < j1: {N 2k , N 2k 1}} .

G" 
 & .     1   4       4 
 H

     
 . .   j 4  j    4 "    
  3   N   2(j1) .  
 
 .     

N 2(j2)  
 . A
 &   & 
  
& 
 .     4    4         
     A   . 4    
 
 "   
&      
 .      
2


  
    


  

2/ 

8.6

 

 $

The Algorithm

-  4     



Lemma 8.13
 F   
  .&     
& !  "  . j   
 & . {1,2}  F     
  & F 

G!(H HF.j + FF.j + j t.2 + (Nj1) t.1 + (j1) t.2 ,


4 
HF.j = i : 0 i < j1 : t.(N2i)

 

FF.j = i : 3 i N 2(j1) : t.i .

G"  A   


    .4 
     4 
    

H
Proof "   4    8.     .     . j  1 
8       
  
i : 2 i N : t.i + (N2) t.1 .


HF.1 + FF.1 + 1 t.2 + (N11) t.1 + (11) t.2
=

A . HF  FF  




0 + i : 3 i N : t.i + t.2 + (N2) t.1


=




i : 2 i N : t.i + (N2) t.1 .

8.    .     . j  5    


 . .    
 2  8    HF.j    .4 
 .      F  FF.j   
.4 
 .   A
  F   j t.2    .4 
 .   j .
 +&  1 1  
  (Nj1) t.1   2 1  
 
(j1) t.2  G  2   
 . j .4    1   
 .
j + (N 2(j1) 3 + 1) .4  ?     
. j1  Nj1  N2 
4   4  4 '    
 .    H
2
+  j  4  j    2  A OT .j     
 
  &  
N  4   
 & . {1,2}      j  "   OT .j   & G!(H
?4 4    

  
    


  



2  
 
HF.(j+1) HF.j = t.(N2j+2) ,


FF.(j+1) FF.j = (t.(N2j+2) + t.(N2j+1)) .

  5 
OT .(j+1) OT .j = t.(N2j+1) + 2 t.2 t.1 .

?  
OT .(j+1) OT .j OT .(k+1) OT .k
=

 

t.(N2j+1) + 2 t.2 t.1 t.(N2k+1) + 2 t.2 t.1


=




t.(N2k+1) t.(N2j+1)
=

t   

j k .

"   OT .(j+1) OT .j 


   j 
    5      



 . OT .j   
 &   .     4    F  . 
 .
    
"  
 4&      
&     5   
 N            4 j   1.  
    2 t.2 t.1 + t.(N2j+1)  .
 8.    true   
.  {1,2}   I    .4 &    .  1      
 {N2j+2 , N2j+1}       .  2  8.      .
  
 N2j+2        N2j+1 A
 8 
     
 &  1 I  4 4    1

-    
 N     
 .      &  &
  
   
  . j  "      .4
{

2N }

i,j := 1 , N2 ;
{ Invariant:
1 i j N2

k : 1 k < i : OT .(k+1) OT .k


  
    


  

21 
  


k : j k < N2 : OT .(k+1) OT .k }

do i < j

m := (i+j)2 ;
if

2 t.2 t.1 + t.(N2m+1) i := m

2 t.2 t.1 + t.(N2m+1) j := m

fi
od
{

1 j N2

k : 1 k < j : OT .(k+1) OT .k

k : j k < N2 : OT .(k+1) OT .k }

> 
 j   
 & . {1,2}   
    OT .j 
  5 
 
 "  j1    
 .   & !  /
A   
 .      

 !  A 4  &   


"  
 N2j+2          N2j+1 A
 
    

8.7

Conclusion

8    4    


      
.   /
& 
 .    &   
 "    
 
  
.        4        

          


D       

4  
      " 
.    .     
. .4  @  
 . 

 4     


 .  . 


 . .4  4            5 

#& .    4      
 5   KL
C     .4  
 & 4      
 &
I   KL G   4  
   H    4   
K
L G   4  
  H    . 6     
   
 4 
    
 . K L  G
 & 4 H
       
 .      4 . 
   "  .
.   .       
 4 
 
  & & &   . 
    . .4 I . 4         5  .   
&    &      
%     
& 
    A 
G
 H    K L   
      .  5

  
    


  



2  
 

C         


    
 K L  G  
.   
& &      H   4&    
4& 4 . 
   
   K L

8.8

Bibliographic Remarks

8 
 /%      
    KE    
LI    
/

    K   
L "          
  &/
/   
 4     & .      

M N 8 
 .  
   
          & 2
 
I     &   
     &  M N
        
 . K
 L      "  F/
   A  %  .  K
 L   4      D
  . 4  " 
 F   

 !I       &


       5  .  =  
    
     &        .      

 
      /
  . 
   
  
1   &   G &           =
& K  
  . . .   
 & &      2t2 t1 
    . ti 1L  .  2t2 t1
         C   
1  2  5  10
  
  '
9H
   
    &         4 
  
    -     & .       
  
 

    #& K L  .  
  
   . + '
           
   




 .  G8. N = 4     & .     3    

  1  1  4  4  4      
  8  4      5 
"    
  3   9 H "   . K&L .   
    4&   4 . 
 .4     K.L G   
    .  & .      H 1 .
 .   
 
 . K
 L            . 
4  


  
    


  

Chapter 9
Knights Circuit
"   
         &   B &  & /

    
 
 4 F       
 

 
"   
  A  7 1

 .    "   A  5  .

   4   7          5 .    
 5 ' &        5  4      
"   
    .    
I        & 
&
  '  '
 .       7   .4   /
 C  /   
  =4    64 5    I
 
 64
      .  5 +
 .   
5       . D 2
  .
 .   16  5  
    . 8
 G A $HI .
  
 5   4  6
 
  "   
 
 .            .4 6
.    &    
 4  
 4   &   5/

      .


       & 5 ' &      

  
 "  7 1    
    .     
 .
    4     .  
     "  5         & 
 
   7 1
  
&   A &  
  G4 5
<&   &   5      H "    
 .   4  4
   '

9.1

Straight-Move Circuits

+  7 1            


 '  .  

    
  


  
    




  

(

8 , 9  


+ $J 7 1 #


61     
    & 4 
    .  /
  &  4     .
   &  4 
 - 
 .
 4 & 2
"    A 4   4
 .
& 5  &   5 8
   1 4&         . &  4   < C
 .
& 5  
   5     &   
   5  ' I        5     D 
& 61   .
    .

"  

   5 <&   & G"    
  
 7 
  '  
H -   
 

8    
    .    D 4  
2
"  4  &    1 

&   B


&     A 
 /
   &       &  A 
 &
 &
  .         
   
I        
 
  88       &    


  &  5 &    .       4     

   /
   .    2 "         &
5 ' &         
 5
 K L

  2
8    
 .
& 4    
      .4 ' 
8   &  .4
Exercise 9.1
(a) -      4   
 .
   
    .
      
 . 52 ; & 4   4     
 
 
    .    .       G=J   


  
    


  

8  & +:  


)

  
  .
 5   F  5 > 4  
4        .
    4H
(b) + 4   . m      
   /
   .  
. < 2m1 2 G 2m1    
 . 5I   
 . 5
 2m H
(c)  4        
   /
   .  2n  . 
GH  . n  G 2n   4 4  4  n 5H
2

"    . '  $      /


    &   .  
  < 2mn  .  
 m  n  "       2m
       n  G m  n
  /<      


   '  .     5H    /
   
4&  
 4      < 2n  .  n  "    
4 4 &      /
   .  2mn  . m   
 n     &    m    2n      .  
   
" 
       4      . < 2m n 
4  m     1       .   4  m  
  1   2m n      4  . < 2p n  2q n  &
4   p  q  
   m  p+q 5 m  -
&    
  &      /
   .       -
D   
   4  
"  &    
     5 "   4  
 .

 .    5  &  /


  
    8  

    <
 ?4 
    2m n     
2p n    2q n  4   .
      G"  
4       A 
    
 . 4         
. 
 .   H "  
/.  .   2p n   

&
    /.  .   2q n     /
   .  
4  + $  4    

 & "  4 < 


   
/. .   
    <
   4 4

.
 .   4   "          .     G>.
     .         .     .      
  H
?4  
      .
   .        
<
 .
  
/.  /.  &  
   4
&        A $ "  
    

  
    


  

*

8 , 9  

n

2p

2m

2q

+ $ J 
  /
   +      4 

     /
   .  
n

2p

2m

2q

+ $J   


   4     4
+ $(  4             4& .  6 8  %./
. &    .     
   /
   .  
2 8  "       
  
&   <
 &
    
  4  A $(
Exercise 9.2

   4       
 . 5 
    
  33  8  &      /
   .   5
  
 5 G A $)H 8          /

  .   5   .    5 =4   /
  
.    .     . 5 C  5 D     5 .
'
  
/. 5C    
%' 4       4            /

  .    5      .  <


  
    


  

8  & ' 



+ $(J   /


   .  6 8 

+ $)J  /


   G 4  H .  3 3  
  .  
5
2

9.2

Supersquares

6  4     7 1/    


 "  &      '
4  4 4    
 "  4&       
     8 8
      4 4  &    2 2 5  K/
5L   4  A $*
8.       7 1
   A  4 &J +  

    K L 4      5I     7 1

  .    .
 5    5    &
   4  <&    .       ,  

      4      5I 
   .    

  
    


  

!

8 , 9  


+ $*J

     4 4  . 5

7  .
 5      .        
5 8 A $    .   5    &   
I     .   7    4       
    
     
      

0110 1
0
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
11111111111111
00000000000000
1010 1
0
0
1
0
1
0
1
0
1
0
1
1010 1
0
0
1
0
1
11111111111111
00000000000000
00
11
0
1
0
1
0
1
00
11
0
1
0
1
0
1
0
1
11111111111111
00000000000000
00
11
1010 1
0
0
1
0
1
0
1
0
1
0
1
1010 1
0
0
1
0
1
11111111111111
00000000000000
0
1
0
1
0
1
1010 1
0
0
1
0
0 1
1
0 1
0
1

+ $J   GH  3 GH 7 1 # +

 
 G H  .   5    &    
+      
 4 4
      + $!  4
   
 .
 5 C  
/. 5C  & /
4  <&  4 "      
    

+ '
 .
  
/.  5   
  
    /.
 5     
/   5
>     .    @ 
 E       
' 4  <
 E  
  < ' G"   

    / /&4  / / 
I   <

    /'//  4 4   &4/'// 4H

  
    


  

8  & ' 

1111
00
00
0011
11
00
0011
11
0011
00
00
11
0011
11
0011
0011
00
0011
11
0011
0011
00

$

+ $!J  


 .
  
/. 5
6   & v    . E   
 .  2 2 5 G v   
. K LH 
&    & h G  . K <LH    .
E   4 .   5 ?4   A' 
     
.    . '
 v ; h     . A E   

   E   4 .   5 +   
    E  
4    
  E   4      
 "  
v;h = h;v .

  5     2 2 5   1800      
  c G  . K LH   
 "   & A . c 
G$H

v;h = c = h;v .

-  4 A      2 2 5 "    . 
4     /   %4  4   skip  
  
 = .  & 4  n G  . K LH + 4 
 &  4  <&   4    1800      

     "  I
G$(H

v;v = h;h = c;c = n .

    .  . &     


     
G$)H

n;x = x;n = x .

"    5 G$H G$(H  G$)H   4   .      
.      G      x .4 & 4 
y  z      
   A x .4 & y      4
z C  &
 x ; (y ; z) = (x ; y) ; z H 4   
A  . & 5  .  
    + '


  
    


  

(

8 , 9  

v;c;h
=

v;h = c

v;v;h;h
=

v;v = h;h = n

n ; x = x  4 x := n

n;n
=

n .

8 4 E  &      1800        
E <&    
     G? 4  &   
/
 & 4   A     "   .  A'  . K.4
&L .    /
    5H
Exercise 9.6
   4/
      4   F  . ' 
4  x  y   "       . 4  . 

   &  . n  v  h  c  G;    &    . E 5 
    H
;      .&   . x  y     {n,v,h,c} 

G$H

x;y = y;x .

     . x  y     {n,v,h,c} 


G$!H

x ; (y ; z) = (x ; y) ; z .

G8   &    43   64  F 


 "  . 4& 
    
 . 4H
2
Exercise 9.9 "4           2 2 5   
        90      4         4
8.&  
  .    2 2 5  '   )    
 4   F  . 
 &  . 
2


  
    


  

8      

9.3

(

Partitioning the Board

"  A  .   .   5   A   4
   7 1/    
   .   5    K n L
"    
 A. 5   5&    n  v  h  c
5           5 + $$  4
4      4      
/. 5   K n L 5 " 


&      K v L 5      .    K h L 5  &


D      K c L 5 5 .  4&       
. 
   
  5   64 5 .        . D  8
A $$   F   & A &    .   5 "4 5
   
  5  &     .
   &   7 1

 G"   4 5 .   


 
    .
   &
  7 1
  4 5 . F 
    .

   &   7 1


H

11
00
000
111
00
11
000
111
00
11
00
11
000
111
000
111
00
11
111
00
11
111
111
111
000
000
00
11
00
11
000
000
v 11
c 11
v11
c
11
111
111
111
111
00
000
00
000
00
00
000
000
00
11
000
111
00
11
000
111
00
11
00
11
000
111
000
111
n 11
h 11
n11
h
00
11
000
111
00
000
111
00
00
000
111
000
111
00
11
00
11
00
11
000
111
00
11
000
111
000
111
000
111
11
111
11
111
111
111
00
000
00
000
00
11
00
11
000
000
00
11
00
11
00
11
000
111
00
11
000
111
000
111
000
111
v
c
v
c
00
11
000
111
00
11
000
111
00
11
00
11
000
111
000
111
00
11
000
111
00
11
000
111
00
11
00
11
000
111
000
111
00
11
000
111
00
11
000
111
00
11
00
11
000
111
000
111
00
11
000
111
00
11
000
111
000
111
000
111
00
11
00
11
n
h
n
h
00
11
00011
111
00
000
11111
0011
00
000
111
000
111
00
11
00011
111
00
000
11111
0011
00
000
111
000
111
+ $$J 6  5

      .  /


      $ "  4   

  
    


  

(

8 , 9  


  
 .             /
   .   . 4 
               4 8   4
     /
   .  4 4 
?4  K L 7 1
  K L 4      5 . 
  " 
 4     /
   .  .   . 
. 5      8 A $$  
      .    
5    .     5    .     5    
.    &4 5
- 4  .  %           5 .   /
 "  A    
       "  4&       '
  KL 7 1
 G.    A $ .  
 . KL 
  'H
 
 4& . 
   .     
  
    

   4  + '
 4  
        
 K/ L   . .   I 
& 4  
     &4
    K/&4L   +& & 
   /   /&4
   
   .      

+ $J  
 . .
 K/ L  K/&4L   "   /

           


      &
   "   
    &   
  4  

+ $  4  
 & 4 /   /&4    .
I
   4  
 G  &  H        
  & 
 G  &   H + $  4  4& .
      
    
      
  &4   C    4 KL  
    &
4 KL 
 %' &

&   &  A 


 K

  
    


  

8      

(

L 4 4   
   &4         >  
       
 .
    .
&4   5I
5&   
      K/L   K&4/ L  

1111
00
00
111
00011
0011
0011
00
000
111
000
111
00
11
00
11
111
000
00
11
00
11
00
11
000
111
000
111
00
11
00
11
000
111
00
11
00
11
00
11
000
111
000
111
0011
11
00
00011
111
0011
0011
00
000
111
000
111
00
00
00000
00
11
00
000
000
11
11
111
11
11
111
111
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
111
00000
11
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
11
111
1111
111
111
0011
00
00000
0011
00
000
000
00
00
000
00
00
11
00
000
000
11
11
111
11
11
111
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
0011
11
00
00011
111
0011
0011
00
000
111
000
111
0011
11
00
111
00000
1111
0011
00
000
111
000
111

+ $J +


 K/ L  K/&4L   "   
  4
      &   
  4   
    /
     
  A $  .
 K/
L   " 
  . 
    &      J  
 
 G H    & 
 G H " 
 
  .   4   4   /       /&4   
         
 4   /    "   .  
' 
    & .  
          /
    
 
 + '
    
  . 
   
   A $            G      &
 4&   /
   .    5       
/.
 .   H =4    /
   .    5  
&   5
   
  .  
  4     "   & 
 & A    

         /


  
    


  

((

8 , 9  


+ $ J  K/ L   K L   


  4   
   & 
  4         
   4
 
  &
8      7 1   . 
 <    F  .


            "     
   .  8 6    . 
 
Exercise 9.10
   7 1   .  8 8      
 /
   3   
 .  8 6  8  & 4   
    
  .
    
2
Exercise 9.11 + $    4&         

"  .  /
          
 . 4   
E    &    "      
 /
4    8.      & 
 G   

&    H        .   
 
" &       . 
  A $    A
"  &     4 


. F       

    &4
 4  ' &

& G8     


      
H    .
 &  


  
    


  

8      

()

+ $J  
 . 
  /#   +  
 G/
  &  H    & . 
 G  &   
H
 . 
    
/.  &       . 

I  
 4 &

&  
   
 &  A
- 
  

   8 8   6 8     

  . *      
 
      4m 2n
  m  n 
  m 2  n 3 .
G"    .       .  8 8    .  6 8 
      
           .    /

   8. &     &       90 4 

    .   

H
2
Exercise 9.12 3 .   . < (4m + 2) (4n + 2)  5 &
 (2m + 1) (2n + 1) KL           
 .
GH 5   /
         &   ' 
$      =4        7 1   .  .
< (4m + 2) (4n + 2)  4   m  n    1  & ' ' 
$ 
"  &     .  /
   .    
  .
  5 G  '  $ . 4    H "  .    


  
    


  

(*

8 , 9  


    & 4


 C  
   
C  4 
    
 5 "   
    A $(

11
00
0011
11
00
00
11

+ $(J & .    7 1   . (4m + 2) (4n + 2) 
> 5    .  /
        

 5 "         4

    .   & .  6 6  # .  .  
&

& .  6 6  8       4 



  
 A $(        D  I   
    . & 
  
      
%' 4   &  .   6 6     7 1 /
 . &  . < (4m + 2) (4n + 2)  4   m  n   
1
2

9.4

Discussion

8      .  &
 &   7 1    
  &  
 
  4 
    &  '
 .    
/

  
    


  

8 5 
 

(

 " 
  4        
 &  
 
/
 C     7 1    
    /
  
 
    
"  &  .      4        '   
  
 "       .  
  4      
7 1    
 " 
           . 
8 8     
   &   
   
"  &  

   A  .
  K L  KL
  /
   . 5 
  
  
  /
   
>   
    .&    &  

   
    7 1    
    4 

    
. 4    &  & 
   .  
 
 4   .  
       &         
 5  4            7 1   . 
 4   
 . 5 "       4     
< (2m) (2n + 1)  . 
 m  n  G"          
     H +     
   .   < .
 . 4   7 1   '      $)
"  7 1/    
'
A  
 .
 
    4 
& 4   &   4  "  n  v  h  c    .

 '
 .  KLI     5 .    &  

  '
 .  K5  L    .         
5  . D    '
 .    
  K5 
L
"  7 1/    
    .    .  
 
K=
/    
L 8       =
/    

  /  K L G 4 . KL 4 KL     H   
5
   A             ' &   .
      =
/    
      . 
 .  
  K? / 
L  
 ? / 
  
   

  &  . A    & .   "    
    &    4         G. '
 
& 5  .   5       &    4       
7 1  HI 4 .    . ? / 
  
   
 

  
    


  

(!

8 , 9  


 4    
 .    K 
'&  &L    
 
    . 
      &  5.& 4   

 
 & 

9.5

Boards of Other Sizes

"  4

9.6

Bibliographic Remarks

    .


    7 1    
 &  .
        .   M# U  4 ;N   A
7 1       . & / /
     &
 .  4 
"   . 5     7 1    
   % -
3D M3D$ N =   .  /<      
  .

  /
       '  $ "  4 

.  /
      3  #  M 

 N 


    . K
L # 1    & . 
  D 4  . K
L    .    3D1
    . .  G4  
&H    
 

  
     "    '  $ 4   &     4
. .   .
# 


  
    


  

Solutions to Exercises
2.1 1233 

  & 6 k    
 . &      
   g    
 . 
     & 8& k  g 
 5  0  %& 
  
  & 
 &      k
 g  4& 5 "     
 12341 &
   
 =    
 . 

  &
8  .    p &   
  . p1 

2
2.2 6 m  n  p    
 .  D  .  
"   
   
 &   

m,n,p := m+1 , n1 , p1 .

     
mn  np  pm  8  &     & . 
   &   
 G8     F       
     & 2 H    
 .  F  . &   

 4&  G   <  4H            4   
 4  F  4         
    .   
  <  F  "     
    .    D   
.   
     
    .  

"  
 
  D 
        D   .  

  5    &   D  
 8.   
     D 

  
  4  D  . F        

.  D  .         .5& 4
      
  
 .  D 
+
& 4 ' J
do mnp = m m,n,p := m+1 , n1 , p1
od
2


  
    

($


  

)

&    !"




3.1
{

5C ||

2C,3H |3W| ; 2C,3H |1W| 2W ; 5H |3W| 2W


;

5H || 5W

5H |2W| 3W ; 2C |3H| 3W
;

2C || 3C }

2C |1C| 2C
;

3C || 2C }

3W |3H| 2C ; 3W |2W| 5H
;

5W || 5H

2W |3W| 5H ; 2W |1W| 2C,3H ; |3W| 2C,3H


{

|| 5C

2
3.2 -
.&       A/   
 F & &   
     .  - J
{

4C ||

1C,3H |3W| ; 1C,3H |1W| 2W ; 4H |2W| 2W


;

4H || 4W

4H |2W| 2W ; 2C |2H| 2W
;

2C || 2C }

2C |1C| 1C
;

3C || 1C }

3W |3H| 1C ; 3W |1W| 4H
;

4W || 4H

2W |2W| 4H ; 2W |1W| 1C,3H ; |3W| 1C,3H


{

|| 4C

&  .    4      J


{

4C ||


  
    


  

&    !"




)

1C,3H |3W| ; 1C,3H |1W| 2W ; 4H |2W| 2W


;

4H || 4W

4H |1W| 3W ; 1C |3H| 3W
;

1C || 3C }

1C |1C| 2C
;

2C || 2C }

2W |2H| 2C ; 2W |2W| 4H
;

4W || 4H

2W |2W| 4H ; 2W |1W| 1C,3H ; |3W| 1C,3H


{

|| 4C

2
3.3 6 M     & .      N    
 . 
- 
   N    2  M  
  


. 3  N/2  G" 
  

     .   .  & 2  4    


.  & 3  6 H
6 lH    
 .      .  "  
 .  
       rH     NlH  
&  lW    

. 4    .  "  
 . 4        rW    
NlW 
-      

GH

(lH = lW) (lH = 0) (lH = N) .

"                     
.   4  8   5
 . &      &    G8.
   0 < lH < lW  0 < rH < rW  8 4   4 
    
   .         8       H
?4 4 
      
  M  N   
(a) "       .  

G(H

M < lH ,


(b)           

G)H

M lH .


  
    


  

)

&    !"




& GH  &  & G    


   M  N H
?4  GH        
 .
.    ?  
lH = 0  .  .    G.    lH = 0  '  &  

   M < lH  .    lH = 0  
    
 M
     H     GH    J   

      4 
G*H

(lH = lW) (lH = N) ,


     .&   & > 4     
?     .//    lH P lW   
-  4  .   J lH = N   (lH = lW) (lH = N) 
  
 N/2       . lH = N .    
 N/2  .    .  "   G H   .   
8. (lH = lW) (lH = N) .      & lH = lW
 
/
 &    G"     G*H
 
     
  lH H "    5 
 . 4   

   
  &       
    . lH    & 
 1 
 G(H  
    .   I 5& G)H   .  
            "  G H   .   
8   4       .    G H  
?4  G H      
 .
   .   //.
  lH P lW      lH = 0 .     lH
    lH = 0 .    G & 4   
   M 
/<       lH = 0  > &           &
0   '  .
9H
 4  4 J   
      
   
8.        .    lH I  .    M < lH 
G. H        .  "  GH   .   
8. & 4  
       lH = N G   G*H
 

 . & 4    
  
   lH = lW H "   lH 
   M < lH  .  .    .      
 .     .  .    "  GH   .   
8   4       GH   .   
8 

& & GH   &  . GH     .// 
 
 G H  
 I  / . G H      //. 

 GH  
     
   GH  G H   "     
               
2

  
    


  

&    !"




)

3.4       4 


   & .4   4  
&      G- 
   DA  .   
 
  ! .   
 4 &   

& 
H
 5      
   . ' & 5  . 4  2
        
 & D   2  

   &  
   . 4   3  4 C  4
4C    G  & 5  .      .  
   8.    .   4 4
   D&      .
  4 .  
   D& &    4  4
 .     5   4 5   . 4      .   
 5  C     
 .            

.    .4     &   
 
H
"   4  .    4 4  J   
  
   
 & "  & K   4 4   L  
/

 &   1  2 G  4 .H  4  1 
"   3  4    2  +&  1  2 
 "   
     & 
t1t2 + t1 + t3t4 + t2 + t1t2 .

G"  A'  K L 


'

   4 
   t1 t2 t3 t4 
t1t2 
A  t2  t3t4 
A  t4  =4  
.&   .

D &
   &  .&      H 8.   4 4 
&      &     1  4  
    .
    8

   &    
 .
t1t2 + t1 + t1t4 + t1 + t1t3 .

G"     1   2     1   


 1   4     1   A&  1  3
 "    4   2  3  4   .  

 " 
   .    
 .   
H

    
   A &     4  t2+t2 t1+t3 
    & 4  t1+t3 t2+t2  G"    
 
 . 


   J 4  t2 + t2 = t1 + t3   &  
& 
 4   4
H
&       4  A  4 J
(a) "  
   1
 1
 3
  3
J   1+1 1+3 
  4 4      "    
  1+1+3+1+1   7




  
    


  

)(

&    !"




(b) "  
   1
 4
 4
  5
  
1+5 4+4    4 4    & "    
  4+1+5+1+4 
 15
 G"    
 .   4 4     4+1+5+4+4 
 18

2
3.5 62331   108 
2
4.1 H ?
 & &  3 
      3 
    " 
 .  &    
  ?
 G      & . ?
H

& 
 & &      ?
       
    
  ?
 "   .  &    
 
>   8    4 &   
    & .  
  " 
    .   
    .   '
  -     &   
&  
  F 

H 8 3 
        /
 &    4 /
    /
 & G" J "  K L    &  
   & 
 "     4   
& .   4
H "   .   /
 &   /
 &   & 4  
 
 
  /
 &   4 & 
8     & 4  
  3 
 4 & &  ?
 
   4  8 >    3 
   /
 &  
  & &  
   4  
&    
/
 &     & &  0&   4 
"     0 4     
 . & "  & 4 

  0& I 5& & /
 &  0    &
" 
   & /
 &  #&   4 &I I  & /

 &     4 & " 
    #    &
  #    
 . &    4 4 . 3 
  ?

  %& /
 &  #    &  & &  + & 
 4 & +&   /
 &  0&   &
-         &    4 "  &   

   & .   .4
 4    /
 &   ?
 

0&  + & > 4   &   
   ' & .   &
   
 .    /&
2
4.2

"  A     4    


  
    


  

&    !"



 0
"&
6
#

))
1
6

2
2

3
2

4
6

5
5

6
6

7
6

8
6

9
5

10
6

"  J - G-H  6 G6H  .     {2 , 5 , 6}
 11
"&
6
#

12
6

13
2

14
2

15
6

16
5

17
6

18
6

19
6

20
5

21
6

"  J - G-H  6 G6H  .     {2 , 5 , 6}
"              
2
4.3 "  5             . .      
 .        A  4    
   &  .  6  . 4    
   &

    
 
 4   &
 8. &     

   
WWW
   4WWW
2
4.4 H     .  
' 
       10. " 
'

  .
 I     
' 
 .  m  5   

' 
 .  m mod 11 
0

"&
6
#' ?
 0

1
6
0

2
1

3
1

4
6
0

5
2

6
1

7
3

8
6
0

9
2

10
1

"  J #' 
 .     {2 , 5 , 6} 

8   . 
  
' 
 .  m  m mod 3  "  4  
'

 .     
    4  
   ( G>  4 
  .   4
H
2

  
    


  

)*

&    !"



6. ,
   ,
 KL  4



(



)
)

*
$
(

(

"  (J -


5.5 6 col     .   5  n    


 .
 
 
 
col , n := col , n+1 .

  .   
 
col even.n .

  
 .
 G 63 H       .   5 1 
   
      5     
  col even.n 
4   
 
2
5.6    
 .   n  "   2n       4 
    4 4 0  2n 2  8. 2n 1 .  
C& 
  C    4  F 
 .   
  
 4 
   4 k  .  k 4 0  2n 2 G H
8. n  1    &            1    
         0 

?4    n     1. 8         4 
          1     4  4    
0  2n 2 
 "   4     2n 2 
   4 &
'     G  
. . H &   &

& .    / 


          & '    1     4
    8 .4     4  4     0  2n 2 

    4.           .4       
1 
?4  4      "   4    &  .
    n1  "  
 . 
          
&                
 . 



  
    


  

&    !"




)

     4 


      &     & 
    D       1  % 
   
 . 

      1        &  "      1

  .      n1 

2
5.8
(a) false
(b) false
(c) false
(d) p
(e) false
(f) q  r
(g) p
(h) true
2
5.9
true
=

4 p p false 4 p := true

true false
=

4 true p p 4 p := false

false .
2
5.10
p
=

4 p p false 4 p := p

p false
=

4 p p false 4 p := p


 &

& . 5 

p .


  
    


  

)!

&    !"




2
5.11 "    .  & .  & 
 a  (a  b)  
a  (a  b)
=

   

(a  a)  b
=

G a  a false H

false  b
=

A . 

false b
=

A . J G)H

b .
2
5.12 6 Q    5 "  Q A A  B   Q B  8 4  
4      
2
6.1 8  5   & 4        8.     
    &     
 .       .  .   4
         . D      & .  
.      . & "         
   
.   D       
 .       &   

  
"   
   &     .   4    
    4         
"  
 .   4& 4 
 4
&     "  
&    4       -     n+1   &  .
            
  &   .  .
   ' & 4  
    . F 4& . 
     .  =4 4 .    . &     
.  D      
  F "   ' & 4 4& .
    4 n+1 
2
6.5 -  m  0     D   D  "     5  D   0 G4 
5 20 H 
        . 


  
    


  

&    !"




)$

 4   m     0     3m  D   3   .


3m1  D  > .   3  4   F 4       4 4 
4  . 5 4  
 2 
    
 4  .  
   "  &   
  .  2(m1) 
  5
 A   5  D      "     . 2(m1) + 2   2m 

  5 &     & 
8     
 4     5  D          
  G            D   D    4      
         H 8        A 4 

2
6.6 H + n = 1       0 
   +     

   n1 
    A     . n  D  " A  
  . n+1  D   n1 
  A     . n  D   

    D  4   G n+1 H  D  "    .   4      .
 
   ' 
  

 n  
H + n = 2       1 
   +      
  
2n 3 
    A       . n  D  " A  
    . n+1  D   2n 3 
  A     
 . n  D     L  H     G n+1 H  D  N  "   
. L  N      .  
     . H  N     .
 
 "  5 4 ' 

 (2n 3) + 2   2(n+1) 3 

H 
 A  C  "    .   4      .   . 
 B
 D  "   .   4     .   .
" 4 .  D  A 
 4       A    
 B  64 
   
 4  D         C   
  D  "      
H + m = 1       1 
    A      
. 2  D   1 = 31 2 
    2(m+1)  D     
 & 4 .    D 
6      A     B  &   4  A     
 .   
 2m  D   3m 2 
 6    C 
D   & - 4  .  D  A  B  C  D     A < B 
C < D  &  G H       .   .   .  2 . 

 "           .  2(m+1)  D    
 
 . 
  1 + (3m 2) + 2 4  5 3(m+1) 2 
2
7.1 +
& 4 


  
    


  

*

&    !"



To.d
=

A . T

length(H0.d)
=

A . H0.d

length.[ ]
=

A . length

0 ,


Tn+1.d
=

A . T

length(Hn+1.d)
=

A . Hn+1.d

length(Hn.d ; [n+1 , d] ; Hn.d)


=

A . length

length(Hn.d) + length([n+1 , d]) + length(Hn.d)


=

A . T G4 H  length

Tn.d + 1 + Tn.d .

"  
To.d = 0
Tn+1.d = 2 Tn.d + 1 .

8. 4 '   5 . n = 0 , 1 , 2 , . . .  D  4  .   5 .


H  4     To.d  0  T1.d  1  T2.d  3 G   .  d H " 
   .
.   5 . Tn+1.d G     
  & 2 H
   Tn.d  2n1  "  
   .  

2
7.2 -  &    
       <<
&   8 &
     &      .   <  . 4 4   .&
   .   << 4 &    .& 4       + '

    A    4  .&    1    A   2 
  B   3  4    A   5    B  "   4



  
    


  

&    !"




*

+ )J / 


. 0/  

    
.  A   3
    .    1
    .
 3    5
     
.  B   2
    . 
?   
 .    A             

  .
"   .  n / <<      A &  5  . n  

"  A 
    5       .  1           .
 2     "     k  
    5       G 
H
.  k     
&    .    4        3n
F     n /  

?4 4     4  -  A    
4 
         1 /  
     2 /  
    4
    n /  

-          ' &  J    4      
 & .    "    4  A ) GB
&   &    A
8  .   9H
- 4 ' 4      / 
.   G n+1 H/
 
 .   & n     4      
.   n /
 
 G A *H %    5  . n+1  
 "  A n 

 .&     .   
 n     G n+1 H  A     .
    "       / 
.   n /  

   3     / 


.   G n+1 H/  
 " 
      / 
.   G n+1 H/  
  A &
 5  . n  
 .4 &    
 A  B  C  -   

 
  4 J   4      G   
 n+1 H

    4            


  
    


  

*

&    !"




 A
           -      

&    A  B  C     1 F    
 & 
 4 . 
 .  
  " 
   &  .
 s 
 t    n /  
     .
 sp   tp   
G n+1 H/  
 4     
 p    A  B  C  "  A  
    .   / 
.   G n+1 H/  
  
/ 
.   n /  
 
    .   
"  p  &   
A & 
&  p     .  5  . 

     
?4 
        
 n+1     
 
& & 
 .             
  F
            "   '   .
  n+1 
         / 
J       
AnB  AnC         BnC  BnA     
   CnA  CnB  "      4  
 &  A *    
      . 
    
  n+1 
2
7.3 %       .

      .   
 
G4     
H
2
7.4 "  
  & '    .4       
 '  G 4         
 k   H

      
  k      d   4  k > 1  G 
   1    
 H  d  odd.k d  
 #  k G     d   . H
 #   
       d 
"     DA  .4 -     '  4 4    
A k1           d  .
 k   
 .
  k 
    .    
  "       &

   k1 
       d   "       1  

    d 4 
even.(k1) d  even.1 d .


.& 4    d = (odd.k d  )  G8 4         

 
      
         k 
 . k  

  
    


  

&    !"




*

AnA

`
A
 AA



BnA` 

`



CnC

+ *J

` ` `

AA

` ` `



BnC ` 
A
 AA

AA
A`

AnC

`



AnB

A`

CnA
A
A
A ` CnB
A
 AA

` ` `

AA
A`

BnB

  .   / 


.   (n + 1) /  


  
    


  

*(

&    !"




I  4   
  
         k H " 
  .   "4 . = 
         4 
 5  .
 . 4   k1  4  
   k  3
  5  .
   
  4  
    
   >

 4     .   
 
& 
&   
"  & 
       '   4          

  5
2
7.5 "             4     
4   
 6 k    
 .        I  &
k  N  4   4  k  0  % 
    . k   4 
    k 
      
 
8.   k            k & 1  > 4   
 
    d .
   #   
 k1  
    d   
  k    .  +&   k & 1 
      k  0.
2
9.1
(a) "  
 .
     
 5   
 . 5 . 
 
 .
    .    5  F .
  
.    5  .   
 .
   
   
    5
(b) 81 &       /
   .  21     C
  .   5
      5   
   C  
 4   /
      8. m     1    
5  4
 .
   5I   
      5
      5 4     / 4 5
  
 
(c)  G H .    .  21  + n    1    /
  
.  2n   
 &    
 / &/    
5    
 4         4
2
9.2 +   3 3         ' & 4    
 5 
 D     5 +     
   
   .   
 5  (m, n)  G8 1
 4   



  
    


  

&    !"




*)

  <  H "         .   


 5 ' &
4  even.m = even.n  "           .  
    4&     / /
 5     .   4  

 . 5 "           
 . 5  
  .  
    5     .      
C & 4   3 3      C    .    4  

 5 "            4  A  
odd

even

odd

even

even

+ J  /


   G 4  H .  3 3  
  .  
5
2
9.6

n
v
h
c

n v
v n
h c
c h

h
c
n
v

c
h
v
n

"  )J 5


 . + >

& G$H  A &          &

     /.


 
/   @A  .    & & 

 
"     x  y  z  n    4 
& "       
 "    '
 .  K   .4L .9
2
9.9 "  
2


  
    


  

**

&    !"




9.10  A !


"  
       . F     & 
      
          

? 4     . 
      .    8 .   

  &   &     8     
 .

     &4   "   


 .
     
  
 .
 8   4&    
 .     . 
    
  . 
 
 8      4    
 &4   4   & K &L     

+ !J  7 1           "     
 
      "  
      
   &
   
"  
  . 
 
          . 
8 6 I     5   K L    /
     
 

   
  G     &       
  .  6 8 H
2
9.11 + $  4  . 4    /
    
 #


  
    


  

&    !"




*

  &      &   


   &   


+ $J 3 . 4   .  /


    
I    

   &      & 
   &   
H
+   4          4& "       .
   I     
     
8        . &  . < 4m 2n  4  m   
2  n    3          5   A $  $ .
'  /
     .  & < "     
  . 
   .  .    /
         
8 6 /   

2
9.12 -  & .&  
  4  A $(  A  G?  
&

&H
?4   &  A     /
      
 5 
A 
+     
   &  '    /
  
2


  
    


  

*!

&    !"




+ J 7 1   .  8 6   8 8  G3    
.    I     
      & 
   
A $H


  
    


  

&    !"




*$

+ J 3 . 
   3
   4     

   "      


       
  
      


+
 

J 7 1

  .  6 6  "      .


 .  


  
    


  




  
    

&    !"





  

Bibliography
M N
M N

   

 - 
. -
       

/ +
#
  . 0  -&  6 

   
"    

JPP444  P  P# P  

 



M ,! N %4&  
 0  = 4&    7 ,&   

 8  88  
  $! 
M+N

     # + "   & . 5 


   "4 . =  
   
 '   JX
* 

M6!N

 
  6 6& "  "4 . =  
  

 '   J (X (( $!

M3D$N

% - 3D
%-3!J
"    
http://www.cs.utexas.edu/users/EWD/ewd10xx/EWD1083.PDF
 $$

M3D$ N

%
-
3D
%-3)J
" 
 1

http://www.cs.utexas.edu/users/EWD/ewd11xx/EWD1135.PDF

/
 $$ 

M3D$N

% - 3D
%-3 *J " 
      
http://www.cs.utexas.edu/users/EWD/ewd12xx/EWD1260.PDF # $$

M3$N

% - 3D       


 -
   
+  
 "' 
   
    /@
$$

  

/

M3-N 0   31  3  - (   


 0.
+       = 

  
    





 


  



&    !"




M,!N

3 , 0 +
 


 /@ $!

M,$N

3 ,  +    $ '


 $
/@ $$

M6N

& 6  


  0 ,  $ $    $   
 -& 

M N

,R
 


  ,
 ( 

           *  $ 

   0  
 -   +
 
 !J (X (* >    

M
!N &
 
&   0 1 2 0 3
%4 F ?0 $!

 /=

M$N

8 4 0 (


 (  -A  ?  6 $$

M")*N

. " '


4 +  
4 (    
4    5678 
5689 >'. ;&  $)* " & 0=-

M-!N

0, -  A  & .     
'   )J X ( $!