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

数字拆分为斐波那契数列

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

数字拆分为斐波那契数列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值