A $\texttt{\binary}$ Klein bottle
These 72 replacement code tokens
form 2 static macros and 1 dynamic macro,
while blurring the distinction between inside and outside,
reminiscent of a
Klein bottle,
with data that are not just produced by recursive calls
but are explicitly those calls themselves.
$$\require{begingroup}\begingroup \def\safe{\text{\endgroup error}}
\def \@ #1\endgroup{
#1\@
\endgroup0}
\def \binary #1#2
#3\endgroup{
\def \N ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
##8 ##9
{
##8 ##3#2
#3##6\endgroup##2}
\N \binary \binary \@ 1
#3 \@
9 {}
8 7 6 5 4 3 2 1 0 % \binary {}
\binary #3 #2 \@
0
#3#3#3#3 #3 #1 #1 {}
\binary {}
}
\binary{19}
\endgroup$$
$\kern29em\safe$
This should actually count as 68 tokens
because 4 tokens are nonfunctional empty
$\small\texttt{\{}\,\texttt{\}}$ braces that ensure
spaces at ends of lines.
If those spaces haven’t been lost,
here goes 68 without the empties:
$$\require{begingroup}\begingroup \def\safe{\text{\endgroup error}}
\def \@ #1\endgroup{
#1\@
\endgroup0}
\def \binary #1#2
#3\endgroup{
\def \N ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
##8 ##9
{
##8 ##3#2
#3##6\endgroup##2}
\N \binary \binary \@ 1
#3 \@
9
8 7 6 5 4 3 2 1 0 % \binary
\binary #3 #2 \@
0
#3#3#3#3 #3 #1 #1
\binary
}
\binary{19}
\endgroup$$
$\kern29em\safe$
(If
$\small\bbox[mistyrose]{\color{red}{\text{\safe}}} \raise-.8ex\strut$
follows those results,
$\small\texttt{\endgroup}$ successfully practiced safe MathJax.)
The solution resides almost entirely within
$\small\texttt{\binary{}}$, if within means anything anymore.
$\small\texttt{\binary{}}$ performs two different stages:
cumulative conversion to unary and recursive halving to binary.
Conversion to unary adapts the
$\small\texttt{\stars{}}$ macro from
Starring MathJax.
Conversion to binary follows
Davide Cervone’s
framework.
Each unary digit is an instance of
$\small\texttt{\binary}$ itself,
called as a macro in the unary-to-binary stage.
Leading digit 0 acts as a
wheelhouse
and uses two dynamic skip parameters,
$\small\texttt{##4}$ and $\small\texttt{##5}$,
to decide how to handle
i) a mere $\small\texttt{\binary{0}}$ call,
ii) completion of a non-final decimal digit's unary accumulation,
and iii) stage transition from unary accumulation to binary halving.
Being mandatory and otherwise confining,
$\small\texttt{\endgroup}$ is commandeered as a delimiter,
to terminate an initially-empty intermediate result and
to precede the ultimate 0s and 1s.
This does not defeat
$\small\texttt{\endgroup}$ because it is faithfully rewritten
and no macros follow it.
More explanation and proofreading to come as time allows,
but for now just a fractal trace, a listing, and worksheets.
In the live trace of
$\small\texttt{\binary{25}}$,
$\small\texttt{\b}$ represents
$\small\texttt{\binary}$.
Superfluous line breaks at the end
are side effects of avoiding blank lines
and of distinguishing between input 1s and output 1s.
$$\require{begingroup}\begingroup
\def \Par #1{ \, \rlap{ \kern2mu\texttt {#1} }
\G{\raise-.2ex\underline{\hphantom{ \texttt{#1}\, }}} }
\def \G { \color{#888} }
\def \Eol {{ \, \large \raise-.3ex\G{\unicode{8629}} \, }}
\def \TSUB #1#2#3{ %1=[from] %2=[to] %3={fill2[from]fill1[from]fill0}
\def \TSUC % ##1#1##2#1%\TSUC##3{\texttt{##1}##3\TSUC % ##2#1%\TSUC{##3}}
\TSUC % #3#1%\TSUC{#2}#1%\TSUC%
} \def \TYPE #1{{ \TSUB {
} \Eol {#1} }}
\def \RETURN #1~{ \small \TYPE{#1} \\[-1ex]
#1~ }
%
\def \@ #1\endgroup{
\RETURN
#1\@
\endgroup0}
%
\def \b #1#2
#3\endgroup{
\def \N ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
##8 ##9
{
\RETURN
##8 ##3#2
#3##6\endgroup##2}
\N \b \b \@ 1
#3 \@
9 {}
8 7 6 5 4 3 2 1 0 % \b {}
\b #3 #2 \@
0
#3#3#3#3 #3 #1 #1 {}
\b {}
}
%
\begin{array}{r}
\RETURN
\b{25}
\endgroup~
\end{array}$$
Empty $\small\texttt{\{}\,\texttt{\}}$ braces
may be disregarded as they are nonfunctional
and only highlight trailing spaces.
. - - - WRUNG OUT - - -
$$\require{begingroup}\begingroup
\def\@#1\endgroup{#1\@
\endgroup0}
\def\binary#1#2
#3\endgroup{\def\N##1#1 ##2 ##3 ##4 ##5 ##6 ##7
##8 ##9
{##8 ##3#2
#3##6\endgroup##2}
\N \binary \binary \@ 1
#3 \@
9 {}
8 7 6 5 4 3 2 1 0 % \binary {}
\binary #3 #2 \@
0
#3#3#3#3 #3 #1 #1 {}
\binary {}
}
\binary{19}
\endgroup$$
.
. - - - AIRED OUT - - -
$$\require{begingroup}\begingroup
%
%
% \@{} prepends 0, as a remainder while halving,
% to the result that follows \endgroup
%
\def \@ #1\endgroup{
#1\@
\endgroup0}
%
%
% \binary{} has a decimal-to-unary accumulation stage
% and a unary-to-binary halving stage
%
\def \binary #1#2
#3\endgroup{
\def \N ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
##8 ##9
{
##8 ##3#2
#3##6\endgroup##2}
\N \binary \binary \@ 1
#3 \@
9 {}
8 7 6 5 4 3 2 1 0 % \binary {}
\binary #3 #2 \@
0
#3#3#3#3 #3 #1 #1 {}
\binary {}
}
%
%
\binary{19}
\endgroup$$
. - - - PARAMETERS - - -
.
. leading "digit's" current value #1 1 - 9 (to be decremented)
. 0 (for quintupling or transition to halving)
. % (for doubling)
. \binary (to be halved for being consecutive)
. \@ (when remainder=1)
. subsequent "digits" #2 remaining digits to accumulate
. OR instances of \binary to be halved
. resulting \binary\binary...\binary #3 (being accumulated OR being halved)
. ##1 (skip to current leading digit OR \binary OR \@)
. additional 1 for the result ##2 when leading-digit \@ signals that remainder=1 after halving
. leading digit's next value ##3 decrement OR % OR blank
. ##4,##5 (skip to ##6)
. append to #3 (\binary...\binary) ##6 \binary (as increment or as half of \binary\binary)
. #3#3#3#3 (quintuple)
. #3 (double)
. \@ (when beginning a new round of halving)
. ##7 (skip to ##8)
. next call ##8 \binary (while accumulating)
. empty (while halving)
. 0 (abort for \binary{0})
. ##9 (leftovers)
If the following worksheets don’t make sense, join the club.
They make less sense by the minute to me too,
and any portion may be out of date.
. call #1 #2... | #3... \e ...
. -- ----- ---------------------------- ----
. \binary{0}|\endgroup \b 0 - | - \e
. 0 % - | - \e
.
.
. call #1 #2... | #3... \e ...
. ---- -- ----- ---------------------------- ----
. \binary{12}|\endgroup \b 12 - | - \e
. \binary 12 |\endgroup \b 1 2 | - \e
. \b 0 2 | \b \e
. \b % 2 | \b\b\b\b\b \e
. \b - 2 | \b\b\b\b\b\b\b\b\b\b \e
. ...:
. :
. \b 2 - | \b\b\b\b\b\b\b\b\b\b \e
. \b 1 - | \b\b\b\b\b\b\b\b\b\b\b \e
. \b 0 - | \b\b\b\b\b\b\b\b\b\b\b\b \e
. - % - | \b\b\b\b\b\b\b\b\b\b\b\b \@| \e
. ..........................: :
. : call #1 #2... .............................:
. : ---- -- ----------------------- :
. % | \b \b \b\b\b\b\b\b\b\b\b\b \@ | - \e
. - \b \b \b\b\b\b\b\b\b\b \@ | \b \e
. - \b \b \b\b\b\b\b\b \@ | \b\b \e
. - \b \b \b\b\b\b \@ | \b\b\b \e
. - \b \b \b\b \@ | \b\b\b\b \e
. - \b \b \@ | \b\b\b\b\b \e
. - \@ - | \b\b\b\b\b\b \e
. \b\b\b\b\b\b \@| \e 0
. ...................................: :
. : .................:
. : :
. \b \b \b\b\b\b \@ | - \e 0
. - \b \b \b\b \@ | \b \e 0
. - \b \b \@ | \b\b \e 0
. - \@ - | \b\b\b \e 0
. \b\b\b \@| \e 00
. ...................................: :
. : ...........:
. : :
. \b \b \b \@ | - \e 00
. - \b \@ - | \b \e 00
. - - | \b \@| \e 100
. ..................................: :
. : .......:
. : :
. | \b \@ - | - \e 100
. - - - | - \e 1100
.
.
. d: c n #2 #3 a b
.
. #1: \b #1 (-) | (-) - \e -
. 2: \b 1 (#2/-) | (#3/-) \b \e -
. 1: \b 0 (#2/-) | (#3/-) \b \e -
. 0: \b % (#2) | (#3/-) #3#3#3#3 \e -
. %: \b - (#2) | (#3/-) #3 \e -
.
. (instant 0, blank #2,#3) 0: 0 % - | - - \e -
. (begin halving, blank #2) 0: - % - | (#3) \@| \e -
.
. \b: - - (#2) | (#3/-) \b \e -
. \@: - - (-) | (#3) \@| \e 1
. (all done, blank #3) \@: - - (-) | - - \e 1
.
.
. Un-encapsulate \stars{19} #1,,#1,,,,,,,,,{}|,\b,{}|,,,,,,,,,,}
. to be \stars 19 . ''db__nnsssttta..|f__c
.
. Decrement a leading-digit 9. 9,{}|,.8,,7 . . . 1..0..%,,,,,,\b,{}|,\b,#3.#2..\@|.0|...#3#3#3#3...#3..#1..#1.........{}|.\b.{}|,,,,,,,,,,}
. 'd___b__nn...............sssttt__a..|f__c.......................................................|zzzzzzzzzz
.
. Decrement a leading-digit 1 1,,0,,%,,,,,,\b,{}|,\b,#3.#2..\@|.0|...#3#3#3#3...#3..#1..#1.........{}|.\b.{}|,,,,,,,,,,}
. 'db_nn.sssttt__a..|f__c.......................................................|zzzzzzzzzz
.
. More decimals, quintuple 0,,%,,,,,.\b.{}|.\b..#2..\@|.0|,,,#3#3#3#3,..#3..#1..#1.........{}|,\b,{}|,,,,,,,,,,}
. (nonempty #2, maybe empty #3). 'db_nnsss......................ttt________a.......................|f__c..|zzzzzzzzzz
.
. More bits 0,,%,,,,,.\b.{}|.\b.#3,,,\@|,0|,,.#3#3#3#3...#3..#1..#1.........{}|,\b,{}|,,,,,,,,,,}
. (empty #2, nonempty #3). 'db_nnsss.............ttt___a.|fc........................................|zzzzzzzzzz
.
. All done (empty #2,#3). 0,,%,,,,,.\b.{}|.\b,,,,\@|.0|,,.#3#3#3#3...#3..#1..#1.........{}|,\b,{}|,,,,,,,,,,}
. 'db_nnsss..........ttta..|f__c.........................................|zzzzzzzzzz
.
. Double (nonempty #2, maybe empty #3). %,,,,..\b.{}|.\b..#2..\@|.0|,,,#3#3#3#3...#3..#1..#1.........{}|,\b,{}|,,,,,,,,,,}
. 'dbnn.......................sss........ttt__a..................|f__c..|zzzzzzzzzz}
.
. \N \b.,,,,,,,,,\b,\@.1|,,.....#3...\@| 9 {}| 8 7 . . . 1 0 % \b {}| \b #3 #2 \@| 0| #3#3#3#3 #3 #1 #1 {}| \b {}|,,,,,,,,,,}
. ''dbnnsssttt__a....|fc...............................................................................................................|zzzzzzzzzz
.
. New round of \@,1|,,,,,,.#3,,,\@|,.9.{}|,,8 7 . . . 1 0 % \b {}| \b #3 #2 \@| 0| #3#3#3#3 #3 #1 #1 {}| \b {}|,,,,,,,,,,}
. halving. ''d__bnnsss...ttt___a.....|fc.........................................................................................|zzzzzzzzzz
.
. All done \@,1|,,,,,,,,,,\@|,,9 {}| 8 7 . . . 1 0 % \b {}| \b #3 #2 \@| 0| #3#3#3#3 #3 #1 #1 {}| \b {}|,,,,,,,,,,}
. (empty #3). ''d__bnnsssttta..|fc................................................................................................|zzzzzzzzzz
.
.
. ##1, 'd digit = 1 d+b,n,a,f,c+x LT s --> 3 LE s
. ##2, _b bit = 1
. ##3,, _nn next digit = 2 a,f,c+x LT t LT d+b+n --> 3 LE t
. ##4,,, sss skip1 = 3
. ##5,,, ttt skip2 = 3 t LE x+y LT t+a LE c+x+y
. ##6, __a append = 1
. ##7|, |f further = eol+1 a LT f+c LT t+a,b+n
. ##8, __c call = 1
. ##9|,,,,,,,,,, |zzzzzzzzzz cleanup = eol+10 b LE f+c
. . conditional spacer for 0 x = 1
. .. conditional spacer for 0 y = 2 n+s+t+a LT z
.
. #1d b #1n s t a -{}|-->f \b c {}|z
. 9d {}| b 8n -7--6- - -2----1----0-----%-->s t \b a -{}|-->f \b c -#3---#2---\@|------0|--------#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
. 2d b 1n --0-----%-->s t \b a -{}|-->f \b c -#3---#2---\@|------0|--------#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
. 0d b -%n -->s -\b----{}|-----\b----#3---->t \@| a ---0|-->f c -#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
. 1d b 0n ---%-->s t \b a -{}|-->f \b c -#3-x-#2-y-\@|------0|--------#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
. 0d b -%n -->s -\b----{}|-----\b---------#2-y-\@|------0|---->t #3#3#3#3 a --#3----#1----#1---------{}|-->f \b c {}|z
. 0d b -%n -->s -\b----{}|-----\b-------->t a -\@|--> f 0| c -----#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
. %d b n --\b----{}|-----\b---------#2-y-\@|------0|---->s -#3#3#3#3-->t #3 a -#1----#1---------{}|-->f \b c {}|z
. \bd b n s t \b a -\@--1|-->f c -----#3-----\@|-->f c -9-(d)-{}|--------8--7- - -#3---#2---\@|------0|--------#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
. \@ 1| b n s -(a)-#3-->t \@| a -----9-(d)-{}|-->f c -8--7- - -#3---#2---\@|------0|--------#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
. \@d 1| b n s t a --------\@|-->f c -9-(d)-{}|--------8--7- - -#3---#2---\@|------0|--------#3#3#3#3-----#3----#1----#1---------{}|-->f \b c {}|z
.
. b LE f+c LT b+n a LT t d+b,n LT s a,f,c+x LT s,t a LT f+c LT t+a
. a LT t LT d+b+n x+y LT t+a LE c+x+y n+s+t+a LT z
. t LE x+y
Initial solution:   This road to
$\texttt{\binary}$ goes through
$\texttt{\unary}$   (cleaned up)
The stripped down version would have 84 81 code tokens
in 3 static macros and 2 dynamic macros,
In the spirit of plagiarism solidarity,
Davide Cervone’s
solution’s
thrifty and nifty use of
$\small\texttt{#1}$ as a self-serving default digit-match,
as adapted to
$\small\texttt{#2}$ here, saved 3 code tokens for
$\small\texttt{\binary{}}$ and reduced from 4 to 3
the number of additional tokens needed for each increment of output base.
$$\require{begingroup}\begingroup
\def \binary #1{
\unary #1
10 }
\def \unary #1#2
#3 {
\def \U ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
{
##3 \unary ##4#2
##6#3 }
\U 9 8 7 6 5 4 3 2 1 0 #2 % * \halvesies #3#3#3#3 #3
}
\def \halvesies #1#2#3 #4 {
\def \H ##1#2
##2 ##3 ##4
##5 {
##3 \halvesies
##4#2 }
\H *
#3 #4
#2
#4 %
#410
}
\binary{19}
\endgroup$$
Traced:
$$\require{begingroup}\begingroup
\def \G { \color{#888} }
\def \Eol {{ \large \kern1mu \raise-.3ex\G{\unicode{8629}}\kern2mu }}
\def \Par #1{ \rlap{ \kern2mu\texttt {#1} }
\G{\raise-.2ex\underline{\hphantom{ \texttt{#1}\, }}} }
\def \RETURN
#1
#2\strut{ \texttt{#1} \Eol \texttt{#2} \\[-.5ex] \normalsize
#1
#2\strut }
%
%
\def \binary #1{
\small \texttt{\binary{#1}} & & \longrightarrow & \RETURN
\unary #1
10 }
\def \unary #1#2
#3 {
\def \U ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
{
\scriptsize \texttt{\unary} \, \Par {#1}
\, \Par {#2}
\Eol \Par {#3}
\kern-3.8em & & \longrightarrow & \RETURN
##3 \unary ##4#2
##6#3 }
\U 9 8 7 6 5 4 3 2 1 0 #2 % * \halvesies #3#3#3#3 #3
}
%
\def \halvesies #1#2#3 #4 {
\def \H ##1#2
##2 ##3 ##4
##5 {
\scriptsize \texttt{\halvesies} \, \Par {#1}
\, \Par {#2}
\, \Par {#3}
\kern-3.8em & \Par{#4}
& \longrightarrow & \RETURN
##3 \halvesies
##4#2 }
\H *
#3 #4
#2
#4 %
#410
}
%
\scriptsize\begin{array}{lrl}
\binary{19}
\strut
\end{array}
\endgroup$$
Septenary
MathJax allows ready extension of this earlier version
up to output base 7, where
$\small\texttt{\septenary{19}} = \texttt{25}$, using 96 (99 with empty
$\small\texttt{\{}\,\texttt{\}}$ braces) replacement code tokens.
$$\require{begingroup}\begingroup
\def \septenary #1{
\unary #1
6543210 }
\def \unary #1#2
#3 {
\def \U ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
{
##3 \unary ##4#2
##6#3 }
\U 9 8 7 6 5 4 3 2 1 0 #2 % * \seventhsies #3#3#3#3 #3
}
\def \seventhsies #1#2#3#4#5#6#7#8 #9 {
\def \H ##1#7
##2 ##3 ##4
##5 {
##3 \seventhsies
##4#7 }
\H *
#8 #9
#7
#9 %
#96543210
}
\septenary{19}
\endgroup$$
Traced:
$$\require{begingroup}\begingroup
\def \G { \color{#888} }
\def \Eol {{ \large \kern1mu \raise-.3ex\G{\unicode{8629}}\kern2mu }}
\def \Par #1{ \rlap{ \kern2mu\texttt {#1} }
\G{\raise-.2ex\underline{\hphantom{ \texttt{#1}\, }}} }
\def \RETURN
#1
#2\strut{ \texttt{#1} \Eol \texttt{#2} \\[-.5ex] \normalsize
#1
#2\strut }
%
%
\def \septenary #1{
\small \texttt{\septenary{#1}} & & \longrightarrow & \RETURN
\unary #1
6543210 }
\def \unary #1#2
#3 {
\def \U ##1#1 ##2 ##3 ##4 ##5 ##6 ##7
{
\scriptsize \texttt{\unary} \, \Par {#1}
\, \Par {#2}
\Eol \Par {#3}
\kern4.5em & & \longrightarrow & \RETURN
##3 \unary ##4#2
##6#3 }
\U 9 8 7 6 5 4 3 2 1 0 #2 % * \seventhsies #3#3#3#3 #3
}
%
\def \seventhsies #1#2#3#4#5#6#7#8 #9 {
\def \H ##1#7
##2 ##3 ##4
##5 {
\scriptsize \texttt{\seventhsies} \, \Par {#1}
\, \Par {#2}
\, \Par {#3}
\, \Par {#4}
\, \Par {#5}
\, \Par {#6}
\, \Par {#7}
\, \Par {#8}
\kern-4em & \Par{#9}
& \longrightarrow & \RETURN
##3 \seventhsies
##4#7 }
\H *
#8 #9
#7
#9 %
#96543210
}
%
\scriptsize\begin{array}{lrl}
\septenary{19}
\strut
\end{array}
\endgroup$$