Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
\documentclass{article}
2
\usepackage[pdftex,active,tightpage]{preview}
3
\setlength\PreviewBorder{2mm}
4
5
\usepackage[utf8]{inputenc} % this is needed for umlauts
6
\usepackage[ngerman]{babel} % this is needed for umlauts
7
\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
8
\usepackage{amssymb,amsmath,amsfonts} % nice math rendering
9
\usepackage{braket} % needed for \Set
10
\usepackage{algorithm,algpseudocode}
11
12
\begin{document}
13
\begin{preview}
14
\begin{algorithm}[H]
15
\begin{algorithmic}
16
\Require $p \in \mathbb{P}, a \in \mathbb{Z}, p \geq 3$
17
\Procedure{CalculateLegendre}{$a$, $p$}
18
\If{$a \geq p$ or $a < 0$}\Comment{rule (III)}
19
\State \Return $\Call{CalculateLegendre}{a \mod p, p}$ \Comment{now: $a \in [0, \dots, p-1]$}
20
\ElsIf{$a == 0$ or $a == 1$}
21
\State \Return $a$ \Comment{now: $a \in [2, \dots, p-1]$}
22
\ElsIf{$a == 2$} \Comment{rule (VII)}
23
\If{$p \equiv \pm 1 \mod 8$}
24
\State \Return 1
25
\Else
26
\State \Return -1
27
\EndIf \Comment{now: $a \in [3, \dots, p-1]$}
28
\ElsIf{$a == p-1$} \Comment{rule (VI)}
29
\If{$p \equiv 1 \mod 4$}
30
\State \Return 1
31
\Else
32
\State \Return -1
33
\EndIf \Comment{now: $a \in [3, \dots, p-2]$}
34
\ElsIf{!$\Call{isPrime}{a}$} \Comment{rule (II)}
35
\State $p_1, p_2, \dots, p_n \gets \Call{Factorize}{a}$
36
\State \Return $\prod_{i=1}^n \Call{CalculateLegendre}{p_i, p}$
37
\Else \Comment{now: $a \in \mathbb{P}, \sqrt{p-2} \geq a \geq 3$}
38
\If{$\frac{p-1}{2} \equiv 0 \mod 2$ or $\frac{a-1}{2} \equiv 0 \mod 2$}
39
\State \Return $\Call{CalculateLegendre}{p, a}$
40
\Else
41
\State \Return $(-1) \cdot \Call{CalculateLegendre}{p, a}$
42
\EndIf
43
\EndIf
44
\EndProcedure
45
\end{algorithmic}
46
\caption{Calculate Legendre symbol}
47
\label{alg:calculateLegendreSymbol}
48
\end{algorithm}
49
\end{preview}
50
\end{document}
51
52