当前位置:网站首页>实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
2022-07-05 08:16:00 【懒笑翻】
实例003:完全平方数
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
程序分析:因为168对于指数爆炸来说实在太小了,所以可以直接省略数学分析,用最朴素的方法来获取上限,
n = 0
while (n + 1) ** 2 - n * n <= 168:
n += 1
print('----n=', n) # 可以得出范围是在(n+1)**2内
思路是:
最坏的结果是n的平方与(n+1)的平方刚好差168,由于是平方的关系,不可能存在比这更大的间隙。
设这个数是i (i+100)**0.5 (i+100+168)**0.5 则结果都是整数。
至于判断是否是完全平方数,最简单的方法是:平方根的值小数为0即可。
for i in range((n + 1) ** 2):
if i ** 0.5 == int(i ** 0.5) and (i + 168) ** 0.5 == int((i + 168) ** 0.5):
print(i - 100)
结合起来就是:
# 最坏的结果就是n的平方与(n+1)的平方刚好差168,不可能存在比这更大的间隙,我们据此先得出n
n = 0
while (n + 1) ** 2 - n * n <= 168:
n += 1
print('----n=', n) # 可以得出范围是在(n+1)**2内
for i in range((n + 1) ** 2):
if i ** 0.5 == int(i ** 0.5) and (i + 168) ** 0.5 == int((i + 168) ** 0.5):
print(i - 100)
------n= 84
-99
21
261
1581
因此该数可以是:-99 21 61 1581
边栏推荐
- Detailed explanation of SQL server stored procedures
- 动力电池UL2580测试项目包括哪些
- Makefile application
- OC and OD gate circuit
- STM32 --- configuration of external interrupt
- Network communication process
- Weidongshan Internet of things learning lesson 1
- Programming knowledge -- basis of C language
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- Drive LED -- GPIO control
猜你喜欢
How to copy formatted notepad++ text?
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Summary of SIM card circuit knowledge
Drive LED -- GPIO control
The firmware of the connected j-link does not support the following memory access
Introduction of air gap, etc
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Programming knowledge -- assembly knowledge
Embedded composition and route
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
随机推荐
Wifi-802.11 negotiation rate table
Classic application of MOS transistor circuit design (2) - switch circuit design
Soem EtherCAT source code analysis II (list of known configuration information)
Communication standard -- communication protocol
Process communication mode between different hosts -- socket
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
NTC thermistor application - temperature measurement
UE像素流,来颗“减肥药”吧!
UEFI development learning 4 - getting to know variable services
Reasons for rapid wear of conductive slip rings
STM32 --- configuration of external interrupt
Hardware 3 -- function of voltage follower
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
My-basic application 1: introduction to my-basic parser
Synchronization of QT multithreading
C language enhancement -- pointer
OLED 0.96 inch test
STM32 outputs 1PPS with adjustable phase
Consul installation