📚 The CoCalc Library - books, templates and other resources
cocalc-examples / martinthoma-latex-examples / source-code / Pseudocode / Calculate-Legendre / Calculate-Legendre.tex
132939 viewsLicense: OTHER
\documentclass{article}1\usepackage[pdftex,active,tightpage]{preview}2\setlength\PreviewBorder{2mm}34\usepackage[utf8]{inputenc} % this is needed for umlauts5\usepackage[ngerman]{babel} % this is needed for umlauts6\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf7\usepackage{amssymb,amsmath,amsfonts} % nice math rendering8\usepackage{braket} % needed for \Set9\usepackage{algorithm,algpseudocode}1011\begin{document}12\begin{preview}13\begin{algorithm}[H]14\begin{algorithmic}15\Require $p \in \mathbb{P}, a \in \mathbb{Z}, p \geq 3$16\Procedure{CalculateLegendre}{$a$, $p$}17\If{$a \geq p$ or $a < 0$}\Comment{rule (III)}18\State \Return $\Call{CalculateLegendre}{a \mod p, p}$ \Comment{now: $a \in [0, \dots, p-1]$}19\ElsIf{$a == 0$ or $a == 1$}20\State \Return $a$ \Comment{now: $a \in [2, \dots, p-1]$}21\ElsIf{$a == 2$} \Comment{rule (VII)}22\If{$p \equiv \pm 1 \mod 8$}23\State \Return 124\Else25\State \Return -126\EndIf \Comment{now: $a \in [3, \dots, p-1]$}27\ElsIf{$a == p-1$} \Comment{rule (VI)}28\If{$p \equiv 1 \mod 4$}29\State \Return 130\Else31\State \Return -132\EndIf \Comment{now: $a \in [3, \dots, p-2]$}33\ElsIf{!$\Call{isPrime}{a}$} \Comment{rule (II)}34\State $p_1, p_2, \dots, p_n \gets \Call{Factorize}{a}$35\State \Return $\prod_{i=1}^n \Call{CalculateLegendre}{p_i, p}$36\Else \Comment{now: $a \in \mathbb{P}, \sqrt{p-2} \geq a \geq 3$}37\If{$\frac{p-1}{2} \equiv 0 \mod 2$ or $\frac{a-1}{2} \equiv 0 \mod 2$}38\State \Return $\Call{CalculateLegendre}{p, a}$39\Else40\State \Return $(-1) \cdot \Call{CalculateLegendre}{p, a}$41\EndIf42\EndIf43\EndProcedure44\end{algorithmic}45\caption{Calculate Legendre symbol}46\label{alg:calculateLegendreSymbol}47\end{algorithm}48\end{preview}49\end{document}505152