In number theory, Kaprekar's routine is an iterative algorithm named after its inventor, Indian mathematician D. R. Kaprekar. Each iteration starts with a number, sorts the digits into descending and ascending order, and calculates the difference between the two new numbers.
 As an example, starting with the number 8991 in base 10:
 6174, known as Kaprekar's constant, is a fixed point of this algorithm. Any four-digit number (in base 10) with at least two distinct digits will reach 6174 within seven iterations. The algorithm runs on any natural number in any given number base.
 The algorithm is as follows:
 The sequence is called a Kaprekar sequence and the function 
  
    
      
        
          K
          
            b
          
        
        (
        n
        )
        =
        α
        −
        β
      
    
    {\displaystyle K_{b}(n)=\alpha -\beta }
  
  is the Kaprekar mapping. Some numbers map to themselves; these are the fixed points of the Kaprekar mapping, and are called Kaprekar's constants. Zero is a Kaprekar's constant for all bases 
  
    
      
        b
      
    
    {\displaystyle b}
  
, and so is called a trivial Kaprekar's constant. All other Kaprekar's constant are nontrivial Kaprekar's constants.
 For example, in base 10, starting with 3524,
 with 6174 as a Kaprekar's constant.
 All Kaprekar sequences will either reach one of these fixed points or will result in a repeating cycle. Either way, the end result is reached in a fairly small number of steps.
 Note that the numbers 
  
    
      
        α
      
    
    {\displaystyle \alpha }
  
 and 
  
    
      
        β
      
    
    {\displaystyle \beta }
  
 have the same digit sum and hence the same remainder modulo 
  
    
      
        b
        −
        1
      
    
    {\displaystyle b-1}
  
. Therefore, each number in a Kaprekar sequence of base 
  
    
      
        b
      
    
    {\displaystyle b}
  
 numbers (other than possibly the first) is a multiple of 
  
    
      
        b
        −
        1
      
    
    {\displaystyle b-1}
  
.
 When leading zeroes are retained, only repdigits lead to the trivial Kaprekar's constant.
 In base 4, it can easily be shown that all numbers of the form 3021, 310221, 31102221, 3...111...02...222...1 (where the length of the "1" sequence and the length of the "2" sequence are the same) are fixed points of the Kaprekar mapping.
 In base 10, it can easily be shown that all numbers of the form 6174, 631764, 63317664, 6...333...17...666...4 (where the length of the "3" sequence and the length of the "6" sequence are the same) are fixed points of the Kaprekar mapping.
 It can be shown that all natural numbers
 are fixed points of the Kaprekar mapping in even base 
  
    
      
        b
        =
        2
        k
      
    
    {\displaystyle b=2k}
  
 for all natural numbers 
  
    
      
        n
      
    
    {\displaystyle n}
  
