1

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

Mike
  • 3,204
  • 8
  • 47
  • 74
  • 4
    via Jon Skeet: "For values which are "naturally exact decimals" it's good to use decimal. This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too. Consider the score given to divers or ice skaters, for example." http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c – SpaceBison Feb 20 '12 at 14:32

3 Answers3

3

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

linepogl
  • 9,147
  • 4
  • 34
  • 45
  • It's not an infinite generator, it's only generating the first 1000 elements in the fibonacci series. – Mike Feb 20 '12 at 14:53
  • @Mike, OK, then just use directly a List or an array. The real problem here is that the 1000th Fibonacci number is ~4.3x10^209, so you have to resort to some big numerals library... `long`, `int`, `float` or `decimal` are way too small. – linepogl Feb 20 '12 at 15:02
  • :The max value for double is 1.7976931348623157E+308 and the 1000th fibonacci number is 4.34*10^208. So should't it support? – Mike Feb 20 '12 at 16:11
  • From wikipedia (http://en.wikipedia.org/wiki/Floating_point): `any integer with absolute value less than or equal to 2^53 can be exactly represented in the double precision format`. So, you will have only approximations for the numbers above that limit. – linepogl Feb 20 '12 at 16:18
0

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

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Becuase I would like to use lazy evaluation of LINQ. The whole point of the app is to verify the performance. – Mike Feb 20 '12 at 14:33
0

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

Ebad Masood
  • 2,389
  • 28
  • 46