如果一个数是斐波纳契测试 [英] Test if a number is fibonacci

查看:210
本文介绍了如果一个数是斐波纳契测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使斐波那契数列,但我不知道我怎么能测试一个给定的数字属于斐波那契名单 - 随附心中的一种方式是产生FIB的列表。人数达到这个数字,看看它是否属于阵列,但得是另一种更简单,更快速的方法。

I know how to make the list of the Fibonacci numbers, but i don't know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is generate the list of fib. numbers up to that number and see if it belongs to the array, but there's got to be another, simpler and faster method.

任何想法?

推荐答案

一个非常好的测试是N是一个斐波那契数当且仅当 5 N ^ 2 + 4 5N ^ 2 - 4 是一个平方数。有关如何想法,有效地测试一个数平方米指的是<一href="http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer">SO讨论。

A very nice test is that N is a Fibonacci number if and only if 5 N^2 + 4 or 5N^2 – 4 is a square number. For ideas on how to efficiently test that a number is square refer to the SO discussion.

希望这有助于

这篇关于如果一个数是斐波纳契测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