% tkz-tools-arith.tex
% Copyright 2020  Alain Matthes
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status “maintained”.
% 
% The Current Maintainer of this work is Alain Matthes.
%  utf8 encoding
\def\fileversion{3.06c}
\def\filedate{2020/03/18} 
\typeout{2020/03/18 3.06c tkz-tools-arith.tex}    
% 1.161  test if "isfactor"   is already defined
% now with pgf 3.0  gcd isprime isodd iseven are defined
%<--------------------------------------------------------------------------–>
% Objet :  tools for arithmetic 
%<--------------------------------------------------------------------------–>
%    [About integers numbers]
%<--------------------------------------------------------------------------–>
\ProvidesFile{tkz-tool-arith.tex}[tkz-tool-arith 3.02 c]
% \edef\tkzAtCode{\the\catcode`\@}
% \catcode`\@=11\relax
\makeatletter
%<--------------------------------------------------------------------------–>
%                            arithmetic tools 
%<--------------------------------------------------------------------------–>
\tkzutil@ifundefined{pgfmath@function@gcd}{%
\pgfmathdeclarefunction{gcd}{2}{%
 \begingroup%
 \pgfmathcontinuelooptrue
    \pgfmath@xa=#1pt\relax%
    \pgfmath@xb=#2pt\relax%
    \ifdim\pgfmath@xa=0pt \pgfmathcontinueloopfalse
     \pgfmath@xa=\pgfmath@xb
    \fi 
     \ifdim\pgfmath@xb=0pt \pgfmathcontinueloopfalse
       \pgfmath@xb=\pgfmath@xa
    \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
  \edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@xa}}%
  \pgfmath@smuggleone\pgfmathresult
  \endgroup
}
}{} 
%<--------------------------------------------------------------------------->
\tkzutil@ifundefined{pgfmath@function@isprime}{%
\pgfmathdeclarefunction{isprime}{1}{%
	\begingroup%
\pgf@unknowntrue
\c@pgfmath@counta#1\relax% 
 \ifcase#1\relax 
 % |#1| = 0 
 \pgf@isaprimefalse\pgf@unknownfalse
 \or
 % |#1| = 1
  \pgf@isaprimefalse\pgf@unknownfalse
  \or
% |#1| = 2
  \pgf@isaprimetrue\pgf@unknownfalse
 \or
% |#1| = 3
  \pgf@isaprimetrue\pgf@unknownfalse
   \else
 % |#1| > 3
   \ifodd#1\relax 
   \else
    \pgf@isaprimefalse\pgf@unknownfalse
   \fi
 \fi  
\ifpgf@unknown
\c@pgfmath@countd=\thr@@ \pgf@isaprimetrue
 \loop
 \c@pgfmath@countb=\c@pgfmath@counta  
 \divide\c@pgfmath@countb by\c@pgfmath@countd
  \ifnum\c@pgfmath@countb>\c@pgfmath@countd \pgf@unknowntrue
  \else\pgf@unknownfalse\fi
    \multiply\c@pgfmath@countb by\c@pgfmath@countd
  \ifnum\c@pgfmath@countb=\c@pgfmath@counta  
   \global\pgf@isaprimefalse\pgf@unknownfalse\fi
  \ifpgf@unknown\advance\c@pgfmath@countd by 2\relax% 
 \repeat
\fi
\ifpgf@isaprime
  \def\pgfmathresult{1}%
\else
  \def\pgfmathresult{0}%
\fi
 \pgfmath@smuggleone\pgfmathresult%
\endgroup
}}{}  
%<--------------------------------------------------------------------------->
\tkzutil@ifundefined{pgfmath@function@isodd}{%   
\pgfmathdeclarefunction{isodd}{1}{%  
\begingroup
\ifodd #1 \def\pgfmathresult{1}%
\else\def\pgfmathresult{0}%
\fi
 \pgfmath@smuggleone\pgfmathresult%
\endgroup}
}{}  
%<--------------------------------------------------------------------------->
\tkzutil@ifundefined{pgfmath@function@isfactor}{%
   \pgfmathdeclarefunction{isfactor}{2}{%  
\begingroup 
\c@pgfmath@counta#1\relax%   
\c@pgfmath@countb#2\relax%   
\c@pgfmath@countc#1\relax%
\divide\c@pgfmath@counta by\c@pgfmath@countb%
\multiply\c@pgfmath@counta by\c@pgfmath@countb% 
\advance\c@pgfmath@countc by-\c@pgfmath@counta%
\ifnum\c@pgfmath@countc=0 \def\pgfmathresult{1}%
\else
  \def\pgfmathresult{0}%
  \fi
\pgfmath@smuggleone\pgfmathresult%
\endgroup} 
}{}  
%<--------------------------------------------------------------------------->
\def\tkzfactors#1{% 
\begingroup
\c@pgfmath@countb#1 
\pgfmathisprime{\c@pgfmath@countb}% 
\ifnum\pgfmathresult=1 
   \number\c@pgfmath@countb\ % prime number
