Find the smallest positive integer whose square consists of 5 identical leading digits.
As an explicit example, 1 4 9 2 = 2 2 2 0 1 would be the answer for 3 identical leading digits.
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.
1000 can also be d aswer
Log in to reply
1 0 0 0 2 does not have 5 identical leading digits. It has 5 identical trailing digits.
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.
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
Log in to reply
Hm, I'm not quite sure what you mean. Can you elaborate?
This is my java solution
1 2 3 4 5 6 7 8 9 10 11 12 |
|
0,455 s run time
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
1 2 3 4 5 |
|
Execution time : 0.18s
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.
This is a brute-force solution in python 3.4
1 2 3 4 5 6 7 8 9 10 11 |
|
What is the complexity/run-time of the code?
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.
Log in to reply
Right, I'm trying to decide if the number of steps is approximately 1 0 n or 1 0 n / 2 . I believe it's the former.
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 digits always exists.
Then practically the number of steps is approximately be near 1 0 n
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 |
|
What is the complexity/run-time of the code?
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 |
|
Problem Loading...
Note Loading...
Set Loading...
[This is not a complete solution]
Let's consider how to generalize this problem to finding the square with 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 ) 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 and a initial repeating digit d such that
d d d d 0 0 0 … ≤ k 2 < d d d ( d + 1 ) 0 0 0 …
Let's focus on the case of d = 1 , n = 5 .
If k has 2 m digits, then we can conclude that
0 . 1 1 1 1 1 × 1 0 m ≤ k < 0 . 1 1 1 1 2 × 1 0 m . This bounding tells us that the smallest possible value of m for the first case occurs when the values 0 . 1 1 1 1 1 and 0 . 1 1 1 1 2 differ in their decimal representation. Thus, the smallest answer would be 10541.
If k has 2 m + 1 digits, then we can conclude that 1 . 1 1 1 1 × 1 0 m ≤ k < 1 . 1 1 1 2 × 1 0 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 , and we are done.