function p = i4_gpf ( n )

%*****************************************************************************80
%
%% i4_gpf() finds the greatest prime factor of an integer.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 November 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the integer to be analyzed.
%
%  Output:
%
%    integer P: the greatest prime factor of N.
%
  nleft = n;
%
%  Find out how many primes we stored.
%
  maxprime = prime ( -1 );
%
%  Try dividing the remainder by each prime.
%
  i = 0;
  p = 1;

  while ( 1 < nleft )

    i = i + 1;
    if ( maxprime < i )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'i4_gpf(): Fatal error!\n' );
      fprintf ( 1, '  Exceeded maximum stored prime number.\n' );
      error ( 'i4_gpf(): Fatal error!' );
    end

    p = prime ( i );

    while ( mod ( nleft, p ) == 0 )
      nleft = nleft / p;
    end

  end

  return
end
