function TF = isProbablePrimeFib(x)
   % isProbablePrimeFib: Applies a Fibonacci prime test to x,
   % usage: TF = isProbablePrimeFib(x)
   % 
   % The Fibonacci test for primality of the number x arises due to
   % some basic rules about the Fibonacci numbers:
   %   mod(x,5) == 1 or 4, then mod(fibonacci(x-1),x) == 0
   %   mod(x,5) == 2 or 3, then mod(fibonacci(x+1),x) == 0
   %   mod(x,5) == 0, then mod(fibonacci(x),x) == 0
   % The converse of those rules also frequently applies,
   %   mod(x,5) == 1 or 4, if mod(fibonacci(x-1),x) == 0, then x is likely prime
   %   mod(x,5) == 2 or 3, if mod(fibonacci(x+1),x) == 0, then x is likely prime
   % 
   % Failures are not terribly common, but they do happen. In that
   % case, x is called a Fibonacci pseudo-prime.
   %
   % Arguments: (input)
   %  x - any positive integer, or array thereof to be tested for primality
   %
   % Arguments: (output)
   % TF - boolean array of the same size and shape as x. 1 indicates x may
   %       be prime, unless x is a Fibonacci pseudoprime
   %
   %  Examples:
   %   
   %
   % See also: isprime, isRough, isProbablePrimeFLT
   %
   % John D'Errico, woodchips@rochester.rr.com, 1/28/2025
   
   TF = false(size(x));
   for i = 1:numel(x)
     xi = x(i);
     if xi >= 2
       % in the alternative, with x(i) < 2, it is clearly not prime
       % and TF(i) was initialized to false.

       rem5 = mod(xi,5);
       switch rem5
         case 0
           % unless x is 5, and only 5, then x is never prime
           % In general, if x is divisible by 5, then fibonacci(x) will
           % also be divisible by 5
           TF(i) = logical(xi == 5);
           
         case {1, 4}
           % when mod(xi,5) == 1 or 4, then the test is:
           TF(i) = logical(fibmod(xi-1,xi) == 0);
           
         otherwise
           % effectively, case {2,3}
           TF(i) = logical(fibmod(xi+1,xi) == 0);
       end
     end
   end
end

