A positive integer is a Kaprekar number if:

 It is   1     (unity)
 The decimal representation of its square may be split once into two parts consisting of positive integers which sum to the original number. 

Note that a split resulting in a part consisting purely of 0s is not valid,
as 0 is not considered positive.



Example Kaprekar numbers

 



2223


{\displaystyle 2223}

 is a Kaprekar number, as 



2223
∗
2223
=
4941729


{\displaystyle 2223*2223=4941729}

, 



4941729


{\displaystyle 4941729}

 may be split to 



494


{\displaystyle 494}

 and 



1729


{\displaystyle 1729}

, and 



494
+
1729
=
2223


{\displaystyle 494+1729=2223}

.
 The series of Kaprekar numbers is known as A006886, and begins as 



1
,
9
,
45
,
55
,
.
.
.


{\displaystyle 1,9,45,55,...}

.


Example process

10000 (1002) splitting from left to right:

 The first split is [1, 0000], and is invalid; the 0000 element consists entirely of 0s, and 0 is not considered positive.
 Slight optimization opportunity: When splitting from left to right, once the right part consists entirely of 0s, no further testing is needed; all further splits would also be invalid.


Task

Generate and show all Kaprekar numbers less than 10,000.



Extra credit

Optionally, count (and report the count of) how many Kaprekar numbers are less than 1,000,000.



Extra extra credit

The concept of Kaprekar numbers is not limited to base 10 (i.e. decimal numbers);
if you can, show that Kaprekar numbers exist in other bases too.



For this purpose, do the following:

 Find all Kaprekar numbers for base 17 between 1 and 1,000,000 (one million);
 Display each of them in base 10 representation;
 Optionally, using base 17 representation (use letters 'a' to 'g' for digits 10(10) to 16(10)), display each of the numbers, its square, and where to split the square. 


225   d4  a52g  a5 + 2g


Reference

 The Kaprekar Numbers by Douglas E. Iannucci (2000). PDF version


Related task

   Casting out nines


*        Kaprekar numbers          22/03/ 2017
KAPREKAR CSECT
         USING  KAPREKAR,R13       base register
         B      72(R15)            skip savearea
         DC     17F'0'             savearea
         STM    R14,R12,12(R13)    save previous context
         ST     R13,4(R15)         link backward
         ST     R15,8(R13)         link forward
         LR     R13,R15            set addressability
         LA     R10,0              n=0
         LA     R6,1               i=1
       DO WHILE=(C,R6,LE,=F'1000000')  do i=1 to 1000000
         CVD    R6,PI                pi=i
         ZAP    PS,PI                ps=pi
         MP     PS,PI                ps=pi*pi
         ZAP    PX,PS                ps
         OI     PX+7,X'0F'           zap sign
         UNPK   SW,PX                packed PL8 to zoned CL16
         MVC    SS(16),SW            s=pic(ps,16)
         MVI    OK,X'00'             ok=false
         LA     R7,1                 j=1
       DO WHILE=(C,R7,LE,=F'15')     do j=1 to 15
         LA     R2,16                  16
         SR     R2,R7                  -j
         ST     R2,LL                  l=16-j
         LA     R2,S1                  @s1
         LA     R3,20                  20
         LA     R4,SS                  @s
         LR     R5,R7                  j
         ICM    R5,B'1000',=C' '       pad
         MVCL   R2,R4                  s1=substr(s,1,j)        
         LA     R2,S2                  @s2
         LA     R3,20                  20
         LA     R4,SS                  @s
         AR     R4,R7                  +j
         L      R5,LL                  l
         ICM    R5,B'1000',=C' '       pad
         MVCL   R2,R4                  s2=substr(s,j+1,l)
         MVC    ZZ,=20C'0'             zw=(20)'0'
         LA     R2,S1                  @s1
         LR     R3,R7                  j
         LA     R4,ZZ                  @zz
         LR     R5,R7                  j
         CLCL   R2,R4                  if substr(s1,1,j)=substr(zz,1,j)
         BE     ITERJ                  then iterate j
         LA     R2,S2                  @s2
         L      R3,LL                  l
         LA     R4,ZZ                  @zz
         L      R5,LL                  l
         CLCL   R2,R4                  if substr(s2,1,l)=substr(zz,1,l)
         BE     EXITJ                  then leave j
         XDECI  R2,S1                  unedit s1
         ST     R2,M1                  m1=s1
         XDECI  R2,S2                  unedit s2
         ST     R2,M2                  m2=s2
         L      R2,M1                  m1
         A      R2,M2                  +m2
         ST     R2,MM                  m=m1+m2
       IF C,R6,EQ,MM THEN              if i=m then
         MVI    OK,X'01'                 ok=true
         B      EXITJ                    leave j
       ENDIF    ,                      end if
ITERJ    LA     R7,1(R7)               j++
       ENDDO    ,                    enddo j
EXITJ    EQU    *                    exitj:
       IF CLI,OK,EQ,X'01',OR,C,R6,EQ,=F'1' THEN  if ok or i=1 then
         LA     R10,1(R10)               n=n+1
         XDECO  R10,PG                   edit n
         XDECO  R6,PG+12                 edit i
         XPRNT  PG,L'PG                  print buffer
       ENDIF    ,                      end if
         LA     R6,1(R6)             i++
       ENDDO    ,                  enddo i
         L      R13,4(0,R13)       restore previous savearea pointer
         LM     R14,R12,12(R13)    restore previous context
         XR     R15,R15            rc=0
         BR     R14                exit
OK       DS     X                  ok logical
LL       DS     F                  l  binary
MM       DS     F                  m  "
M1       DS     F                  m1 "
M2       DS     F                  m2 "
         DS     0D                 -- alignment for cvd
PI       DS     PL8                pi fixed decimal(15)
PM       DS     PL8                pm "
PS       DS     PL8                ps "
PX       DS     PL8                px "
SS       DC     CL20' '            s  character(20)
S1       DS     CL20               s1 "
S2       DS     CL20               s2 "
ZZ       DS     CL20               z  "
SW       DS     CL16               sw character(16)
PG       DC     CL80' '            buffer
         YREGS
         END    KAPREKAR