\else% we can found factors
\tkz@factors{#1} 
\fi
\endgroup}% 
%<--------------------------------------------------------------------------–>
\def\tkz@factors#1{%
\begingroup
\c@pgfmath@countb#1 
\c@pgfmath@countd2\relax
 \loop
 \ifodd\c@pgfmath@countb\relax% 
 \else
  \divide\c@pgfmath@countb by2 %
   \number\c@pgfmath@countd\ % 
 \repeat%
%%%%%%%%%%%%%%
\def\tkzprim@last{1}%
\loop% first loop
  \ifnum\c@pgfmath@countb>1\relax
    \c@pgfmath@countd\tkzprim@last\relax
    \c@pgfmath@countc1\relax
%%%%%%%%%%%%%%
\tkz@find@last
%%%%%%%%%%%%%%
   \divide\c@pgfmath@countb by\c@pgfmath@countd\relax
   \number\c@pgfmath@countd\ %  affiche le facteur
\repeat% end first loop
%%%%%%%%%%%%%%   
\endgroup%
}%
%<--------------------------------------------------------------------------->
\def\tkz@find@last{%
     \ifnum\c@pgfmath@countc>\z@
     \edef\tkzprim@last{\the\c@pgfmath@countd}%
     \advance\c@pgfmath@countd by2\relax% begins with 3
     \c@pgfmath@counta\c@pgfmath@countb% macro mod !!
     \c@pgfmath@countc\c@pgfmath@counta%
     \divide\c@pgfmath@counta by\c@pgfmath@countd
     \multiply\c@pgfmath@counta by\c@pgfmath@countd
     \advance\c@pgfmath@countc by-\c@pgfmath@counta
      \let\tkz@find@next=\tkz@find@last
     \else
     \let\tkz@find@next=\relax%   
     \fi
     \tkz@find@next
}%
%<--------------------------------------------------------------------------->
\def\tkzReducFrac#1#2{% 
\begingroup
\ifnum#1<0\relax%
   \c@pgfmath@counta=0\relax 
   \advance\c@pgfmath@counta by-#1
 \else
    \c@pgfmath@counta#1
 \fi
    \c@pgfmath@countb#2
    \pgfmathgcd{\c@pgfmath@counta}{\c@pgfmath@countb}%
    \pgfmathsetcount\c@pgfmath@countc{\pgfmathresult}
 \ifnum\c@pgfmath@counta=0\relax
   \c@pgfmath@counta#1
   \c@pgfmath@countb#2
 \else 
   \c@pgfmath@counta#1
   \c@pgfmath@countb#2
   \divide\c@pgfmath@counta by\c@pgfmath@countc%
   \divide\c@pgfmath@countb by\c@pgfmath@countc%
 \fi
\xdef\tkzMathFirstResult{\the\c@pgfmath@counta}
\xdef\tkzMathSecondResult{\the\c@pgfmath@countb} 
\endgroup}
%<--------------------------------------------------------------------------->
%\catcode`\@=\tkzAtCode\relax 
\makeatother
\endinput