summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/math/pgfmathfunctions.integerarithmetics.code.tex
blob: 9094da50dc9d1e2eb6518906ee731ad5c0ff371a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
% 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