测试一个数字是否是斐波那契数 [英] Test if a number is fibonacci

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

问题描述

我知道如何制作斐波那契数列,但我不知道如何测试给定数字是否属于斐波那契数列——我想到的一种方法是生成 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 + 45N^2 – 4 是一个平方数.有关如何有效测试数字是否为平方的想法,请参阅 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天全站免登陆