固定斐波那契数的范围为1至50 python
我有一组从1到50的数字,从这些数字中我必须找到fibonacci编号,之后我必须找到以下数字1和2
。 ,1、2、3、5、8、13、21、34我写了一个代码,但我无法添加问题的最后一部分,因此它发现以下哪个数字包含一个数字1或2他们。 这是我的代码,
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s * s == x
def isFibonacci(n):
return isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4)
for i in range(0, 51):
if isFibonacci(i) == True:
print(i, "is a Fibonacci Number")
else:
print(i, "is a not Fibonacci Number ")
for q in range(1, 51):
if isFibonacci(i) == True and '1' in i and '2' in i:
print(q)
else:
print('Error')
结果是这样的是给我所有这些数字0、1、2、3、5、8、13、21、34作为fibonacci,其余的不是完美的,但是它不断给我错误,我想做的是编写一个循环,其中包含所有类似fibonacci的数字,类似于这些数字0、1、2、3、5、8、13、21、34,并且要打印的数字中包含一个1和2出去,但它只是将所有内容打印为错误。
I have a set of numbers from 1 to 50 and from these numbers I have to find which are Fibonacci number, after that I have to find which of these numbers contain the number 1 and 2.
For example in my code my Fibonacci numbers are 0, 1, 2, 3, 5, 8, 13, 21, 34 I have written a code, but I can't add the last part of the question, so it finds out which of these numbers contain a number 1 or 2 in them.
here is my code
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s * s == x
def isFibonacci(n):
return isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4)
for i in range(0, 51):
if isFibonacci(i) == True:
print(i, "is a Fibonacci Number")
else:
print(i, "is a not Fibonacci Number ")
for q in range(1, 51):
if isFibonacci(i) == True and '1' in i and '2' in i:
print(q)
else:
print('Error')
the result is something like this is gives me all of these numbers 0, 1, 2, 3, 5, 8, 13, 21, 34 as Fibonacci and the rest are not which is perfect but then it keeps giving me error, what i wanted to do is it to write a loop that contains all Fibonacci numbers like these 0, 1, 2, 3, 5, 8, 13, 21, 34 and that contain a 1 and 2 in the number to be printed out but it just prints everything as Error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
q
,而不是i
。str(...)
将数字转换为字符串。q
, noti
.str(...)
.