summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tkz/tkz-base/latex/tkz-tools-arith.tex
blob: c25572c7097c3768d26cca48f12bbed4267d781d (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
% 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.05c}
\def\filedate{2020/03/03} 
\typeout{2020/03/03 3.05c 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