數字拆分為斐波那契數列_檢查數字是否為斐波那契

數字拆分為斐波那契數列

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.

搜索算法是線性搜索,但是具有挑戰性的是檢查是否為斐波那契數。

  1. 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.

    蠻力方法是生成斐波那契數列并將其存儲在數組中。 我們需要生成斐波那契數列,直到覆蓋搜索數組的最大元素。 然后,我們需要檢查搜索數組的每個元素是否屬于包含生成的斐波那契數列的新數組。 不用說,強力方法不適用于較大的值,因為復雜度要高得多,并且復雜度還包括斐波那契數列生成,這是此處的附加任務。

  2. 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.

    n5n2 + 45n2-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

數字拆分為斐波那契數列

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377719.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377719.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377719.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

伙伴分配器的一個極簡實現

提起buddy system相信很多人不會陌生&#xff0c;它是一種經典的內存分配算法&#xff0c;大名鼎鼎的Linux底層的內存管理用的就是它。這里不探討內核這么復雜實現&#xff0c;而僅僅是將該算法抽象提取出來&#xff0c;同時給出一份及其簡潔的源碼實現&#xff0c;以便定制擴展…

[USACO3.2.3 Spinning Wheels]

[關鍵字]&#xff1a;模擬 枚舉 [題目大意]&#xff1a;有5個輪子&#xff0c;每個輪子優r個缺口并且會按一定速度不停轉動&#xff0c;問什么時候可以使一條光線射過所有輪子。 // [分析]&#xff1a;從0到1000&#xff08;或其他的&#xff09;枚舉分鐘然后判斷&#xff0c;當…

一、SQLServer2008安裝(帶密碼)、創建數據庫、C#窗體項目測試

一、下載和安裝SQLServer2008 東西太大了&#xff0c;沒法上傳到資源里面&#xff0c;官網其他公眾號都下載可以。 右擊管理員身份 運行setup.exe 這個密鑰不能用的話&#xff0c;也可以去百度其他密鑰 JD8Y6-HQG69-P9H84-XDTPG-34MBB 建議改一下路徑&#xff0c;我這邊修…

python獲取當前日期_Python程序獲取當前日期

python獲取當前日期In the below example – we are implementing a python program to get the current date. 在下面的示例中-我們正在實現一個python程序來獲取當前日期 。 Steps: 腳步&#xff1a; Import the date class from datetime module. 從datetime模塊導入日期類…

【C++grammar】多態、聯編、虛函數

目錄1、多態概念1.多態性有兩種表現的方式2、聯編&#xff08;實現多態&#xff09;1.靜態聯編2.動態聯編3、實現運行時多態1.為何要使用運行時多態&#xff1f;2.如何實現運行時多態3.多態的例子1.調用哪個同名虛函數&#xff1f;2. 用途&#xff1a;可以用父類指針訪問子類對…

一 MVC - HtmlHelper

HtmlHelper類位于System.Web.Mvc.Html之中主要有七個靜態類組成&#xff1a; FormExtensions - BeginForm, BeginRouteForm, EndForm InputExtensions - CheckBox, CheckBoxFor, Hidden, HiddenFor, Password, PasswordFor, RadioButton, RadioButtonFor, TextBox, TextBoxFor …

HDOJ 400題紀念。

剛剛交了1506&#xff0c;無意間瞟到左邊的隨筆數&#xff0c;發現已經401題了&#xff0c;這么說前幾天就400題了啊囧。 昨天還想交到400題就先放放&#xff0c;背單詞的&#xff0c;沒想到那么快。等把USACO那個八皇后寫完吧。人生總是有許多不想做又不得不做的事情。。。 還…

二、用戶登錄和注冊

一、頁面設計 一共四個頁面 主頁面Form1&#xff0c;登錄頁面login&#xff0c;注冊頁面resister&#xff0c;主菜單頁面main_page 系統運行進入Form1&#xff0c;單擊登錄按鈕跳轉到login&#xff0c;數據庫中得存在數據信息且輸入正確才可登錄成功&#xff0c;跳轉到main_pa…

readdir函數_PHP readdir()函數與示例

readdir函數PHP readdir()函數 (PHP readdir() function) The full form of readdir is "Read Directory", the function readdir() is used to read the directory i.e. read the name of the next entry in the directory. readdir的完整形式為“ Read Directory”…

