斐波那契数据类型 [英] Fibonacci datatype

查看:153
本文介绍了斐波那契数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个Fibonacci系列发生器,我不知道我应该使用哪个数据类型。这是我在做什么:




  • 生成Fibonacci系列中的前1000个数字,并将其存储在集合中。 / p>


  • 随机播放上述系列(即拼图元素)并将其存储在新集合中。


  • 通过按照以下规则转换上述2个集合来创建新集合 - 新集合中的每个元素将是位于同一索引中的前2个集合中的各个元素的平均值。 ie newcollection [0] =(original [0] + shuffle [0])/ 2。




我的原始集合和改组的集合应该是 IEnumerable< long> 。平均值集合应该 IEnumerable< double> ,你认为这是正确的吗?我应该使用 IEnumerable< decimal> 用于平均值集合吗?



注意:最终所有集合都被刷新

解决方案



p> IEnumerable< long> 适用于(无限)生成器。



$ c> IEnumerable 接口不保证对元素的随机访问的常数(O(1)),这至少对于shuffling是必需的。因此,您应该将生成器的结果存储到 IList< long> 或简单数组中。





我做了一些研究,看起来即使第100个斐波纳契数字也不能适应一个长的(查看Wolfram alpha )。您必须在此处使用一些大整数数据类型。 ..


I'm writing a Fibonacci series generator and I'm not sure which datatype should I use for it. This is what I'm doing:

  • Generate the first 1000 numbers in a Fibonacci series and store it in a collection.

  • Shuffle the above series (i.e juggle the elements)and store it in a new collection.

  • Create a new collection by transforming the above 2 collections by the following rule- every element in the new collection will be an average of the respective elements in the first 2 collections, residing in the same index. i.e newcollection[0]= (original[0]+shuffle[0])/2.

I have decided that my original collection and the shuffled collection should be an IEnumerable<long>. The averaged out collection should be IEnumerable<double>, do you think this is correct? Should I be using IEnumerable<decimal> for the averaged out collection?

Note:Eventually all the collections are flushed to the console.

Thanks, -Mike

解决方案

The IEnumerable<long> is fine for an (infinite) generator.

However, the IEnumerable interface does not guarantee constant (O(1)) random access to the elements, which is required at least for the shuffling. So you should store the results of the generator into an IList<long> or into a simple array.

EDIT:

I did a little research, and it seems that even the 100th Fibonacci number cannot fit into a long (check Wolfram alpha). You have to use some big integer data type here...

这篇关于斐波那契数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