function value = singular_f ( x, n )

%*****************************************************************************80
%
%% singular_f() evaluates the Powell Singular function.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 July 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X(N), the evaluation point.
%
%    integer N, the number of variables.
%
%  Output:
%
%    real VALUE, the function value.
%
  value = 0.0;

  for j = 1 : 4 : n

    if ( j + 1 <= n )
      xjp1 = x(j+1);
    else
      xjp1 = 0.0;
    end

    if ( j + 2 <= n )
      xjp2 = x(j+2);
    else
      xjp2 = 0.0;
    end

    if ( j + 3 <= n )
      xjp3 = x(j+3);
    else
      xjp3 = 0.0;
    end

    f1 = x(j) + 10.0 * xjp1;

    if ( j + 1 <= n )
      f2 = xjp2 - xjp3;
    else
      f2 = 0.0;
    end

    if ( j + 2 <= n )
      f3 = xjp1 - 2.0 * xjp2;
    else
      f3 = 0.0;
    end

    if ( j + 3 <= n )
      f4 = x(j) - xjp3;
    else
      f4 = 0.0;
    end

    value = value ...
      +        f1 ^ 2 ...
      +  5.0 * f2 ^ 2 ...
      +        f3 ^ 4 ...
      + 10.0 * f4 ^ 4;

  end

  return
end
