% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

% Macros contributed by Alain Matthes and adapted by CJ
\pgfmathdeclarefunction{gcd}{2}{%
  \begingroup
    \pgfmathcontinuelooptrue
    \pgfmath@xa=#1pt
    \pgfmath@xb=#2pt
    \ifdim\pgfmath@xa=0pt 
      \pgfmathcontinueloopfalse
      \pgfmath@xa=\pgfmath@xb
    \fi 
    \ifdim\pgfmath@xb=0pt
      \pgfmathcontinueloopfalse
      \pgfmath@xb=\pgfmath@xa
    \fi
    \ifdim\pgfmath@xa<0pt
      \pgfmath@xa=-\pgfmath@xa
    \fi
    \ifdim\pgfmath@xb<0pt
      \pgfmath@xb=-\pgfmath@xb
    \fi
    \loop
      \ifpgfmathcontinueloop 
      \ifdim\pgfmath@xa=\pgfmath@xb
        \pgfmathcontinueloopfalse
      \else
        \ifdim\pgfmath@xa>\pgfmath@xb 
          \advance\pgfmath@xa by-\pgfmath@xb
        \else 
          \advance\pgfmath@xb by-\pgfmath@xa
        \fi
      \fi
    \repeat
    \pgfmathparse{int(\pgfmath@xa)}%
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}

\newif\ifpgfmath@isprime@unknown
\newif\ifpgfmath@isprime 

\pgfmathdeclarefunction{isprime}{1}{%
  \begingroup
    \pgfmath@isprime@unknowntrue
    \pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}%
    \ifcase\c@pgfmath@counta\relax 
      % |#1| = 0 
      \pgfmath@isprimefalse
      \pgfmath@isprime@unknownfalse
    \or
      % |#1| = 1
      \pgfmath@isprimefalse
      \pgfmath@isprime@unknownfalse
    \or
      % |#1| = 2
      \pgfmath@isprimetrue
      \pgfmath@isprime@unknownfalse
    \or
      % |#1| = 3
      \pgfmath@isprimetrue
      \pgfmath@isprime@unknownfalse
    \else
      % |#1| > 3
      \ifodd\c@pgfmath@counta\relax 
      \else
        \pgfmath@isprimefalse
        \pgfmath@isprime@unknownfalse
      \fi
    \fi  
    \ifpgfmath@isprime@unknown
      \c@pgfmath@countd=3
      \pgfmath@isprimetrue
      \pgfmathloop
        \c@pgfmath@countb=\c@pgfmath@counta  
        \divide\c@pgfmath@countb by\c@pgfmath@countd
        \ifnum\c@pgfmath@countb>\c@pgfmath@countd
          \pgfmath@isprime@unknowntrue
        \else
          \pgfmath@isprime@unknownfalse
        \fi
        \multiply\c@pgfmath@countb by\c@pgfmath@countd
        \ifnum\c@pgfmath@countb=\c@pgfmath@counta  
          \global\pgfmath@isprimefalse
          \pgfmath@isprime@unknownfalse
        \fi
      \ifpgfmath@isprime@unknown\advance\c@pgfmath@countd by 2
      \repeatpgfmathloop
    \fi
    \ifpgfmath@isprime
      \def\pgfmathresult{1}%
    \else
      \def\pgfmathresult{0}%
    \fi
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}

\pgfmathdeclarefunction{isodd}{1}{%
  \begingroup
    \pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}%
    \ifodd\c@pgfmath@counta
      \def\pgfmathresult{1}%
    \else
      \def\pgfmathresult{0}%
    \fi
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}

\pgfmathdeclarefunction{iseven}{1}{%
  \begingroup
    \pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}%
    \ifodd\c@pgfmath@counta
      \def\pgfmathresult{0}%
    \else
      \def\pgfmathresult{1}%
    \fi
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}

\endinput
