斐波那契数据类型
我正在编写一个斐波那契数列生成器,但我不确定应该使用哪种数据类型。这就是我正在做的事情:
生成斐波那契数列中的前 1000 个数字并将其存储在集合中。
打乱上述系列(即调整元素)并将其存储在一个新集合中。
通过按照以下规则转换上述 2 个集合来创建一个新集合 - 新集合中的每个元素将是前 2 个集合中各自元素的平均值,并且驻留在同一索引中。即 newcollection[0]= (original[0]+shuffle[0])/2。
我决定我的原始集合和打乱后的集合应该是 IEnumerable
。平均后的集合应该是 IEnumerable
,您认为这是正确的吗?我应该使用 IEnumerable
来计算平均集合吗?
注意:最终所有集合都会刷新到控制台。
谢谢, -麦克风
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IEnumerable
对于(无限)生成器来说很好。但是,
IEnumerable
接口不保证对元素的恒定 (O(1)) 随机访问,而这至少是混洗所必需的。因此,您应该将生成器的结果存储到IList
或简单数组中。编辑:
我做了一些研究,似乎即使是第 100 个斐波那契数也无法放入 long (检查 Wolfram alpha)。您必须在此处使用一些大整数数据类型。 ..
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 anIList<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...
为什么不只使用标准数组? int[1000] 和 double[1000]?
Why don't you use just standard Arrays? int[1000] and double[1000]?
只需使用
List
,因为斐波那契元素是整数。Just use
List<int>
, because Fibonacci elements are integers.