Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Let us say I have an integer of an arbitrary length such as:

$209484250490600018105614048117055336$

Is there an elegant function which allows me to select the $n$-th digit such that:

$f(1) = 6$ $f(2) = 3$ and so forth.

share|improve this question
2  
Please check my question math.stackexchange.com/questions/141919/… –  Mathlover Jan 25 '13 at 6:47

2 Answers 2

up vote 6 down vote accepted

Yes, it can be expressed concisely as $f(n) = \lfloor x \cdot 10^{-n+1} \rfloor \mod{10}$

share|improve this answer

An alternative:

$f(n)={\large\lfloor} 10\cdot\left(10^{-n}\cdot x-\lfloor 10^{-n}\cdot x\rfloor \right){\large\rfloor}$.

share|improve this answer
1  
Iterated floor functions can be simplified, $$f(n)=\lfloor 10^{-n}\cdot x{\large\rfloor}-10\lfloor 10^{-(n+1)}\cdot x{\large\rfloor}$$ –  Ethan Jan 25 '13 at 7:14
    
@Ethan: Thank you. Are you sure it isn't $f(n)=\lfloor 10^{-n+1}\cdot x{\large\rfloor}-10\lfloor 10^{-n}\cdot x{\large\rfloor}$? (Although it would make sense if $f(n)$ corresponded to the coefficient of $10^n$ rather than $10^{n-1}$, I'm using the convention in the question.) –  Jonas Meyer Jan 25 '13 at 7:22
    
My bad you are correct, the function I listed corresponds to the nth power of 10, it should be the new one you listed, where you scaled x by a factor of 10. –  Ethan Jan 25 '13 at 7:30
    
@Ethan: Just thought I'd double check that detail, but the idea is clear and gives another, maybe better way to think of it. Thanks again. –  Jonas Meyer Jan 25 '13 at 7:32
    
Just a remark: I arrived at my form essentially by thinking of clearing the digits to the left first, then those to the right. Similarly, Ethan's form can be thought of as clearing the digits to the right first, then those to the left. –  Jonas Meyer Jan 25 '13 at 17:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.