| 1 |
% This file may be distributed and/or modified |
| 2 |
% |
| 3 |
% 1. under the LaTeX Project Public License and/or |
| 4 |
% 2. under the GNU Public License. |
| 5 |
% |
| 6 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 7 |
|
| 8 |
% Macros contributed by Alain Matthes and adapted by CJ |
| 9 |
\pgfmathdeclarefunction{gcd}{2}{% |
| 10 |
\begingroup |
| 11 |
\pgfmathcontinuelooptrue |
| 12 |
\pgfmath@xa=#1pt |
| 13 |
\pgfmath@xb=#2pt |
| 14 |
\ifdim\pgfmath@xa=0pt\relax |
| 15 |
\pgfmathcontinueloopfalse |
| 16 |
\pgfmath@xa=\pgfmath@xb |
| 17 |
\fi |
| 18 |
\ifdim\pgfmath@xb=0pt\relax |
| 19 |
\pgfmathcontinueloopfalse |
| 20 |
\pgfmath@xb=\pgfmath@xa |
| 21 |
\fi |
| 22 |
\ifdim\pgfmath@xa<0pt\relax |
| 23 |
\pgfmath@xa=-\pgfmath@xa |
| 24 |
\fi |
| 25 |
\ifdim\pgfmath@xb<0pt\relax |
| 26 |
\pgfmath@xb=-\pgfmath@xb |
| 27 |
\fi |
| 28 |
\loop |
| 29 |
\ifpgfmathcontinueloop |
| 30 |
\ifdim\pgfmath@xa=\pgfmath@xb |
| 31 |
\pgfmathcontinueloopfalse |
| 32 |
\else |
| 33 |
\ifdim\pgfmath@xa>\pgfmath@xb |
| 34 |
\advance\pgfmath@xa by-\pgfmath@xb |
| 35 |
\else |
| 36 |
\advance\pgfmath@xb by-\pgfmath@xa |
| 37 |
\fi |
| 38 |
\fi |
| 39 |
\repeat |
| 40 |
\pgfmathparse{int(\pgfmath@xa)}% |
| 41 |
\pgfmath@smuggleone\pgfmathresult |
| 42 |
\endgroup} |
| 43 |
|
| 44 |
\newif\ifpgfmath@isprime@unknown |
| 45 |
\newif\ifpgfmath@isprime |
| 46 |
|
| 47 |
\pgfmathdeclarefunction{isprime}{1}{% |
| 48 |
\begingroup |
| 49 |
\pgfmath@isprime@unknowntrue |
| 50 |
\pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}% |
| 51 |
\ifcase\c@pgfmath@counta\relax |
| 52 |
% |#1| = 0 |
| 53 |
\pgfmath@isprimefalse |
| 54 |
\pgfmath@isprime@unknownfalse |
| 55 |
\or |
| 56 |
% |#1| = 1 |
| 57 |
\pgfmath@isprimefalse |
| 58 |
\pgfmath@isprime@unknownfalse |
| 59 |
\or |
| 60 |
% |#1| = 2 |
| 61 |
\pgfmath@isprimetrue |
| 62 |
\pgfmath@isprime@unknownfalse |
| 63 |
\or |
| 64 |
% |#1| = 3 |
| 65 |
\pgfmath@isprimetrue |
| 66 |
\pgfmath@isprime@unknownfalse |
| 67 |
\else |
| 68 |
% |#1| > 3 |
| 69 |
\ifodd\c@pgfmath@counta\relax |
| 70 |
\else |
| 71 |
\pgfmath@isprimefalse |
| 72 |
\pgfmath@isprime@unknownfalse |
| 73 |
\fi |
| 74 |
\fi |
| 75 |
\ifpgfmath@isprime@unknown |
| 76 |
\c@pgfmath@countd=3 |
| 77 |
\pgfmath@isprimetrue |
| 78 |
\pgfmathloop |
| 79 |
\c@pgfmath@countb=\c@pgfmath@counta |
| 80 |
\divide\c@pgfmath@countb by\c@pgfmath@countd |
| 81 |
\ifnum\c@pgfmath@countb>\c@pgfmath@countd |
| 82 |
\pgfmath@isprime@unknowntrue |
| 83 |
\else |
| 84 |
\pgfmath@isprime@unknownfalse |
| 85 |
\fi |
| 86 |
\multiply\c@pgfmath@countb by\c@pgfmath@countd |
| 87 |
\ifnum\c@pgfmath@countb=\c@pgfmath@counta |
| 88 |
\global\pgfmath@isprimefalse |
| 89 |
\pgfmath@isprime@unknownfalse |
| 90 |
\fi |
| 91 |
\ifpgfmath@isprime@unknown\advance\c@pgfmath@countd by 2 |
| 92 |
\repeatpgfmathloop |
| 93 |
\fi |
| 94 |
\ifpgfmath@isprime |
| 95 |
\def\pgfmathresult{1}% |
| 96 |
\else |
| 97 |
\def\pgfmathresult{0}% |
| 98 |
\fi |
| 99 |
\pgfmath@smuggleone\pgfmathresult |
| 100 |
\endgroup} |
| 101 |
|
| 102 |
\pgfmathdeclarefunction{isodd}{1}{% |
| 103 |
\begingroup |
| 104 |
\pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}% |
| 105 |
\ifodd\c@pgfmath@counta |
| 106 |
\def\pgfmathresult{1}% |
| 107 |
\else |
| 108 |
\def\pgfmathresult{0}% |
| 109 |
\fi |
| 110 |
\pgfmath@smuggleone\pgfmathresult |
| 111 |
\endgroup} |
| 112 |
|
| 113 |
\pgfmathdeclarefunction{iseven}{1}{% |
| 114 |
\begingroup |
| 115 |
\pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}% |
| 116 |
\ifodd\c@pgfmath@counta |
| 117 |
\def\pgfmathresult{0}% |
| 118 |
\else |
| 119 |
\def\pgfmathresult{1}% |
| 120 |
\fi |
| 121 |
\pgfmath@smuggleone\pgfmathresult |
| 122 |
\endgroup} |
| 123 |
|
| 124 |
\endinput |