【C++grammar】訪問控制與抽象類與純虛函數

目錄一、訪問控制 (可見性控制)1.private、public、protected關鍵字2.關鍵字示例1、關鍵字對類數據成員訪問的限制3. 公有繼承4. 私有繼承5. 保護繼承6. 私有繼承和保護繼承的區別二、抽象類與純虛函數1.什么是抽象類2.抽象函數/純虛函數3.抽象類示例一、訪問控制 (可見性控制)…

mongodb 如何刪除 字段值為 json對象中的某個字段值

例如&#xff1a; { attributes: { birthday:1988-01-01, name: aq } } birthday是attributes字段的value的一個字段&#xff0c; 我要刪除birthday 用這句話&#xff1a; db.User.update({email:adminlinkris.com},{$unset:{attributes.birthday:}})轉載于:https://www.cnblog…

使用 Spring 的 Web 服務模擬器框架解決方案

http://www.ibm.com/developerworks/cn/web/wa-aj-simulator/index.html轉載于:https://www.cnblogs.com/diyunpeng/archive/2012/02/28/2371390.html

三、上傳織物圖片至SQL Server并提供name進行展示織物照片

一、數據庫的建立 還是在fiber_yy數據庫下創建images表 images表設計如下 二、頁面完善設計 main_page頁面進行功能完善 入庫管理系統 warehousing頁面 庫存查詢系統 query頁面 登錄注冊頁面前面幾個博文已經實現過了&#xff0c;這里就再贅述了&#xff0c;仍是沿用前…

gettype_PHP gettype()函數與示例

gettypePHP gettype()函數 (PHP gettype() function) In PHP, we have a library function gettype() to identify the type of data. The function is primarily used to sanity check the type of data being input in a variable. The function can identify the data into …

ARM MMU工作原理剖析[轉]

一、MMU的產生 許多年以前&#xff0c;當人們還在使用DOS或是更古老的操作系統的時候&#xff0c;計算機的內存還非常小&#xff0c;一般都是以K為單位進行計算&#xff0c;相應的&#xff0c;當時的程序規模也不大&#xff0c;所以內存容量雖然小&#xff0c;但還是可以容納當…

棧與隊列在SGI STL的底層實現

棧 棧提供push和pop等接口&#xff0c;不提供走訪功能&#xff0c;也不提供迭代器。 STL中棧不被歸類為容器&#xff0c;而被歸類為container adapter(容器適配器)&#xff0c;這是因為棧是以底層容器完成其所有的工作&#xff0c;對外提供統一的接口&#xff0c;底層容器是可…

【原創】SharePoint Document library List Check out 文檔時碰到的問題解決

環境&#xff1a;TFS(Team Foundation Server)集成的WSS 3.0&#xff08;SharePoint Service 3.0&#xff09; 問題&#xff1a;如題&#xff0c;祥見下圖 解決&#xff1a;一般碰到沒有經驗的問題&#xff0c;大家當然是外事不決問谷歌了&#xff0c;于是谷歌搜到了這篇博客 h…

getdate函數_PHP getdate()函數與示例

getdate函數PHP getdate()函數 (PHP getdate() function) getdate() function is used to get the local date/time (or it is also used to get the date/time based on the given timestamp. getdate()函數用于獲取本地日期/時間(或也用于根據給定的時間戳獲取日期/時間。 S…

四、入庫管理功能的完善

一、數據庫的創建 在fiber_yy數據庫下創建yy_textile表 先隨便添加幾條數據 二、頁面的完善 登錄注冊頁面我就不演示了&#xff0c;前幾篇博文也都有介紹 warehousing入庫頁面 main_page頁面進行功能完善 三、代碼實現 warehousing頁面 using System; using System.…

leetcode 232. 用棧實現隊列 思考分析

題目 請你僅使用兩個棧實現先入先出隊列。隊列應當支持一般隊列的支持的所有操作&#xff08;push、pop、peek、empty&#xff09;&#xff1a; 實現 MyQueue 類&#xff1a; void push(int x) 將元素 x 推到隊列的末尾 int pop() 從隊列的開頭移除并返回元素 int peek() 返…