I am currently working on an assignment that involves calculating Fibonacci numbers up to F(300) using a linear time complexity algorithm. I know via a quick search that a Java long
will overflow at around F(93), so I am trying to figure out how to store such a large number.
However, our assignment states that we are not allowed to use language libraries such as BigInteger
to store our numbers. We must write our own large integer data type or object.
What exactly am I to do in this case? I have never before had to write my own data type to handle something like a large integer. Is there some usual method of doing this?
I believe what you need is to create a data type out of primitive arrays, like int[] or long[] so that you store result value by bits across elements of this array. Just like you can use two int's to store a long's bits you make take more ints to store a longer number.
One problem though is to interpret this value, say, you wish to print the value - that would require an algorithm on its own, if a string like 2^n1 + 2^n2 + ... + 2^nk is not acceptable.
An example (using byte array to store decimal digits):