日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数字拆分为斐波那契数列_检查数字是否为斐波那契

發布時間:2023/12/1 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数字拆分为斐波那契数列_检查数字是否为斐波那契 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數字拆分為斐波那契數列

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)的時間復雜度,我們不需要任何其他存儲。

  • .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    用于搜索斐波納契數的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 integerint s=sqrt(k);// only in case of perfect square there //will not be any rounding off errorif(s*s==k)return 1;elsereturn 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;elsereturn 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";elsecout<<"\nno fibonacci number is present in the array"; }int main(){int n;// enter array lengthcout<<"enter no of elements\n"; cin>>n;int* a=(int*)(malloc(sizeof(int)*n));//fill the arraycout<<"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

    數字拆分為斐波那契數列

    總結

    以上是生活随笔為你收集整理的数字拆分为斐波那契数列_检查数字是否为斐波那契的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。