栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何找到最近的斐波那契数列编号?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何找到最近的斐波那契数列编号?

为什么这种方法行之有效:

这是一种不需要先前计算的方法,因此对于 性能 以及在检查非常大的数字时非常有用。


该程序:

from math import *n = int(input("Enter a number:"))if sqrt(5*n**2+4)%1==0 or sqrt(5*n**2-4)%1==0:    print("Your number is a Fibonacci number!")else:    print("Your number is not a Fibonacci number.")    c = 0    while 1:        c += 1        if sqrt(5*(n+c)**2+4)%1==0 or sqrt(5*(n+c)**2-4)%1==0: print("%s is the closest Fibonacci number to your entry." % str(n+c)) break        if sqrt(5*(n-c)**2+4)%1==0 or sqrt(5*(n-c)**2-4)%1==0: print("%s is the closest Fibonacci number to your entry." % str(n-c)) break

说明:

如果(5 * n ^ 2 + 4)或(5 * n ^ 2-4 – 4)是一个理想平方,则n是斐波那契数。


程序输入/输出

Enter a number: 9999999999Your number is not a Fibonacci number.9999816735 is the closest Fibonacci number to your entry.Enter a number: 9999816735Your number is a Fibonacci number!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/651511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号