數字拆分為斐波那契數列
Description:
描述:
We are often used to generate Fibonacci numbers. But in this article, we are going to learn about how to search Fibonacci numbers in an array?
我們經常被用來產生斐波那契數。 但是在本文中,我們將學習如何在數組中搜索斐波那契數 ?
Introduction:
介紹:
Fibonacci numbers are often used in mathematics and computer science fields. Fibonacci numbers are often considered as an important part of number theory because of their some amazing properties and the connection with the golden ratio. We are all familiar with Fibonacci number generation using dynamic programming or simple Fibonacci property. But to check a number whether it's part of Fibonacci series or not is something really challenging.
斐波那契數通常用于數學和計算機科學領域。 斐波那契數通常被認為是數論的重要組成部分,因為它們具有驚人的性質以及與黃金比率的聯系。 我們都熟悉使用動態編程或簡單的Fibonacci屬性生成斐波那契數的方法。 但是要檢查一個數字是否屬于斐波那契數列確實是一項挑戰。
搜索斐波那契數的算法 (Algorithms to search for Fibonacci numbers)
The searching algorithm is linear search but what is challenging is to check for the number whether Fibonacci or not.
搜索算法是線性搜索,但是具有挑戰性的是檢查是否為斐波那契數。
Brute force
蠻力
The brute force approach is to generate the Fibonacci series and to store that in an array. We need to generate the Fibonacci series till we cover the maximum element of the search array. Then we need to check each element of the search array whether it's part of the new array consisting generated Fibonacci series. Needless to say, the brute force approach is not going to work for larger values since the complexity is much higher and the complexity also includes Fibonacci series generation which is an additional task here.
蠻力方法是生成斐波那契數列并將其存儲在數組中。 我們需要生成斐波那契數列,直到覆蓋搜索數組的最大元素。 然后,我們需要檢查搜索數組的每個元素是否屬于包含生成的斐波那契數列的新數組。 不用說,強力方法不適用于較大的值,因為復雜度要高得多,并且復雜度還包括斐波那契數列生成,這是此處的附加任務。
Using Mathematical formula
使用數學公式
Fibonacci numbers have an amazing property and one of the property is that for every Fibonacci number
斐波那契數字具有驚人的特性,其中一個屬性是每個斐波那契數字
n, 5n2+4 or 5n2-4 is a perfect square.
n , 5n2 + 4或5n2-4是一個完美的正方形。
Such property has made the checking possible in only
這樣的屬性使得檢查僅在
O(1) time complexity and we don't need any additional storage.
O(1)的時間復雜度,我們不需要任何其他存儲。
用于搜索斐波納契數的C ++實現 (C++ implementation for searching Fibonacci numbers)
#include <bits/stdc++.h>
using namespace std;
int isSquare(int k){
// if k isn't perfect square then the square root
//will be a float value but we are rounding it off to integer
int s=sqrt(k);
// only in case of perfect square there
//will not be any rounding off error
if(s*s==k)
return 1;
else
return 0;
}
int checkFibo(int k){
//checking whether (5n^2+4) or (5n^2-4) is perfect square
if(isSquare(5*k*k-4)||isSquare(5*k*k+4))
return 1;
else
return 0;
}
void findFibos(int* a, int n){
int count=0;
for(int i=0;i<n;i++){
if(checkFibo(a[i])){
cout<<a[i]<<" ";
count++;
}
}
if(count)
cout<<"\nabove "<<count<<" fibonacci numbers are present in the array\n";
else
cout<<"\nno fibonacci number is present in the array";
}
int main(){
int n;
// enter array length
cout<<"enter no of elements\n";
cin>>n;
int* a=(int*)(malloc(sizeof(int)*n));
//fill the array
cout<<"enter elements................\n";
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
findFibos(a,n);
return 0;
}
Output (first run)
輸出(首次運行)
enter no of elements
6
enter elements................
2
3
10
13
15
21
2 3 13 21
above 4 fibonacci numbers are present in the array
Output (second run)
輸出(第二次運行)
enter no of elements
5
enter elements................
6
7
11
12
14
no fibonacci number is present in the array
翻譯自: https://www.includehelp.com/algorithms/search-a-fibonacci-number.aspx
數字拆分為斐波那契數列