. 
 
  
    
      
        α
        =
        (
        2
        k
        −
        1
        )
        
          b
          
            2
            n
            +
            2
          
        
        
          (
          
            
              ∑
              
                i
                =
                0
              
              
                n
              
            
            
              b
              
                i
              
            
          
          )
        
        +
        (
        k
        )
        
          b
          
            n
            +
            1
          
        
        
          (
          
            
              ∑
              
                i
                =
                0
              
              
                n
              
            
            
              b
              
                i
              
            
          
          )
        
        +
        (
        k
        −
        1
        )
        
          (
          
            
              ∑
              
                i
                =
                0
              
              
                n
              
            
            
              b
              
                i
              
            
          
          )
        
      
    
    {\displaystyle \alpha =(2k-1)b^{2n+2}\left(\sum _{i=0}^{n}b^{i}\right)+(k)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)\left(\sum _{i=0}^{n}b^{i}\right)}
  

 
  
    
      
        β
        =
        (
        k
        −
        1
        )
        
          b
          
            2
            n
            +
            2
          
        
        
          (
          
            
              ∑
              
                i
                =
                0
              
              
                n
              
            
            
              b
              
                i
              
            
          
          )
        
        +
        (
        k
        )
        
          b
          
            n
            +
            1
          
        
        
          (
          
            
              ∑
              
                i
                =
                0
              
              
                n
              
            
            
              b
              
                i
              
            
          
          )
        
        +
        (
        2
        k
        −
        1
        )
        
          (
          
            
              ∑
              
                i
                =
                0
              
              
                n
              
            
            
              b
              
                i
              
            
          
          )
        
      
    
    {\displaystyle \beta =(k-1)b^{2n+2}\left(\sum _{i=0}^{n}b^{i}\right)+(k)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(2k-1)\left(\sum _{i=0}^{n}b^{i}\right)}
  

 
  
    
      
        
          
            
              
                
                  K
                  
                    b
                  
                
                (
                m
                )
              
              
                
                =
                α
                −
                β
              
            
            
              
              
                
                =
                (
                (
                2
                k
                −
                1
                )
                −
                (
                k
                −
                1
                )
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                k
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                (
                k
                −
                1
                )
                −
                (
                2
                k
                −
                1
                )
                )
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                )
                
                  b
                  
                    2
                    n
                    +
                    1
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    1
                  
                
                +
                
                  b
                  
                    2
                    n
                    +
                    1
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    1
                    −
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        1
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                
                  b
                  
                    2
                    n
                    +
                    1
                    −
                    1
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    1
                    −
                    n
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                
                  b
                  
                    2
                    n
                    +
                    1
                    −
                    n
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                
                  b
                  
                    n
                    +
                    1
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                2
                k
                )
                
                  b
                  
                    n
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                k
                
                  b
                  
                    n
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                        −
                        1
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                    −
                    1
                  
                
                +
                
                  b
                  
                    n
                    +
                    1
                    −
                    1
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                        −
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                    −
                    n
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                
                  b
                  
                    n
                    +
                    1
                    −
                    n
                  
                
                −
                k
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                        −
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                b
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                b
                −
                k
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                b
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                2
                k
                −
                k
              
            
            
              
              
                
                =
                k
                
                  b
                  
                    2
                    n
                    +
                    3
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                
                  b
                  
                    2
                    n
                    +
                    2
                  
                
                +
                (
                2
                k
                −
                1
                )
                
                  b
                  
                    n
                    +
                    1
                  
                
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                (
                k
                −
                1
                )
                b
                
                  (
                  
                    
                      ∑
                      
                        i
                        =
                        0
                      
                      
                        n
                      
                    
                    
                      b
                      
                        i
                      
                    
                  
                  )
                
                +
                k
              
            
            
              
              
                
                =
                m
              
            
          
        
      
    
    {\displaystyle {\begin{aligned}K_{b}(m)&=\alpha -\beta \\&=((2k-1)-(k-1))b^{2n+2}\left(\sum _{i=0}^{n}b^{i}\right)+(k-k)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+((k-1)-(2k-1))\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+2}\left(\sum _{i=0}^{n}b^{i}\right)-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+b^{2n+2}-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k)b^{2n+1}-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{2n+1}+b^{2n+1}-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{2n+1-1}\left(\sum _{i=0}^{1}b^{i}\right)+b^{2n+1-1}-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{2n+1-n}\left(\sum _{i=0}^{n}b^{i}\right)+b^{2n+1-n}-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+b^{n+1}-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(2k)b^{n}-k\left(\sum _{i=0}^{n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+kb^{n}-k\left(\sum _{i=0}^{n-1}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{n+1-1}+b^{n+1-1}-k\left(\sum _{i=0}^{n-n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{n+1-n}\left(\sum _{i=0}^{n}b^{i}\right)+b^{n+1-n}-k\left(\sum _{i=0}^{n-n}b^{i}\right)\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b\left(\sum _{i=0}^{n}b^{i}\right)+b-k\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b\left(\sum _{i=0}^{n}b^{i}\right)+2k-k\\&=kb^{2n+3}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b^{2n+2}+(2k-1)b^{n+1}\left(\sum _{i=0}^{n}b^{i}\right)+(k-1)b\left(\sum _{i=0}^{n}b^{i}\right)+k\\&=m\\\end{aligned}}}
  

 All numbers are expressed in base 
  
    
      
        b
      
    
    {\displaystyle b}
  
, using A−Z to represent digit values 10 to 35. 
 220222101 → 221021101 → 220222101
 202222211 → 210222111 → 211021111 → 202222211
 31104443 → 43255222 → 33204323 → 41055442 → 54155311 → 44404112 → 43313222 → 31104443
 42104432 → 43204322 → 42104432
 53104421 → 53304221 → 53104421
 1776 → 6062 → 6332 → 3774 → 4244 → 1776
 3065 → 6152 → 5243 → 3065
 42744 → 47773 → 42744
 51753 → 61752 → 63732 → 52743 → 51753
 3076 → 7252 → 5254 → 3076
 5074 → 7072 → 7432 → 5074
 53955 → 59994 → 53955
 61974 → 82962 → 75933 → 63954 → 61974
 62964 → 71973 → 83952 → 74943 → 62964
 64308654 → 83208762 → 86526432 → 64308654
 865296432 → 763197633 → 844296552 → 762098733 → 964395531 → 863098632 → 965296431 → 873197622 → 865395432 →753098643  → 954197541 → 883098612 → 976494321 → 874197522 → 865296432
 8653266432 → 6433086654 → 8332087662 → 8653266432
 8765264322 → 6543086544 → 8321088762 → 8765264322
 8633086632 → 8633266632 → 6433266654 → 4332087666 → 8533176642 → 7533086643 → 8433086652 → 8633086632
 9775084221 → 9755084421 → 9751088421 → 9775084221
 3098 → 9452 → 7094 → 9272 → 7454 → 3098
 5096 → 9092 → 9632 → 7274 → 5276 → 5096
 3BB8 → 8284 → 6376 → 3BB8
 4198 → 8374 → 5287 → 6196 → 7BB4 → 7375 → 4198
 2B → 85 → 2B
 0D → C1 → A3 → 67 → 0D
 2D → A5 → 4B → 69 → 2D
 0F → E1 → C3 → 87 → 0F
 3FFC → C2C4 → A776 → 3FFC
 A596 → 52CB → A596
 E0E2 → EB32 → C774 → 7FF8 → 8688 → 1FFE → E0E2
 E952 → C3B4 → 9687 → 30ED → E952
 86F88 → 8FFF7 → 86F88
 A3FB6 → C4FA4 → B7F75 → A3FB6
 A4FA6 → B3FB5 → C5F94 → B6F85 → A4FA6
 310EED → ED9522 → CB3B44 → 976887 → 310EED
 532CCB → A95966 → 532CCB
 840EB8 → E6FF82 → D95963 → A42CB6 → A73B86 → 840EB8
 A80E76 → E40EB2 → EC6832 → C91D64 → C82C74 → A80E76
 C60E94 → E82C72 → CA0E54 → E84A72 → C60E94
 B62FC95 → D74FA83 → C92FC64 → D85F973 → C81FD74 → E94FA62 → DA3FB53 → CA5F954 → B74FA85 → B62FC95
 B71FD85 → E83FB72 → DB3FB43 → CA6F854 → B73FB85 → C63FB94 → C84FA74 → B82FC75 → D73FB83 → CA3FB54 → C85F974 → B71FD85
 3110EEED → EDD95222 → CBB3B444 → 97768887 → 3110EEED
 5332CCCB → A9959666 → 5332CCCB
 7530ECA9 → E951DA62 → DB52CA43 → B974A865 → 7530ECA9
 A832CC76 → A940EB66 → E742CB82 → CA70E854 → E850EA72 → EC50EA32 → EC94A632 → C962C964 → A832CC76
 C610EE94 → ED82C722 → CBA0E544 → E874A872 → C610EE94
 C630EC94 → E982C762 → CA30EC54 → E984A762 → C630EC94
 C650EA94 → E852CA72 → CA50EA54 → E854AA72 → C650EA94
 CA10EE54 → ED84A722 → CB60E944 → E872C872 → CA10EE54
 In 1949 D. R. Kaprekar discovered that if the above process is applied to four-digit numbers in base 10, the sequence converges to 6174 within seven iterations or, more rarely, converges to 0. The number 6174 is the first Kaprekar's constant to be discovered, and thus is sometimes known as Kaprekar's constant.
 The set of numbers that converge to zero depends on whether leading zeros are  discarded (the usual formulation) or are retained (as in Kaprekar's original formulation). In the usual formulation, there are 77 four-digit numbers that converge to zero, for example 2111. However, in Kaprekar's original formulation the leading zeros are retained, and only repdigits such as 1111 or 2222 map to zero. This contrast is illustrated below:
 2111 − 1112 = 999
999 −  999 = 0
 2111 − 1112 = 0999
9990 − 0999 = 8991
9981 − 1899 = 8082
8820 − 0288 = 8532
8532 − 2358 = 6174
 Below is a flowchart. Leading zeros are retained, however the only difference when leading zeros are discarded is that instead of 0999 connecting to 8991, we get 999 connecting to 0.
 If the Kaprekar routine is applied to numbers of three digits in base 10, the resulting sequence will almost always converge to the value 495 in at most six iterations, except for a small set of initial numbers which converge instead to 0.
 The set of numbers that converge to zero depends on whether leading zeros are discarded (the usual formulation) or are retained (as in Kaprekar's original formulation). In the usual formulation, there are 60 three-digit numbers that converge to zero, for example 211. However, in Kaprekar's original formulation the leading zeros are retained, and only repdigits such as 111 or 222 map to zero.
 Below is a flowchart. Leading zeros are retained, however the only difference when leading zeros are discarded is that instead of 099 connecting to 891, we get 99 connecting to 0.
 For digit lengths other than three or four (in base 10), the routine may terminate at one of several fixed points or may enter one of several cycles instead, depending on the starting value of the sequence. See the table in the section above for base 10 fixed points and cycles.
 The number of cycles increases rapidly with larger digit lengths, and all but a small handful of these cycles are of length three. For example, for 20-digit numbers in base 10, there are fourteen constants (cycles of length one) and ninety-six cycles of length greater than one, all but two of which are of length three. Odd digit lengths produce fewer different end results than even digit lengths.
 The example below implements the Kaprekar mapping described in the definition above to search for Kaprekar's constants and cycles in Python.