当前位置:网站首页>实例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

边栏推荐
- STM32 tutorial triple ADC interleaved sampling
- Tailq of linked list
- STM32 --- serial port communication
- Circleq of linked list
- Naming rules for FreeRTOS
- Weidongshan Internet of things learning lesson 1
- Network communication model -- Network OSI tcp/ip layering
- [trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
- Live555 push RTSP audio and video stream summary (I) cross compilation
- Stablq of linked list
猜你喜欢

1-stm32 operation environment construction

Take you to understand the working principle of lithium battery protection board

QEMU STM32 vscode debugging environment configuration

实例010:给人看的时间

Summary -st2.0 Hall angle estimation

Circleq of linked list

More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!

99 multiplication table (C language)

Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)

Stablq of linked list
随机推荐
Shell script basic syntax
Nb-iot technical summary
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Array integration initialization (C language)
99 multiplication table (C language)
Brief discussion on Buck buck circuit
Adaptive filter
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Negative pressure generation of buck-boost circuit
DCDC circuit - function of bootstrap capacitor
Detailed explanation of SQL server stored procedures
The firmware of the connected j-link does not support the following memory access
Management and use of DokuWiki (supplementary)
Reasons for rapid wear of conductive slip rings
What are the test items of power battery ul2580
Why is 1900 not a leap year
C language # and #
Use indent to format code
STM32 tutorial triple ADC interleaved sampling
Briefly talk about the identification protocol of mobile port -bc1.2