斐波那契数据类型

发布于 2025-01-07 03:21:34 字数 520 浏览 2 评论 0原文

我正在编写一个斐波那契数列生成器,但我不确定应该使用哪种数据类型。这就是我正在做的事情:

  • 生成斐波那契数列中的前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑忘罢 2025-01-14 03:21:34

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

流星番茄 2025-01-14 03:21:34

为什么不只使用标准数组? int[1000] 和 double[1000]?

Why don't you use just standard Arrays? int[1000] and double[1000]?

<逆流佳人身旁 2025-01-14 03:21:34

只需使用 List,因为斐波那契元素是整数。

Just use List<int>, because Fibonacci elements are integers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文