Skip to content

Instantly share code, notes, and snippets.

@ShaneTsui
Last active April 20, 2019 20:06
Show Gist options
  • Select an option

  • Save ShaneTsui/8993591e4636b68b42bae81579a4349b to your computer and use it in GitHub Desktop.

Select an option

Save ShaneTsui/8993591e4636b68b42bae81579a4349b to your computer and use it in GitHub Desktop.
[tex pseudocode] # latex # pseudocode
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{algorithm}
\caption{Calculate $y = x^n$}
\begin{algorithmic}
\REQUIRE $n \geq 0 \vee x \neq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\IF{$n < 0$}
\STATE $X \leftarrow 1 / x$
\STATE $N \leftarrow -n$
\ELSE
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
\ENDIF
\WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$
\STATE $N \leftarrow N / 2$
\ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$
\STATE $N \leftarrow N - 1$
\ENDIF
\ENDWHILE
\end{algorithmic}
\end{algorithm}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment