Python 程序检查给定数字是否为斐波那契数

2024 年 8 月 29 日 | 阅读 3 分钟

在本教程中,我们将学习如何检查给定数字是否为斐波那契数。

这里,我们有一个数字“n”,我们需要检查它是否是斐波那契数。斐波那契数列的起始数字是:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,依此类推。

示例

我们还可以使用斐波那契数的以下属性来检查给定数字是否为斐波那契数:

  • 一个数是斐波那契数当且仅当 (5 * R * R + 4) 或 (5 * R * R - 4) 或两者都是完全平方数。

Python 程序:检查给定数字是否为斐波那契数

输出

Number: 1 : Yes, the given number is a Fibonacci_Number
Number: 2 : Yes, the given number is a Fibonacci_Number
Number: 3 : Yes, the given number is a Fibonacci_Number
Number: 4 : No, the given number is not a Fibonacci_Number
Number: 5 : Yes, the given number is a Fibonacci_Number
Number: 6 : No, the given number is not a Fibonacci_Number
Number: 7 : No, the given number is not a Fibonacci_Number
Number: 8 : Yes, the given number is a Fibonacci_Number
Number: 9 : No, the given number is not a Fibonacci_Number
Number: 10 : No, the given number is not a Fibonacci_Number
Number: 11 : No, the given number is not a Fibonacci_Number
Number: 12 : No, the given number is not a Fibonacci_Number
Number: 13 : Yes, the given number is a Fibonacci_Number
Number: 14 : No, the given number is not a Fibonacci_Number
Number: 15 : No, the given number is not a Fibonacci_Number
Number: 16 : No, the given number is not a Fibonacci_Number
Number: 17 : No, the given number is not a Fibonacci_Number
Number: 18 : No, the given number is not a Fibonacci_Number
Number: 19 : No, the given number is not a Fibonacci_Number
Number: 20 : No, the given number is not a Fibonacci_Number
Number: 21 : Yes, the given number is a Fibonacci_Number

结论

在本教程中,我们讨论了用户如何使用 Python 检查给定数字是否为斐波那契数。