当前位置:网站首页>实例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
边栏推荐
- FIO测试硬盘性能参数和实例详细总结(附源码)
- Management and use of DokuWiki
- Count the number of inputs (C language)
- Development tools -- gcc compiler usage
- 亿学学堂给的证券账户安不安全?哪里可以开户
- Slist of linked list
- Solutions to compilation warnings in Quartus II
- Interview catalogue
- Class of color image processing based on Halcon learning_ ndim_ norm. hdev
- Bluetooth hc-05 pairing process and precautions
猜你喜欢
Hardware 3 -- function of voltage follower
Relationship between line voltage and phase voltage, line current and phase current
C WinForm [help interface - send email] - practice five
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
STM32 single chip microcomputer - bit band operation
Working principle and type selection of common mode inductor
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
Talk about the circuit use of TVs tube
实例010:给人看的时间
99 multiplication table (C language)
随机推荐
STM32 tutorial triple ADC interleaved sampling
Negative pressure generation of buck-boost circuit
Naming rules for FreeRTOS
Live555 push RTSP audio and video stream summary (I) cross compilation
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Introduction of air gap, etc
Network communication model -- Network OSI tcp/ip layering
FIO测试硬盘性能参数和实例详细总结(附源码)
STM32 virtualization environment of QEMU
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Use indent to format code
Hardware 1 -- relationship between gain and magnification
Void* C is a carrier for realizing polymorphism
C WinForm [realize the previous and next selection pictures] - practice 7
Circleq of linked list
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Shell script basic syntax
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine