2
$\begingroup$

The leftmost digit of the decimal representations of the natural numbers $2^n$ and $5^n$ is the same. Prove that such digit is equal to $3$.

I wanted to see some pattern in the given question(so as to get the feel of the problem) so I used my programming knowledge(as far it is taught in school).
Here is it:

public class blackadd 
{
    public static void main()
    {int m,n;
        int h=0,l=0;
        for(int i=1;i<=30;i++)
        {n=(int)Math.pow(2,i);
            m=(int)Math.pow(5,i);
            while(n!=0)
            {h=n%10;
                n=n/10;
            
        }
               System.out.println("for 2   :"+h); 
                while(m!=0)
            {l=m%10;
                m=m/10;
            
        }
               System.out.println("for 5  :"+l+"\n"+"-------");   
            if(l==h)
            System.out.println(i);
           
        }}}   

The anomalous output obtained is:

for 2   :2
for 5  :2
-------
28  

where $28$ is $n$ in the question. I could not figure out what is wrong with my program. Please let me know. Further, How can I prove this if it is true?Slight hint would do good.

$\endgroup$
11
  • 1
    $\begingroup$ I think what it means is that when it's equal, it's a 3. $\endgroup$ Commented Jul 3, 2021 at 17:34
  • 1
    $\begingroup$ Thank you, @MartinArgerami. $\endgroup$ Commented Jul 3, 2021 at 17:35
  • 1
    $\begingroup$ My pocket calculator tells me $2^{28}$ and $5^{28}$ have different leading digits (2 and 3 respectively). A rough heuristics which you probably have to make more precise is this. The product of $2^n$ and $5^n$ is $10^n$ which has leading digit $1$. The only possibility is that both of the leading digits are $3$ so you get $3\times 3\approx 10$ or you get $1$ and the leading digit is $\approx 1$. $\endgroup$
    – daruma
    Commented Jul 3, 2021 at 17:36
  • 1
    $\begingroup$ Essentially Java cannot handle integers bigger than a certain limit. So then it wraps back and gives the wrong answer. (Basically, the integers in Java are just integers modulo $N$ for some "big" $N$.) $\endgroup$ Commented Jul 3, 2021 at 17:39
  • 1
    $\begingroup$ All the primitive numeric data types in Java (such as int) have strictly defined limits. See docs.oracle.com/javase/tutorial/java/nutsandbolts/…. $\endgroup$
    – David K
    Commented Jul 3, 2021 at 17:45

1 Answer 1

2
$\begingroup$

A tiny bit of numerical experimentation gives me that the first few $n$ where the first digit of $2^n$ is equal to the first digit of $5^n$ are $$ 5,15,78,88,98,108,118,181,191,201,211,274,284,294,304. $$ In all cases such first digit is a 3. I don't know how to prove the general fact, though. It looks like one should exploit the fact that $2^n5^n=10^n$, but since every digit is possible, it is not obvious to me how to approach a proof.

As for your program, you are using math.pow, which uses double arithmetic, and casting to an int. This makes your program start to fail when i=14. That's when $5^{14}=6,103,515,625$, which is already greater than the highest possible int which is $2,147,483,647$. What Java is doing (which I personally don't think is very nice) is just give you back $2,147,483,647$; that's why your program is incorrectly giving every single first digit for $5^n$, $n\geq14$, as $2$. Here is your program modified to work with double

public class blackadd 
{
    public static void main(String[] args)
    {double m,n;
        long startTime = System.nanoTime();
        int h=0,l=0;
        for(int i=1;i<=440;i++)
        {n= Math.pow(2,i);
            m= Math.pow(5,i);
            while(n>=1)
            {h=(int)(n - 10 * Math.floor(n / 10));
                n=n/10;
            
        }
                while(m>=1)
            {l=(int)(m - 10 * Math.floor(m / 10));
                m=m/10;
            
        }
               System.out.println(i+":   "+h+" --  "+l);   
            if(l==h)
                System.out.println("\n"+"-------"+i);
           
        }
        long endTime   = System.nanoTime();
        long totalTime = (endTime - startTime) / 1000000;
        System.out.println(totalTime);    
    }
    
}   

This should work fine until $n=541$, more or less, which is where $5^n$ reaches the greatest number than can be expressed as a double. Note that % does not work with doubles, so I had to change the way to calculate the digits. I run the program up to $i=440$ because beyond that the platform I was using (codingground) failed; not sure if it is a Java or platform thing.

As a general comment, you would usually not want to use Math.pow to calculate powers of integers, in particular when you are using all the powers through a loop like here. You can initialize n=1 and multiply it by 2 on every iteration. I didn't put this in the code because in this case to extract the digit from a double is harder than to extract the digit from an integer as you are doing. For a double the best way I know is to do

h = Math.floor(n / Math.pow(10,Math.floor(Math.log10(n)))),

which is not particularly efficient with four calls to the Math library.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .