Inspired by Brian Charlesworth

Find the smallest positive integer whose square consists of 5 identical leading digits.

As an explicit example, 14 9 2 = 22201 149^2 = 22201 would be the answer for 3 identical leading digits.


The answer is 10541.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

7 solutions

Calvin Lin Staff
Jul 30, 2015

[This is not a complete solution]

Let's consider how to generalize this problem to finding the square with n n identical leading digits.

For those of you who decided to do a brute-force search from 1, 2, 3, it will take you O ( 1 0 n ) O ( 10 ^n) searches to get to the answer, which is not very appealing. Instead, let's apply math to this problem.

First, let's not care about finding the smallest. Let's find all such numbers (or at least categorize them). We want to find a k k and a initial repeating digit d d such that

d d d d 000 k 2 < d d d ( d + 1 ) 000 \overline{dddd000 \ldots } \leq k^2 < \overline{ddd(d+1)000 \ldots}

Let's focus on the case of d = 1 , n = 5 d = 1, n = 5 .
If k k has 2 m 2m digits, then we can conclude that
0.11111 × 1 0 m k < 0.11112 × 1 0 m \sqrt{0.11111} \times 10^m \leq k < \sqrt{0.11112} \times 10 ^ m . This bounding tells us that the smallest possible value of m m for the first case occurs when the values 0.11111 \sqrt{0.11111} and 0.11112 \sqrt{0.11112} differ in their decimal representation. Thus, the smallest answer would be 10541.
If k k has 2 m + 1 2m+1 digits, then we can conclude that 1.1111 × 1 0 m k < 1.1112 × 1 0 m \sqrt{1.1111} \times 10^m \leq k < \sqrt{ 1.1112} \times 10 ^ m . Similar to the above, we can conclude that the smallest answer would be 33334.


We repeat this for all 9 possible values of d d , and we are done.

1000 can also be d aswer

Akarsh Kumar Srit - 5 years, 4 months ago

Log in to reply

100 0 2 1000^2 does not have 5 identical leading digits. It has 5 identical trailing digits.

Calvin Lin Staff - 5 years, 4 months ago

Brute force is actually only O(n). I did bf it with just a calculator. You just need to calc the square root of ddddd0... , round the result to the next bigger int and square that number.

devilchn X - 4 years, 8 months ago

typos 1. In general bound for k k d must be 5 times and 4 times resoectively 2. Assumption would be if k k has 2m or 2m +1 digits

Daanish bansal - 2 years, 11 months ago

Log in to reply

Hm, I'm not quite sure what you mean. Can you elaborate?

  1. What do you mean by bound for k 2 d k^2 d must be 5 times and 4 times respectively?
  2. What assumption are you referring to? Notice that I split out into 2 cases of "If k has 2m digits" and "If k has 2m+1 digits".

Calvin Lin Staff - 2 years, 11 months ago

This is my java solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public static void main(String[] arg){
    int i=1000;
    while(d(i).replaceAll(d(i).substring(0, 1),"").length()!=0){
        i++;    
    }
    System.out.println(i);
}

private static String d(int i) {
    long a=i*i;
    return String.valueOf(a).substring(0,5);        
}

0,455 s run time

Anu Sood
Apr 29, 2016

Brute force is "relatively" easy. We know square of answer is at least 11,111. So it could be 11,111n where n is an integer between 1 and 9, inclusive. None of these work so then try 111,110n + p where p is an integer between 0 and 9, inclusive (n as previous). Continue with 1,111,100n + q where q is an integer between 0 and 99, inclusive (n as previous), etc. You will find the first perfect square with 5 identical digits at 111,112,681. Q.E.D. (well kind of)!

Hint: I actually did it backwards, e.g. taking sequential numbers in local proximity to square roots to see if squares had the requisite 5 digits.

But what if you could find a perfect square of the form 22222x? This number will be smaller than 111,112,681

Steven Gottlieb - 3 years ago
Manojkumar P
Apr 19, 2016
1
2
3
4
5
for x in range(100,99999):
             a=str(x*x)
             if(a[0]==a[1]==a[2]==a[3]==a[4]):
                     x
                     break

Execution time : 0.18s

Menachem Avinoam
Jan 5, 2016

I solved it in my JavaScript console with:

for(var i=0;i<1000000;i++)with({n:Math.pow(i,2)})if(n>10000&&/^1{5}|^2{5}|^3{5}|^4{5}|^5{5}|^6{5}|^7{5}|^8{5}|^9{5}/.test(n)){console.log(i+'*'+i+'='+n);break;}

Could also write the regular expression like this:

for(var i=0;i<1000000;i++)with({n:Math.pow(i,2)})if(n>10000&&/^(\d)\1{4}/.test(n)){console.log(i+'*'+i+'='+n);break;}

This takes JavaScript a split second to solve.

Abdelhamid Saadi
Jul 31, 2015

This is a brute-force solution in python 3.4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def kleadident(n, k):
     return len(str(n))>= k and len(set(str(n)[0:k])) == 1

def solve(k):
    "Inspired by Brian Charlesworth"
    i = 1
    while not kleadident(i*i, k):
        i += 1
    return i

print(solve(5))

What is the complexity/run-time of the code?

Calvin Lin Staff - 5 years, 10 months ago

Log in to reply

The complexity of this code is exponential in the number of identical leading digits. In my machine the time becomes prohibitive from the value of 10.

Abdelhamid Saadi - 5 years, 10 months ago

Log in to reply

Right, I'm trying to decide if the number of steps is approximately 1 0 n 10 ^n or 1 0 n / 2 10 ^ { n / 2 } . I believe it's the former.

Calvin Lin Staff - 5 years, 10 months ago

Log in to reply

@Calvin Lin From the algorithm itself, we can not know the complexity.

But since we can prove easily that a solution with n n digits always exists.

Then practically the number of steps is approximately be near 1 0 n 10^n

Abdelhamid Saadi - 5 years, 10 months ago
Rudresh Tomar
Jul 30, 2015

I was kinda bored on solving this question on paper so i end up making this java code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public static void main(String args[])
{
    int i=100;

    while(i>=100)
    {
        String k=String.valueOf(i*i);
        if(k.charAt(0)==k.charAt(1))
        {
            if(k.charAt(0)==k.charAt(2))
            {
                if(k.charAt(0)==k.charAt(3))
                {
                    if(k.charAt(0)==k.charAt(4))
                    {
                        System.out.println(i);
                        break;
                    }
                    else
                    {
                        i++;
                        continue;
                    }
                }
                else
                {
                    i++;
                    continue;
                }
            }
            else
            {
                i++;
                continue;
            }
        }
        else
        {
            i++;
            continue;
        }

    }

}

What is the complexity/run-time of the code?

Calvin Lin Staff - 5 years, 10 months ago

If I can find the solution with one line of code in less than 0.1 seconds, why bother? Complexity is O(exp(n)), where n is number of digits.

Also, using first terms 15, 149, 2357, 10541, 57735, 745356, 1490712 you can find more infor here: https://oeis.org/A119998 https://oeis.org/A119511

1
2
bc <<<'for(i=1;1;i++) print i^2," ",i,"\n"' | grep -Em1 '^([0-9])\1{4}'
111112681 10541

Big Chiroptera - 2 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...