Get most significant digit python

Assuming you're only dealing with positive numbers, you can divide each number by the largest power of 10 smaller than the number, and then take the floor of the result.

>>> from math import log10, floor
>>> lst = [34523, 55, 65, 2]
>>> [floor[x / [10**floor[log10[x]]]] for x in lst]
[3, 5, 6, 2]

If you're using Python 3, instead of flooring the result, you can use the integer division operator //:

>>> [x // [10**floor[log10[x]]] for x in lst]
[3, 5, 6, 2]

However, I have no idea whether this is more efficient than just converting to a string and slicing the first character. [Note that you'll need to be a bit more sophisticated if you have to deal with numbers between 0 and 1.]

>>> [int[str[x][0]] for x in lst]
[3, 5, 6, 2]

If this is in a performance-critical piece of code, you should measure the two options and see which is faster. If it's not in a performance-critical piece of code, use whichever one is most readable to you.

$\begingroup$

I'm doing a project euler problem [//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.

asked Jul 6, 2013 at 16:09

$\endgroup$

2

$\begingroup$

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...!]

answered Jul 6, 2013 at 17:39

Greg MartinGreg Martin

65.7k3 gold badges63 silver badges102 bronze badges

$\endgroup$

2

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two positive integers X and Y, the task is to find the MSB of X, in the given base Y.
    Examples:

    Input: X = 55, Y = 3 
    Output:
    Explanation: 
    55 is 2001 in base 3 with first digit as 2.
    Input: X = 123, Y = 10 
    Output:
    Explanation: 
    123 is 123 in base 10 with first digit 1. 

    Approach: Let the task to find 1st digit of X = 1234 in base Y = 10, So to get First digit = 1:  

    Divide 1234 by 1000 
    = X / 103 
    = X / 10Number of Digits in X – 1 
    = X / 10log[X] / log[10] [which is for base 10]  

    For any other base, we can replace 10 with Y. Therefore, we can calculate the first digit of a number X in base Y by using the formula:

    X / Y[log[X]/log[Y]]

    Below is the implementation of the above approach:  

    C++

    #include

    using namespace std;

    void first_digit[int x, int y]

    {

        int length = log[x] / log[y] + 1;

        int first_digit = x / pow[y, length - 1];

        cout

    Chủ Đề