# \power{n}{k} The power nk, but without \multiply

How to do this using macros in TeX. Something like \power{n}{k} - the power of n to the value k - but without using \multiply.

Something like this needs to be done

k_{n+1} = n^2 + k_n^2 - k_{n-1}

-
It is unclear what your question means. Please provide some code demonstrating what you want to achieve. Do you want to typeset a formula or calculate something with a macro? –  Stephan Lehmke Apr 10 '12 at 17:20
A macros in TEX that compute The power nk, but without \multiply –  Andy Apr 10 '12 at 17:27
If you'd try to do it yourself (using \numexpr) and show where you get stuck, you'd give people here something to start from. Or else, explain a little more about the context you need this for. The way you ask, it looks like you're asking people here to do your homework for you. –  Stephan Lehmke Apr 10 '12 at 17:30
Why without \multiply? –  Joseph Wright Apr 10 '12 at 17:43

Assuming you want to work with integers, and e-TeX is available, something like

\documentclass{article}
\newcommand*{\power}[2]{%
\the\numexpr\poweraux{#1}{#2}\relax
}
\newcommand*{\poweraux}[2]{%
\ifnum\numexpr#2 = 0 %
\expandafter\powerend
\else
#1 * \expandafter\poweraux
\fi
{#1}{#2 - 1}%
}
\newcommand*{\powerend}[2]{1}
\begin{document}
\power{2}{3}
\end{document}


seems about right. I've not tested for edge cases or made this 'maximum efficiency' as I'm not entirely sure what is required.

-

Use the fp package (or pgfmath from the pgf bundle) to produce floating point calculations without having to manually specify \multiply, \divide, or the like. Here's a small example:

\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\begin{document}
\FPeval\result{round(3^4:0)}%
$3^4=​​​​​​​​​​​​​​​​​​​​​​​​​​​\result$
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


See the fp README for more information on the notation and usage.

-