import math for x in range(0,1000): a = math.sqrt(x+100) b = math.sqrt(x+100+168) a = str(a).split('.') b = str(b).split('.') if a[len(a)-1] == '0'and b[len(b)-1]=='0': print x
在网上查找解题方法,还看到一种简单的方法,先贴出来以作学习:
import math for i in range(10000): x = int(math.sqrt(i + 100)) y = int(math.sqrt(i + 268)) if(x * x == i + 100) and (y * y == i + 268): print i
import math def is_sqr(x): a = math.sqrt(x+100) b = math.sqrt(x+100+168) if abs(a-round(a)) < 0.00000001 and abs(b-round(b)) < 0.00000001: return x print filter(is_sqr,range(1,1000))
import math def is_sqr(x): return math.sqrt(x+100).is_integer() and math.sqrt(x+100+168).is_integer() print filter(is_sqr, range(1, 1000)
同时,在最后贴出给了我思路使用abs方法的源代码:
import math def isPP(n): m=2 res=[] while m<=round(math.sqrt(n)): mid=math.log(n,m) if abs(mid-round(mid))<0.000000001: res.append(m) res.append(int(round(mid))) break m+=1 if len(res)==0:return None return res
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/227896.html原文链接:https://javaforall.net
