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.

I'm doing a project euler problem (http://projecteuler.net/problem=40) that requires iteration of each digit of a set of increasing integers, in order.

I solved it by converting each integer to a string and taking the 0 index, but I'd like to know if there's a better, numerical algorithm to find the most significant digit of an arbitrarily long given integer.

Example: Given 3918287712, to easily get both (3) and find the power of 10 (10^10) that corresponds to it. I'd really like pointers to mathematical literature as well - I'm trying to increase my own knowledge of math and algorithms.

share|improve this question
I do not understand. For example, are you trying to find the most significant digit of $\pi^{100000}$ (which is tough), or you have a predetermined and stored number, and you are trying to find the most significant digit of that. If the question is the latter, then how that number is stored? –  Lord Soth Jul 6 '13 at 17:17
Yes, I have a set of predefined numbers, in this case they're stored in memory as a set of long, signed integers. –  Kylar Jul 7 '13 at 14:32

1 Answer 1

Arithmetically, the number of digits of a positive integer $N$ is $d=\lfloor \log_{10} N\rfloor + 1$. For example, $\log_{10} 3918287712 \approx 9.5931$, so $3918287712$ has $\lfloor 9.5931\rfloor + 1 = 10$ digits.

Once you know the number of digits $d$ of $N$, you can find the first digit $f$ of $N$ by calculating $f = \lfloor N/10^{d-1} \rfloor$. For example, the first digit of $3918287712$ is $\lfloor 3918287712/10^{10-1} \rfloor = \lfloor 3.918287712 \rfloor = 3$.

(In practice, your string-based method might be faster...!)

share|improve this answer
Excellent! I'll give this a go. Like I said, the making it faster is only one aspect, I also really want to learn. –  Kylar Jul 7 '13 at 14:33

Your Answer


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.