The issue is that Int
can only store 64bit numbers (on 64bit platforms) and Fibonacci 95 is bigger than the maximum number that can be stored on 64bits.
Fibonacci 95 is 31 940 434 634 990 099 905
, while the biggest number Int64
can hold is 2^63-1 = 9 223 372 036 854 775 807
.
You can use Decimal
for storing larger numbers than what Int
can hold Decimal.greatestFiniteMagnitude
is 3.4028236692093865e+165
.
However, Swift doesn’t have a built-in type for storing arbitrarily large numbers, so if you want to do that you’ll either need to use a 3rd party library or implement an appropriate data type yourself.
1
solved Fibonacci in swift playground [duplicate]