Task   (in three parts)



Part 1
Write a procedure (say 





c
o
9


(
x
)


{\displaystyle {\mathit {co9}}(x)}

) which implements Casting Out Nines as described by returning the checksum for 



x


{\displaystyle x}

. Demonstrate the procedure using the examples given there, or others you may consider lucky.

Part 2
Notwithstanding past Intel microcode errors, checking computer calculations like this would not be sensible. To find a computer use for your procedure:

 Consider the statement "318682 is 101558 + 217124 and squared is 101558217124" (see: Kaprekar numbers#Casting Out Nines (fast)).
 note that 



318682


{\displaystyle 318682}

 has the same checksum as (



101558
+
217124


{\displaystyle 101558+217124}

);
 note that 



101558217124


{\displaystyle 101558217124}

 has the same checksum as (



101558
+
217124


{\displaystyle 101558+217124}

) because for a Kaprekar they are made up of the same digits (sometimes with extra zeroes);
 note that this implies that for Kaprekar numbers the checksum of 



k


{\displaystyle k}

 equals the checksum of 




k

2




{\displaystyle k^{2}}

.
Demonstrate that your procedure can be used to generate or filter a range of numbers with the property 





c
o
9


(
k
)
=


c
o
9


(

k

2


)


{\displaystyle {\mathit {co9}}(k)={\mathit {co9}}(k^{2})}

 and show that this subset is a small proportion of the range and contains all the Kaprekar in the range.

Part 3
Considering this MathWorld page, produce a efficient algorithm based on the more mathematical treatment of Casting Out Nines, and realizing:

 





c
o
9


(
x
)


{\displaystyle {\mathit {co9}}(x)}

 is the residual of 



x


{\displaystyle x}

 mod 



9


{\displaystyle 9}

;
 the procedure can be extended to bases other than 9.
Demonstrate your algorithm by generating or filtering a range of numbers with the property 



k
%
(


B
a
s
e


−
1
)
==
(

k

2


)
%
(


B
a
s
e


−
1
)


{\displaystyle k\%({\mathit {Base}}-1)==(k^{2})\%({\mathit {Base}}-1)}

 and show that this subset is a small proportion of the range and contains all the Kaprekar in the range.



related tasks
 First perfect square in base N with N unique digits
 Kaprekar numbers


  [ true unrot swap 
    witheach 
      [ over find
        over found not if
          [ dip not 
            conclude ] ]
    drop ]                is subset ( [ [ --> [ )
 
  [ abs 0 swap
    [ 10 /mod rot +
      dup 8 > if [ 9 - ]
      swap dup 0 = until ]
   drop ]                  is co9   ( n --> n   )
 
  say "Part 1: Examples from Dr Math page." cr cr
  say "6395 1259 + = " 6395 1259 + echo cr
  say "6395 co9    = " 6395 co9    echo cr
  say "1259 co9    = " 1259 co9    echo cr
  say "5 8 + co9   = " 5 8 + co9   echo cr
  say "7654 co9    = " 7654 co9    echo cr cr
 
  say "6395 1259 * = " 6395 1259 * echo cr
  say "6395 co9    = " 6395 co9    echo cr
  say "1259 co9    = " 1259 co9    echo cr
  say "5 8 * co9   = " 5 8 * co9   echo cr
  say "8051305 co9 = " 7654 co9    echo cr cr
 
  say "Part 2: Kaprekar numbers." cr cr
 
  say "Kaprekar numbers less than one hundred:    "
  []
  100 times
    [ i^ kaprekar if
      [ i^ join ] ]
  dup echo cr
  say '0...99 with property "n co9 n 2 ** co9 =": '
  []
  100 times
    [ i^ co9
      i^ 2 ** co9 = if
        [ i^ join ] ]
  dup echo cr
  say "Is the former a subset of the latter? "
  subset iff [ say "Yes." ] else [ say "No." ] cr cr
 
  say "Part 3: Same as Part 2, but base 17." cr cr
 
  say "Kaprekar (base 17) numbers less than one hundred: "
  17 base put
  []
  100 times
    [ i^ kaprekar if
      [ i^ join ] ]
  base release
  dup echo cr
  say '0...99 with property "n 16 mod n 2 ** 16 mod =":  '
  []
  100 times
    [ i^ 16 mod
      i^ 2 ** 16 mod = if
        [ i^ join ] ]
  dup echo cr
  say "Is the former a subset of the latter? "
  subset iff [ say "Yes." ] else [ say "No." ]