I need to write an algorithm where multiple cases are handled. I have succeeded but the output shows a lot of vertical space below and above the cases statement. Anyone help me to fix this. I am using algorithm2e package, a minimal working example with output is attached.
\documentclass[hidelinks,12pt]{report}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsmath}
\begin{document}
\begin{algorithm}
\DontPrintSemicolon
\caption{BE radix-8 IMML Modular Multiplication}
\label{alg1}
\KwIn{ $x=\sum_{i=o}^{n-1} x_i.2^i, y=\sum_{i=o}^{n-1} y_i.2^i, p=\sum_{i=o}^{n-1}p_i.2^i$ }
\KwOut {$z = x \times y$ \text{mod} $p$}
$z := 0$ $R_1 := 2x$ mod $p$
$R_2 := 3x$ mod $p$, $R_3 := 4x$ mod $p$\;
\[
N=
\begin{cases}
n+3,& \text{if } n \text{ mod } 3 = 0 \quad \tcc{\scriptsize append three 0 to the left of MSB of $y$}\\
n+2,& \text{if } n \text{ mod } 3 = 1 \quad \tcc{ \scriptsize append two 0 to the left of MSB of $y$}\\
n+1,& \text{if } n \text{ mod } 3 = 0 \quad \tcc{ \scriptsize append single 0 to the left of MSB of $y$}
\end{cases}
\]
\For {($i=N+1; i\geq 3; i=i-3 $) }{
$z := 8z $\text{ mod } $p$\;
\Switch{$(y_{(i:i-3)})$}{
\textbf{when} $ 0000 | 1111 \quad z:=z$ \;
\textbf{when}$\lbrace0001\rbrace|\lbrace0010\rbrace\lbrace1101\rbrace|\lbrace1110\rbrace \Longrightarrow z:=z \pm x$ mod $p$ \;
\textbf{when}$\lbrace0011\rbrace|\lbrace0100\rbrace\lbrace1011\rbrace|\lbrace1100\rbrace \Longrightarrow z:= z \pm R_1$ mod $p$ \;
\textbf{when}$\lbrace0101\rbrace|\lbrace0110\rbrace\lbrace1001\rbrace|\lbrace1010\rbrace$ $\Longrightarrow z:= z \pm R_2$ mod $p$\;
\textbf{else} $\Longrightarrow z:= z \pm R_3$ mod $p$
}
}
\Return{$z$}\;
\end{algorithm}
\end{document}
Best Answer
Is perhaps this that you want?