0

Let us say you have the number 1234. If we were to "remove" the number 3, we would have 124. I have seen this operation in many programming tutorials, but I do not know what mathematical operation, if any, this maps onto. What is the mathematical term for this option, and how is it written?

  • 5
    "Removing the ten's digit" could be written as $f(n) = 10\cdot \lfloor \frac{n}{100}\rfloor
    • (n% 10)$ where $%$ is the usual computer science meaning of mod (which can also be rewritten in terms of floor functions if one prefers)
    – JMoravitz Jun 12 '20 at 14:09
  • 2
    As for doing this in a programming language... I think it is far easier to read and understand if you were to simply convert the number to a string, then perform string manipulation, then interpret the resulting string as a number again, rather than performing the necessary algebra (especially due to oddities like 0.1+0.2 != 0.3). Unless if you are doing billions of these operations you shouldn't notice a difference. Having the code be readable and understandable is often more useful than unnoticeable improvements in performance. – JMoravitz Jun 12 '20 at 14:14

1 Answers1

2

To remove the $10^d$'s digit from $x$, you can take $x - 10^d (\lfloor x/10^d \rfloor - \lfloor x/10^{d+1} \rfloor)$.

Thus in your example with $x=1234$ and $d=1$, $1234 - 10 (\lfloor 123.4 \rfloor - \lfloor 12.34 \rfloor) = 124$.

Robert Israel
  • 448,999