Image

Escribir pseudocódigo usando el paquete algorithm

En ésta sección se presenta un paquete de Latex Presenta tus algoritmos en LaTeX con formato de pseudo-código.

\documentclass{article}
% Autor: Favian Arenas
% farenasenator@gmail.com
% 2021-06-25
\usepackage[papersize={8cm,14cm}]{geometry}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algorithmic}
% traducción
\floatname{algorithm}{Algoritmo}
\renewcommand{\listalgorithmname}{Lista de algoritmos}
\renewcommand{\algorithmicrequire}{\textbf{Entrada:}}
\renewcommand{\algorithmicensure}{\textbf{Salida:}}
\renewcommand{\algorithmicend}{\textbf{fin}}
\renewcommand{\algorithmicif}{\textbf{si}}
\renewcommand{\algorithmicthen}{\textbf{entonces}}
\renewcommand{\algorithmicelse}{\textbf{si no}}
\renewcommand{\algorithmicelsif}{\algorithmicelse,\ \algorithmicif}
\renewcommand{\algorithmicendif}{\algorithmicend\ \algorithmicif}
\renewcommand{\algorithmicfor}{\textbf{para}}
\renewcommand{\algorithmicforall}{\textbf{para todo}}
\renewcommand{\algorithmicdo}{\textbf{hacer}}
\renewcommand{\algorithmicendfor}{\algorithmicend\ \algorithmicfor}
\renewcommand{\algorithmicwhile}{\textbf{mientras}}
\renewcommand{\algorithmicendwhile}{\algorithmicend\ \algorithmicwhile}
\renewcommand{\algorithmicloop}{\textbf{repetir}}
\renewcommand{\algorithmicendloop}{\algorithmicend\ \algorithmicloop}
\renewcommand{\algorithmicrepeat}{\textbf{repetir}}
\renewcommand{\algorithmicuntil}{\textbf{hasta que}}
\renewcommand{\algorithmicprint}{\textbf{imprimir}} 
\renewcommand{\algorithmicreturn}{\textbf{devolver}} 
\renewcommand{\algorithmictrue}{\textbf{cierto }} 
\renewcommand{\algorithmicfalse}{\textbf{falso }} 
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
  \REQUIRE $n$
  \ENSURE  $suma$
  \STATE $suma \leftarrow 0$
  \FOR{$i\gets 1,n$}
  \STATE $suma\leftarrow suma+i^2$
  \ENDFOR
  \RETURN $suma$
\end{algorithmic} 
  
  \caption{Uso del entorno For}
  \label{a1}
\end{algorithm}

\begin{algorithm}
  \begin{algorithmic}[0]
    \REQUIRE $n$
    \ENSURE  $suma$
    \STATE $suma \leftarrow 0$
    \STATE $i \leftarrow 1$
    \WHILE{$i\leq n$}
    \STATE $suma\leftarrow suma+i^2$
    \STATE $i \leftarrow i+1$
    \ENDWHILE
    \RETURN $suma$
  \end{algorithmic}
  \caption{Uso del entorno While}
  \label{a2}
\end{algorithm}

\begin{algorithm}
  \begin{algorithmic}[0]
    \REQUIRE $n$
    \ENSURE  $out$
    \IF{$i \% n=0$}
    \STATE $out\leftarrow 1$
    \ELSE 
    \STATE $out\leftarrow 0$
    \ENDIF
    \RETURN $out$
  \end{algorithmic}
  \caption{Uso del entorno if}
  \label{a3}
\end{algorithm}
\begin{algorithm}
  \begin{algorithmic}[0]
    \REQUIRE $n, m$
    \ENSURE  $out$
    \IF{$m/n<10$}
    \STATE $out\leftarrow 1$
    \ELSIF{$m/n<5$} 
    \STATE $out\leftarrow 2$
    \ELSE
    \STATE $out\leftarrow 3$
    \ENDIF
    \RETURN $out$
  \end{algorithmic}
  \caption{Uso del entorno elseif}
  \label{a4}
\end{algorithm}

\end{document}

        

© Profesor Favián Arenas. 2023. Diseñado por HTML Codex