0

I am trying to solve this problem: Problem 44481. How many Fibonacci numbers? This is what I've done

function y = fib_count(x)
  a=unique(x);
  a(mod(sqrt(5*a.^2+4),1)~=0&mod(sqrt(5*a.^2-4),1)~=0)=[];
  y=length(a);
end

The problem is when there are large numbers, like 8944394323791465, they will all be Fibonacci numbers, no matter what.

I tried this: mod(10^20+1,2) and it returns 0.

Have you dealt with this before?

horchler
  • 18,384
  • 4
  • 37
  • 73
  • 3
    The `double` data type does not have enough precision to represent `10^20+1` exactly. You can also check that `10^20+1==10^20` gives `true`. See [this answer](https://stackoverflow.com/a/686454/2586922) – Luis Mendo May 11 '23 at 12:13
  • 1
    You can only expect `mod` to work as expected up to the value of [`flintmax`](https://www.mathworks.com/help/matlab/ref/flintmax.html), the largest consecutive integer in floating point (`2^53` for double precision). Beyond that you'll need to use variable precision arithmetic ([`vpa`](https://www.mathworks.com/help/symbolic/vpa.html)) in the Symbolic Math Toolbox, e.g., `mod(vpa(10)^20+1,2)`. – horchler May 11 '23 at 12:21

0 Answers0