Debian Codegraph @

texmf-dist/tex/latex/tkz-base/tkz-tools-arith.tex

raw
1 % tkz-tools-arith.tex
2 % Copyright 2024 Alain Matthes
3 % This work may be distributed and/or modified under the
4 % conditions of the LaTeX Project Public License, either version 1.3
5 % of this license or (at your option) any later version.
6 % The latest version of this license is in
7 % http://www.latex-project.org/lppl.txt
8 % and version 1.3 or later is part of all distributions of LaTeX
9 % version 2005/12/01 or later.
10 % This work has the LPPL maintenance status “maintained”.
11 % The Current Maintainer of this work is Alain Matthes.
12
13 \def\fileversion{4.21c}
14 \def\filedate{2024/01/16}
15 \typeout{2024/01/16 4.21c tkz-tools-arith.tex}
16 % 1.161 test if "isfactor" is already defined
17 % now with pgf 3.0 gcd isprime isodd iseven are defined
18 %<--------------------------------------------------------------------------–>
19 % Objet : tools for arithmetic
20 %<--------------------------------------------------------------------------–>
21 % [About integers numbers]
22 %<--------------------------------------------------------------------------–>
23 \ProvidesFile{tkz-tool-arith.tex}[tkz-tool-arith 3.02 c]
24 % \edef\tkzAtCode{\the\catcode`\@}
25 % \catcode`\@=11\relax
26 \makeatletter
27 %<--------------------------------------------------------------------------–>
28 % arithmetic tools
29 %<--------------------------------------------------------------------------–>
30 \tkzutil@ifundefined{pgfmath@function@gcd}{%
31 \pgfmathdeclarefunction{gcd}{2}{%
32 \begingroup%
33 \pgfmathcontinuelooptrue
34 \pgfmath@xa=#1pt\relax%
35 \pgfmath@xb=#2pt\relax%
36 \ifdim\pgfmath@xa=0pt \pgfmathcontinueloopfalse
37 \pgfmath@xa=\pgfmath@xb
38 \fi
39 \ifdim\pgfmath@xb=0pt \pgfmathcontinueloopfalse
40 \pgfmath@xb=\pgfmath@xa
41 \fi
42 \loop
43 \ifpgfmathcontinueloop
44 \ifdim \pgfmath@xa=\pgfmath@xb\pgfmathcontinueloopfalse
45 \else
46 \ifdim \pgfmath@xa>\pgfmath@xb
47 \advance \pgfmath@xa by-\pgfmath@xb
48 \else
49 \advance \pgfmath@xb by-\pgfmath@xa
50 \fi
51 \fi
52 \repeat
53 \edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@xa}}%
54 \pgfmath@smuggleone\pgfmathresult
55 \endgroup
56 }
57 }{}
58 %<--------------------------------------------------------------------------->
59 \tkzutil@ifundefined{pgfmath@function@isprime}{%
60 \pgfmathdeclarefunction{isprime}{1}{%
61 \begingroup%
62 \pgf@unknowntrue
63 \c@pgfmath@counta#1\relax%
64 \ifcase#1\relax
65 % |#1| = 0
66 \pgf@isaprimefalse\pgf@unknownfalse
67 \or
68 % |#1| = 1
69 \pgf@isaprimefalse\pgf@unknownfalse
70 \or
71 % |#1| = 2
72 \pgf@isaprimetrue\pgf@unknownfalse
73 \or
74 % |#1| = 3
75 \pgf@isaprimetrue\pgf@unknownfalse
76 \else
77 % |#1| > 3
78 \ifodd#1\relax
79 \else
80 \pgf@isaprimefalse\pgf@unknownfalse
81 \fi
82 \fi
83 \ifpgf@unknown
84 \c@pgfmath@countd=\thr@@ \pgf@isaprimetrue
85 \loop
86 \c@pgfmath@countb=\c@pgfmath@counta
87 \divide\c@pgfmath@countb by\c@pgfmath@countd
88 \ifnum\c@pgfmath@countb>\c@pgfmath@countd \pgf@unknowntrue
89 \else\pgf@unknownfalse\fi
90 \multiply\c@pgfmath@countb by\c@pgfmath@countd
91 \ifnum\c@pgfmath@countb=\c@pgfmath@counta
92 \global\pgf@isaprimefalse\pgf@unknownfalse\fi
93 \ifpgf@unknown\advance\c@pgfmath@countd by 2\relax%
94 \repeat
95 \fi
96 \ifpgf@isaprime
97 \def\pgfmathresult{1}%
98 \else
99 \def\pgfmathresult{0}%
100 \fi
101 \pgfmath@smuggleone\pgfmathresult%
102 \endgroup
103 }}{}
104 %<--------------------------------------------------------------------------->
105 \tkzutil@ifundefined{pgfmath@function@isodd}{%
106 \pgfmathdeclarefunction{isodd}{1}{%
107 \begingroup
108 \ifodd #1 \def\pgfmathresult{1}%
109 \else\def\pgfmathresult{0}%
110 \fi
111 \pgfmath@smuggleone\pgfmathresult%
112 \endgroup}
113 }{}
114 %<--------------------------------------------------------------------------->
115 \tkzutil@ifundefined{pgfmath@function@isfactor}{%
116 \pgfmathdeclarefunction{isfactor}{2}{%
117 \begingroup
118 \c@pgfmath@counta#1\relax%
119 \c@pgfmath@countb#2\relax%
120 \c@pgfmath@countc#1\relax%
121 \divide\c@pgfmath@counta by\c@pgfmath@countb%
122 \multiply\c@pgfmath@counta by\c@pgfmath@countb%
123 \advance\c@pgfmath@countc by-\c@pgfmath@counta%
124 \ifnum\c@pgfmath@countc=0 \def\pgfmathresult{1}%
125 \else
126 \def\pgfmathresult{0}%
127 \fi
128 \pgfmath@smuggleone\pgfmathresult%
129 \endgroup}
130 }{}
131 %<--------------------------------------------------------------------------->
132 \def\tkzfactors#1{%
133 \begingroup
134 \c@pgfmath@countb#1
135 \pgfmathisprime{\c@pgfmath@countb}%
136 \ifnum\pgfmathresult=1
137 \number\c@pgfmath@countb\ % prime number
138 \else% we can found factors
139 \tkz@factors{#1}
140 \fi
141 \endgroup}%
142 %<--------------------------------------------------------------------------–>
143 \def\tkz@factors#1{%
144 \begingroup
145 \c@pgfmath@countb#1
146 \c@pgfmath@countd2\relax
147 \loop
148 \ifodd\c@pgfmath@countb\relax%
149 \else
150 \divide\c@pgfmath@countb by2 %
151 \number\c@pgfmath@countd\ %
152 \repeat%
153 %%%%%%%%%%%%%%
154 \def\tkzprim@last{1}%
155 \loop% first loop
156 \ifnum\c@pgfmath@countb>1\relax
157 \c@pgfmath@countd\tkzprim@last\relax
158 \c@pgfmath@countc1\relax
159 %%%%%%%%%%%%%%
160 \tkz@find@last
161 %%%%%%%%%%%%%%
162 \divide\c@pgfmath@countb by\c@pgfmath@countd\relax
163 \number\c@pgfmath@countd\ % affiche le facteur
164 \repeat% end first loop
165 %%%%%%%%%%%%%%
166 \endgroup%
167 }%
168 %<--------------------------------------------------------------------------->
169 \def\tkz@find@last{%
170 \ifnum\c@pgfmath@countc>\z@
171 \edef\tkzprim@last{\the\c@pgfmath@countd}%
172 \advance\c@pgfmath@countd by2\relax% begins with 3
173 \c@pgfmath@counta\c@pgfmath@countb% macro mod !!
174 \c@pgfmath@countc\c@pgfmath@counta%
175 \divide\c@pgfmath@counta by\c@pgfmath@countd
176 \multiply\c@pgfmath@counta by\c@pgfmath@countd
177 \advance\c@pgfmath@countc by-\c@pgfmath@counta
178 \let\tkz@find@next=\tkz@find@last
179 \else
180 \let\tkz@find@next=\relax%
181 \fi
182 \tkz@find@next
183 }%
184 %<--------------------------------------------------------------------------->
185 \def\tkzReducFrac#1#2{%
186 \begingroup
187 \ifnum#1<0\relax%
188 \c@pgfmath@counta=0\relax
189 \advance\c@pgfmath@counta by-#1
190 \else
191 \c@pgfmath@counta#1
192 \fi
193 \c@pgfmath@countb#2
194 \pgfmathgcd{\c@pgfmath@counta}{\c@pgfmath@countb}%
195 \pgfmathsetcount\c@pgfmath@countc{\pgfmathresult}
196 \ifnum\c@pgfmath@counta=0\relax
197 \c@pgfmath@counta#1
198 \c@pgfmath@countb#2
199 \else
200 \c@pgfmath@counta#1
201 \c@pgfmath@countb#2
202 \divide\c@pgfmath@counta by\c@pgfmath@countc%
203 \divide\c@pgfmath@countb by\c@pgfmath@countc%
204 \fi
205 \xdef\tkzMathFirstResult{\the\c@pgfmath@counta}
206 \xdef\tkzMathSecondResult{\the\c@pgfmath@countb}
207 \endgroup}
208 %<--------------------------------------------------------------------------->
209 %\catcode`\@=\tkzAtCode\relax
210 \makeatother
211 \endinput