We demonstrate some features of writing TeX macros by embedding a number-theoretic calculator into TeX.$\phi(1001)=\Phi(1001)$ and get on print
\newcount\s\def\addition#1#2{\s=#1\advance\s by#2 \number\s} \newcount\s % \def\addition#1#2{ % \s=#1 % s \advance\s by#2 % \number\s % } 2+3=\addition{2}{3} will give the seal "2 + 3 = 5". \newif\ifdivisible % \newcount\testMod@n % \def\testMod#1#2{ % \testMod@n=#1 % testMod@n \divide\testMod@n by#2 % \multiply\testMod@n by#2 % \ifnum#1=\testMod@n % , \divisibletrue % , \else % \divisiblefalse % \fi% } \testMod{6}{3} \ifdivisible 1 \else 0 \fi \testMod{6}{4} \ifdivisible 1 \else 0 \fi \catcode `\@11 (This is the same macro used in LaTeX under the alias \ makeatletter.) \def\addition{\newcount\s} declare a variable inside a macro to fail: "Forbidden control sequence found while scanning definition of \ a". There may be several roundabout maneuvers, but it is enough for us to agree to call the “local” (actually global) variables as <function name> @ <personal name> and be careful (!). After all of our macros, we will again make @ a special character using \catcode `\@12 which prevents direct access to these variables from a later document.
\newcount\divisorpower % , \newcount\getDivisorPower@m % - n/d^a \def\getDivisorPower#1#2{ \getDivisorPower@m=#1 % \divisorpower=0 % \testMod{\getDivisorPower@m}{#2} % , d \loop\ifdivisible % while-, divisible \advance\divisorpower by1 % a 1 \divide\getDivisorPower@m by#2 % d \testMod{\getDivisorPower@m}{#2} % , d \repeat % } int divisible; int a; int m; void getDivisorPower(int n, int d){ m = n; a = 0; divisible = (m % d == 0); while(divisible){ a++; m /= d; divisible = (m % d == 0); } } 
\newcount\numberpower % \newcount\getNumberPower@pow % - \def\getNumberPower#1#2{ \numberpower=1 % \getNumberPower@pow=#2 \loop\ifnum\getNumberPower@pow>0 % \multiply\numberpower by#1 \advance\getNumberPower@pow by-1 \repeat } int numberpower; int pow; void getNumberPower(int d, int a){ numberpower = 1; pow = a; while(pow > 0){ numberpower *= d; pow--; } } \getDivisorPower{600}{2} \number\divisorpower \getDivisorPower{600}{3} \number\divisorpower \getDivisorPower{600}{5} \number\divisorpower \getDivisorPower{600}{7} \number\divisorpower \getNumberPower{5}{0} \number\numberpower \getNumberPower{6}{1} \number\numberpower \getNumberPower{7}{2} \number\numberpower Source: https://habr.com/ru/post/122659/
All Articles