40

\documentclass{article} % \makeatletter \def\primes#1#2{{% \def\comma{\def\comma{, }}% \count@\@ne\@tempcntb#2\relax\@curtab#1\relax \@primes}} \def\@primes{\loop\advance\count@\@ne \expandafter\ifx\csname p-\the\count@\endcsname\relax \ifnum\@tempcntb<\count@\else \ifnum\count@<\@curtab\else\comma\the\count@\fi\fi\else\repeat \@tempcnta\count@\...


40

Here is a flat LaTeX2e implementation. \documentclass{article} \usepackage{amsmath} \newcount{\numerator} \newcount{\denominator} \newcount{\gcd} % compute \gcd and returns reduced \numerator and \denominator \newcommand{\reduce}[2]% #1=numerator, #2=denominator {\numerator=#1\relax \denominator=#2\relax \loop \ifnum\numerator<\denominator \...


39

Remarks I used the powerful LaTeX3 featureset l3fp, which is automatically loaded by xparse. Implementation \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\myMathFunction}{m} { \fp_to_decimal:n {((#1) * 5) - (#1)^2} } \ExplSyntaxOff \begin{document} \myMathFunction{2} \end{document}


33

Here is a TikZ/PGF solution. I'm not sure how it compares to the l3fp approach, but it definitely offers more flexibility than a low-level TeX approach because it works in fixed-point arithmetic, not just with integers, and by using the right PGFkeys, you can easily customise how the result should be printed (trailing zeros, scientific notation, etc.). I ...


33

If you are not bound to expl3 (in which case you “just” need to implement the algorithm): \documentclass{scrartcl} \usepackage{xintgcd,xintfrac} \newcommand*\reducedfrac[2] {\begingroup \edef\gcd{\xintGCD{#1}{#2}}% \frac{\xintNum{\xintDiv{#1}{\gcd}}}{\xintNum{\xintDiv{#2}{\gcd}}}% \endgroup} \begin{document} \[ \frac{278922}{74088} = \...


31

D.E. Knuth has done this himself on page 218 of The TeXbook: \newif\ifprime \newif\ifunknown % boolean variables \newcount\n \newcount\p \newcount\d \newcount\a % integer variables \def\primes#1{2,~3% assume that #1 is at least 3 \n=#1 \advance\n by-2 % n more to go \p=5 % odd primes starting with p \loop\ifnum\n>0 \printifprime\advance\p by2 \repeat} \...


31

An option using Lua+LaTeX. Made small improvement. Made a Lua function to be called as a LaTeX command, with the numerator and denominator passed as arguments, instead of hardcoding the values in as before. The command is \simplify{a}{b}: \documentclass{article} \usepackage{luacode} \usepackage{amsmath} %------------------------ \begin{luacode} function ...


30

In good old (Plain) TeX, i.e., without LaTeX, with the proper TeX syntax: \documentclass{article} \begin{document} \newcount\pom % temporary \newcount\kw % square \newcount\first % first \def\myMathFunction#1{\pom#1 \first\pom \kw\pom \multiply\kw by\pom \multiply\first by5 \advance\first by-\kw \the\first} \myMathFunction{2} And an example of loop:...


29

Here's a LuaLaTeX-based solution. The code sets up a LaTeX macro named \rsqrt, which invokes a Lua function named rsqrt. The latter implements the simplification algorithm you've proposed -- with the following refinements: For n=0 or n=1, the code simply returns n (without a square root symbol); and Care is taken to omit the \sqrt{n/i²} term if it's equal ...


28

Yes, you can, and pretty easily too. \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\computesum}{mmm} {% pass control to an internal function \svend_compute_sum:nnn { #1 } { #2 } { #3 } } % a variable for storing the partial sums \fp_new:N \l_svend_partial_sum_fp \cs_new_protected:Npn \svend_compute_sum:nnn #1 #2 #3 { ...


27

Using TikZ and its calc library you can use the ( $ (<name1>)!<value>!(<name1>) $ ) syntax to find a point along the segment passing through (<name1>) and (<name2>). In the example below, ( $ (a)!0.66666!(b) $ ) means a point that is two thirds away from (a) in the segment joining (a) and (b): \documentclass{article} \...


27

An expl3 implementation: \nonstopmode \input expl3-generic \relax \ExplSyntaxOn % -*- expl3 -*- \cs_new:Nn \svend_gcd:nn { \int_compare:nNnTF {#2} = { 0 } {#1} { \svend_gcd:ff {#2} { \int_mod:nn {#1} {#2} } } } \cs_generate_variant:Nn \svend_gcd:nn { ff } \int_new:N \l__svend_tmp_int \cs_new:Nn \svend_reduced:nn { \int_set:Nn \...


26

plain tex (uniquely) is usually used with the classic Tex engine (or at least with pdf and e-tex extensions disabled) so there is no infix arithmetic \newcount\zzz \zzz=5 \multiply\zzz by 3 \advance\zzz by 2 sets \zzz to 17. If you use the plain format with e-tex you can use e-tex infix arithmentic \newcount\zzz \zzz=\numexpr 5*5 + 2\relax


26

This is, indeed, due to some inaccuracies in PGF, and can actually been seen in the manual in the section on coordinate calculations. More specifically it appears to be down to the the \pgfpointnormalised command which has been around for years (i.e., prior to the math engine) but has never been updated. Armed with an alternative definition, the altitudes ...


25

Here's a solution using LuaLaTeX. The MWE provides a LaTeX-side macro called \MyMathFunction that interfaces with a Lua-side function called mymathfunction; the latter does the actual computations. % !TEX TS-program = lualatex \documentclass{article} \usepackage{amsmath} % for '\ensuremath' macro \usepackage{luacode} % for 'luacode' environment % ...


22

Two solutions using only foreach own computing tools \foreach \x [count=\i] in {3.14,6.28,...,21.98} Difference between second and first items in list is calculate and added to second and successive values until it reaches the last one. On the same time \i counts list items. \foreach \i [evaluate=\i as \x using \i*3.14] in {1,2,...,7} \i advances ...


22

PGF You might want to take a look at the mathematical capabilities of pgf. I prepared a MWE: \documentclass{standalone} \usepackage{pgf} \usepgflibrary{fpu} \pgfkeys{ /pgf/fpu = true, /pgf/number format/.cd, precision=2, fixed, fixed zerofill, use comma, 1000 sep={.} } \begin{document} \pgfmathparse{2*(1234.56+9786.45)} \...


22

You can use PGF's calendar library to convert the current day and the first day of the current year into julian dates: \documentclass{article} \usepackage{pgfkeys, pgfcalendar} \newcount\julianA \newcount\julianB \newcommand\doy{% \pgfcalendardatetojulian{\year-\month-\day}{\julianA}% \pgfcalendardatetojulian{\year-1-1}{\julianB}% \advance\...


21

You are overwriting the nodes so it's a little trickier but essentially you can scope the path and use rotate around transformation. \documentclass[tikz]{standalone} \begin{document} \begin{tikzpicture} \providecommand* \angle {30} \coordinate[label=above left:A](A) at (2,3); \coordinate[label=below:B](B) at (0,0); \coordinate[label=below:C]...


21

The difficult task is generating the terms of the sequence, not computing the sum, of course; I present a macro that prints all the terms or just the sum. You can define a different starting point and another difference (defaults 0 and 1). \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\arithmeticsequence}{sO{}m} { \...


21

Reimplementation of Joseph's answer in LuaTeX. The nice thing about doing computations in Lua is that they are always fully-expandable. \documentclass{article} \usepackage{luacode} \begin{luacode} function collatz_next(n) if ( n \% 2 == 0 ) then return .5 * n else return 3 * n + 1 end end function collatz_count(n, m) m = m ...


19

Use the e-TeX structure: \newlength{\msize} \setlength{\msize}{\dimexpr(\paperwidth-\textwidth)/2\relax} The macro \settowidth is used with finding the width of a portion of text such as in: \newlength{\mytextwidth} \settowidth{\mytextwidth}{This is some text whose width I'm measuring} The documentation can be found by running texdoc etex from the ...


19

You can use the ε-TeX primitive \numexpr for expandable integer expressions. The only restriction is that it won't work in engines without ε-TeX extensions (mostly Knuth TeX nowadays), in which case you need to go with siracusa's solution. These macros being expandable, allows you to use them anywhere TeX expects a number, like in \ifnum, \...


18

This solution exploits \pgfmathisprime macro provided by Alain Matthes' tkz-euclide package. \documentclass{article} \usepackage{tkz-euclide} \newif\ifcomma \newcommand{\primes}[2]{% \commafalse% \foreach\numb in {#1,...,#2}{% \pgfmathisprime{\numb}% \ifnum\pgfmathresult=1 \ifcomma, \numb\else\numb\global\commatrue\fi% \fi% }% }...


18

Using PythonTeX: \documentclass{article} \usepackage{pythontex} \newcommand{\MyMathFunction}[1]{\py{2*(#1)**3 - 7}} \begin{document} \MyMathFunction{2} \end{document} Suppose the above is the content of the file my_math_fun_with_python.tex. You run LeTeX on the file: latex my_math_fun_with_latex then you run pythontex: pythontex.py ...


18

In expl3: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\rsqrt}{m} { \manual_rsqrt:n { #1 } } \int_new:N \l_manual_rsqrt_int \cs_new_protected:Nn \manual_rsqrt:n { \int_set:Nn \l_manual_rsqrt_int { \fp_to_decimal:n { trunc(sqrt(#1),0) } } \bool_until_do:nn { \int_compare_p:n { \int_mod:nn { #1 } { \...


Only top voted, non community-wiki answers of a minimum length are eligible