找出一个四位数的数字,其平方是8位数字,最后4位数字是原始数字 [英] Find the a 4 digit number who's square is 8 digits AND last 4 digits are the original number

查看:97
本文介绍了找出一个四位数的数字,其平方是8位数字,最后4位数字是原始数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我的答案上的评论中,有人问了这个问题(措辞):

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

如果您考虑从10003162的数字,则它们的平方为您提供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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