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

边栏推荐
- Circleq of linked list
- Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
- Imx6ull bare metal development learning 1-assembly lit LED
- C WinForm [help interface - send email] - practice five
- NTC thermistor application - temperature measurement
- The firmware of the connected j-link does not support the following memory access
- Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- QEMU STM32 vscode debugging environment configuration
- Explication de la procédure stockée pour SQL Server
猜你喜欢

Hardware 1 -- relationship between gain and magnification

NTC thermistor application - temperature measurement

Count the number of inputs (C language)

Basic embedded concepts

Brief discussion on Buck buck circuit

Management and use of DokuWiki (supplementary)

Summary -st2.0 Hall angle estimation

Stablq of linked list

Why is 1900 not a leap year

PMSM dead time compensation
随机推荐
Programming knowledge -- basis of C language
Simple design description of MIC circuit of ECM mobile phone
Buildroot system for making raspberry pie cm3
STM32 single chip microcomputer - bit band operation
C WinForm [realize the previous and next selection pictures] - practice 7
99 multiplication table (C language)
Halcon's practice based on shape template matching [2]
Semiconductor devices (III) FET
leetcode - 445. 两数相加 II
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
Soem EtherCAT source code analysis I (data type definition)
Fundamentals of C language
Some tips for using source insight (solve the problem of selecting all)
Explain task scheduling based on Cortex-M3 in detail (Part 1)
C language enhancement -- pointer
Bluetooth hc-05 pairing process and precautions
Management and use of DokuWiki
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
How to copy formatted notepad++ text?
DokuWiki deployment notes