+ 2

Display fibonacci 100 numbers without array

https://code.sololearn.com/ca20A11A12a8/#cpp It displays wrong after some elements. please help

22nd Apr 2021, 10:51 AM
Coder
Coder - avatar
5 Réponses
+ 2
Coder & Shadow, double number is not a good idea for such as task. Here the solution in c++ ( cpp ) for big numbers and works here on SL Playground till 4444: https://code.sololearn.com/ccbD2moxNva2/?ref=app
22nd Apr 2021, 6:57 PM
JaScript
JaScript - avatar
+ 1
Yes, because by using (long) double, you will inevitably lose accuracy (not only the last number by the way). https://stackoverflow.com/questions/30052710/why-double-can-store-bigger-numbers-than-unsigned-long-long If you must use C++, there is practically no way around using a multiprecision library like GMP, unless you want to develop and implement a logic to store values larger than 2^64-1 yourself. You could, for example, use two std::uint64_t integers and view them as the most and least significant digits of a base 2^64 numbers, but then you will have to implement addition and especially conversion to base 10 yourself. GMP: https://gmplib.org/ Languages like Python are easier to use here because they already implement arbitrary precision integers, unlike C++. So if you can switch from C++, it might be simpler to do so. However, such libaries will use arrays themselves to implement this kind of logic, so there is that as well.
22nd Apr 2021, 12:09 PM
Shadow
Shadow - avatar
+ 1
JaScript Your code overflows around the 95th number, as I described initially, so it is not a full solution. And yes, I am aware double is no really good either due to precision issues. My problem is that no solution comes to my mind that would really work without any array. I basically already described everything that comes to my mind in both answers earlier. If you have a good idea, I would love to hear a suggestion.
22nd Apr 2021, 7:19 PM
Shadow
Shadow - avatar
0
As far as I can see your logic is correct. The issue is that when the numbers become too big for the integral data type you are using, they will overflow. Unfortunately, even for the largest integral data type unsigned long long int or std::uint64_t, you will get an overflow beginning at the 94th number. However, it should be possible to use double or long double instead, but you will lose precision in this case. There, the idea would be to do something like this: https://code.sololearn.com/c8PG2w5vQgja/?ref=app Edit: It should be noted that the loss of precision will no longer give prefect results. Even though the results seem to be correct for the most part, inspecting the least significant digits of some of the last numbers shows that they are not entirely accurate. If you need to do this perfectly accurate, I don't think there is a way around multiprecision libraries like GMP, unless you want to develop the logic required to store larger integers yourself.
22nd Apr 2021, 11:28 AM
Shadow
Shadow - avatar
0
shadow's code is better but 100th fibonacci number is 354,224,848,179,261,915,075 so the code is still incorrect for that last numbers
22nd Apr 2021, 11:57 AM
Coder
Coder - avatar