Calculate-Legendre.tex 1.8 KB

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