0

In answer to another question, I reported that the square of a string of all ones would yield a number with digits that ascend to the number of original ones and then descend to one again.

For instance (11111)^2=123454321.

The question is, how do I find how many ones there are (how many digits in a number)?

I did a Google search on this question and found many seemingly related questions here but none of the "verbs" in the answers were valid in MathJax on math stack exchange.

UPDATE: I'm told that the word is "control sequence". I would like a control sequence that can do what I do in EXCEL such as

=len(5 * 13 * 17) or =len(1105) where the result is 4.

I am familiar with TEX (all caps in a TTY environment) under the CGOS operating system but I do not need or want this for procedural programming. My needs are for page layout and nothing more.

I need something that will work in both MathJax and TeXShop.

poetasis
  • 219
  • 2
    How is that related to TeX & friends? –  Jan 18 '21 at 05:31
  • MathJax is a form of LaTeX use on math stack exchange. I would like to use something like \length{121212} and have it return a 6. What "verb" should I use instead of \length ? – poetasis Jan 18 '21 at 05:41
  • \length{121212} also does not work in TeXShop which operates with MacTeX to compile LaTeX into a PDF under OS X. I may want to use it for a math paper I am writing. Not knowing either Java or Pascal, the word "verb" made sense to me like \times to generate the symbol X in a formula. – poetasis Jan 18 '21 at 05:47
  • I'm not looking to use \length. What I want is a "control sequence" that will give me the number of digits in a number. – poetasis Jan 18 '21 at 06:18
  • 2
    Well, you're not understanding that MathJax and TeX aren't the same thing and what works in TeX surely won't work in MathJax. Either you're confused or stubborn. –  Jan 18 '21 at 06:40
  • 3
    The line added to your question above is not minimal TeX example. Your intend is not clear. If you don't want to process a document by TeX then the question is off topics in this site. If you do want to process something by TeX, give a minimal example of TeX document. – wipet Jan 18 '21 at 07:32
  • I just wanted something that does what I do easily in EXCEL. If it's not available in MathJax or MacTeX, that answers the question. I can't do it. As for being confused or stubborn, I perceive myself to be the former because this TeX environment is new to me and I still don't understand some of the hoops I've had to jump through to get a document going at all. I can complete my math paper with what I have. I've learned a lot and I appreciate the help I've gotten here on other questions but I think this question should now be closed. Thanks for letting me know that this can't be done. – poetasis Jan 18 '21 at 07:53
  • I still don't understand your comparison with EXCEL. You type somewhere in EXCEL length(11111) and then you process it by EXCEL and you get 5. If you type \length{11111} into a TeX document and use my definitions and then process it by TeX, you get 5 in the PDF output. Where is a difference? – wipet Jan 18 '21 at 08:00
  • 1
    note mathjax does not use tex at all: it is javascript so in that case you could use javascript string operators or take the base 10 log but that is off topic on this site. – David Carlisle Jan 18 '21 at 09:11
  • 5
    I’m voting to close this question because it is not a TeX related problem – Elad Den Jan 18 '21 at 09:29
  • 1
    MathJax's list of Supported TeX/LaTeX commands includes \newcommand (or \def) actually, but that's about it: it doesn't include anything for counting or checking for empty string or anything needed for nontrivial macros. – ShreevatsaR Jan 18 '21 at 18:44

2 Answers2

2

If you don't have the "word" \length (we say the "control sequence"), then you can define it using TeX primitives:

\def\length#1{\lengthA0#1\relax}
\def\lengthA#1#2{\ifx#2\relax\the\numexpr#1\relax\else\afterfi{\lengthA{#1+1}}\fi}
\def\afterfi#1#2\fi{\fi#1}
%test:
\length{1221234}

Note that this cannot work in MathJax, because it is based on real TeX.

wipet
  • 74,238
  • I'm looking for something that will work in MathJax and TeXShop – poetasis Jan 18 '21 at 06:34
  • @poetasis -- TeXshop is a front end to a TeX distribution. TeXshop itself doesn't do any typesetting. Which TeX distribution is installed on your system? – Mico Jan 18 '21 at 06:45
  • I'm using MacTex on a Macbook Pro running OSX 10.15.7 with TeXShop 4.44 as a front end. I don't know the version of MacTeX but it was said to run be the one I need for Intel or ARM and I'm pretty sure my new 16" is still running on Intel. – poetasis Jan 18 '21 at 07:12
  • 1
    @poetasis IMHO, there is no control sequence like \length supported in MathJax. The usage of MathJax at math stack exchange is interactive: you are writing and the result is shown immediatelly. It is not intended for batch document processing. So, why do you need the \length at math stack exchange when you can count the digits by your eyes during writing the input? – wipet Jan 18 '21 at 07:14
  • @wipet It was in connection with an Answer I gave to a Question on MSE. I would like to have told the poster how to predict the final result of his requested formula by showing him how to find the number of ones in the string he began so for example f(n)=(\len(1111))^2=1234321. – poetasis Jan 18 '21 at 07:19
  • @poetasis I'm using Poineer receiver VSX-D514 but IMHO this has nothing to do with TeX:). I don't know what TeXShop is, but if it is a front end to TeX for processing documents by TeX then my code mentioned above must work. If not, give a minimal examaple of your TeX document. – wipet Jan 18 '21 at 07:20
2

The number of digits of the nonnegative integer n is the ceiling of the base 10 logarithm of n + 1.

Yes, 0 has zero digits.

\documentclass{article}
\usepackage{xfp}

\NewExpandableDocumentCommand{\digitlength}{O{10}m}{% \fpeval{ ceil(ln(#2+1)/ln(#1)) }% }

\ExplSyntaxOff

\begin{document}

\digitlength{(11111)^2}

\digitlength{123454321}

\digitlength[2]{(11111)^2}

\digitlength[2]{2^(32)-1}

\end{document}

enter image description here

No chance to get anything like this in MathJax: it is not a programming language. Either you're able to implement this in JavaScript and teach MathJax to use this or you're out of luck.

egreg
  • 1,121,712