Formula for the Extraction of a DigitDate: 06/18/2001 at 17:44:15 From: Jake Subject: Formula for the Extraction of a Digit in a Number Is there a formula to extract any digit of a given number? For example, a formula that could be used to calculate the tens digit of 374, or the thousands digit of 3,289. I know how to do this logically, but I cannot think of any mathematical way to do it. Unfortunately, an algorithm is unacceptable for my purposes. It must be a formula. Is there such a formula? Date: 06/20/2001 at 15:28:34 From: Doctor Toughy Subject: Re: Formula for the Extraction of a Digit in a Number Hi Jake, Thanks for writing to Dr. Math. My understanding of what you want is a formula such that when you are given the number 374 it will give you the value of the tens digit, which in this case equals seven. The formula involves two steps: (1) removing the numbers to the right (which in this case is the units digit or the number 4), and (2) removing the numbers to the left (which in this case is the 100 digit or the number 3). The following is used to extract 37 from 374 by removing the 4: Int(374/10) = 37 Taking the 37 we use the next formula to extract 7, which is our answer: 37 mod 10 = 7 Combining the two formulas, Int(374/10) mod 10 = 7 Let K equal the selected number (in this case 374), and let N = the digit you want extracted such that if N = 1 you would get the 4, when N = 2 you would get the 7, and when N = 3 you would get the 3. Then the formula is: Int(K/10^(N-1)) mod 10 Hope this helps. If you have any more questions about this or any other math topics, please write back. - Doctor Toughy, The Math Forum http://mathforum.org/dr.math/ Date: 06/20/2001 at 17:40:14 From: Jake Subject: Re: Formula for the Extraction of a Digit in a Number Is there any way to do that without rounding? What I really need is a formula that can do that with plain ol' math, no lossy methods like rounding. I really do doubt there is a way to do that, but I'm humoring the possibility just in case. Date: 06/26/2001 at 15:30:29 From: Doctor Ian Subject: Re: Formula for the Extraction of a Digit in a Number Hi, First, Dr. Toughy's algorithm uses truncation, not rounding. Second, since you're just plucking a digit from a number, 'lossiness' isn't really an issue - losing the rest of the number is what you're trying to do! Third, if you feel, ah, unmathematical about using the Int() function, use the floor() function instead, which you can read about here: Math Notation via Email - Karl Hahn http://www.karlscalculus.org/email.html Look near the bottom, in the section called 'Special Functions': http://www.karlscalculus.org/email.html#special I hope this helps. - Doctor Ian, The Math Forum http://mathforum.org/dr.math/ |
Search the Dr. Math Library: |
[Privacy Policy] [Terms of Use]
Ask Dr. Math^{TM}
© 1994-2015 The Math Forum
http://mathforum.org/dr.math/