From the comments on my answer here, the question was asked (paraphrase):
Write a Python program to find a 4 digit whole number, that when multiplied to itself, you get an 8 digit whole number whose last 4 digits are equal to the original number.
I will post my answer, but am interested in a more elegant solutions concise but easily readable solution! (Would someone new-ish to python be able to understand it?)
Here is a 1-liner solution without any modules:
>>> next((x for x in range(1000, 10000) if str(x*x)[-4:] == str(x)), None)
9376
If you consider numbers from 1000
to 3162
, their square gives you a 7
digit number. So iterating from 3163
would be a more optimized because the square should be a 8
digit one. Thanks to @adrin for such a good point.
>>> next((x for x in range(3163, 10000) if str(x*x)[-4:] == str(x)), None)
9376
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With