找出一个四位数的数字,其平方是8位数字,最后4位数字是原始数字 [英] Find the a 4 digit number who's square is 8 digits AND last 4 digits are the original number
问题描述
在在这里我的答案上的评论中,有人问了这个问题(措辞):
From the comments on my answer here, the question was asked (paraphrase):
编写一个Python程序以查找一个4位整数,将其与自己相乘,您将得到一个8位整数,最后4位数字等于原始数字.
我将发布我的答案,但对更简洁的解决方案却更易读的优雅解决方案感兴趣! (请问对Python有新知识的人能够理解它吗?)
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?)
推荐答案
这是一个没有任何模块的 1-liner 解决方案:
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
如果您考虑从1000
到3162
的数字,则它们的平方为您提供7
的数字.因此,从3163
进行迭代将更加优化,因为平方应为8
一位.感谢@adrin这样的好点.
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
这篇关于找出一个四位数的数字,其平方是8位数字,最后4位数字是原始数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!