博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数字拆分为斐波那契数列_检查数字是否为斐波那契
阅读量:2530 次
发布时间:2019-05-11

本文共 3129 字,大约阅读时间需要 10 分钟。

数字拆分为斐波那契数列

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 
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; int* a=(int*)(malloc(sizeof(int)*n)); //fill the array cout<<"enter elements................\n"; for(int i=0;i

Output (first run)

输出(首次运行)

enter no of elements 6enter elements................ 2310 13 15 21 2 3 13 21above 4 fibonacci numbers are present in the array

Output (second run)

输出(第二次运行)

enter no of elements 5enter elements................ 6711 12 14 no fibonacci number is present in the array

翻译自:

数字拆分为斐波那契数列

转载地址:http://nmxzd.baihongyu.com/

你可能感兴趣的文章
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
Linux下Nginx安装
查看>>
LVM扩容之xfs文件系统
查看>>
Hbase记录-client访问zookeeper大量断开以及参数调优分析(转载)
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>
crash 收集
查看>>
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>