Order in Number Theory
Definition1
Let $\gcd (a, p) = 1$. The smallest natural number $e$ satisfying $a^{e} \equiv 1 \pmod{p}$ is written as $\text{ord}_{p} (a)$ and is defined as the order of $a$ modulo $p$.
Theorem
If $a^{n} \equiv 1 \pmod{p}$, then $\text{ord}_{p} (a) \mid n$.
Explanation
For example, considering $p=7$, we have $$ \begin{align*} 1^{1} \equiv & 1 \pmod{ 7 } \\ 2^{3} \equiv & 1 \pmod{ 7 } \\ 3^{6} \equiv & 1 \pmod{ 7 } \\ 4^{3} \equiv & 1 \pmod{ 7 } \\ 5^{6} \equiv & 1 \pmod{ 7 } \\ 6^{2} \equiv & 1 \pmod{ 7 } \end{align*} $$ Here the order of $6$ is $2$, the order of $2, 4$ is $3$, and the order of $3,5$ is $6$.
In particular, setting $n=p-1$ in the theorem above, one can easily check that $2,3,6$ divide $p-1= 6$. Moreover, by Fermat’s little theorem, for a prime $p$ we always have $a^{p-1} \equiv 1 \pmod{p}$, so we can see that $\text{ord}_{p} (a) \mid (p-1)$.
Proof
Setting $G := \gcd ( \text{ord}_{p} (a) , n )$, there exist $s,t$ satisfying $G = \text{ord}_{p}(a) \cdot s + n \cdot t$.
By the definition of the order and the assumption, $$ a^{G} = a^{ \text{ord}_{p}(a) \cdot s + n \cdot t} = \left( a^{ \text{ord}_{p}(a) } \right)^s \cdot \left( a^{n} \right)^{t} \equiv 1 \cdot 1 \pmod{p} $$ holds. Since $\text{ord}_{p}(a)$ was defined as the smallest natural number $e$ satisfying $a^{e} \equiv 1 \pmod{p}$, we have $G \ge \text{ord}_{p}(a)$; therefore $G = \text{ord}_{p}(a)$, and $\text{ord}_{p}(a) \mid p$.
■
Code
The following is code written in the R language that computes the order. The prime factorization code was used.
prime = read.table("../attachment
/cfile8.uf@25411C3C5968BBE322F0D4.txt"); prime = prime[,1]
factorize<-function(p)
{
q=p
factors<-numeric(0)
i=1; j=1
while(q!=1)
{
if(q%%prime[i]) {i=i+1}
else
{
q<-q/prime[i]
factors[j]<-prime[i]
i=1
j=j+1
}
}
return(factors)
}
order<-function(g,p,h=1) #Calculate a order of g in modulo p
{
qe<-table(factorize(p-1))
qe<-rbind(as.numeric(names(qe)),qe)
divisor<-qe[1,1]^(0:qe[2,1])
if((length(qe)/2)==1) {return(qe[1,1]^qe[2,1])}
for(i in 2:(length(qe)/2)) {divisor=c(divisor%*%t(qe[1,i]^(0:qe[2,i])))}
for(i in divisor) {if((FPM(g,i,p))%%p==1) break;}
return(i)
}
order(1,7)
order(2,7)
order(3,7)
order(4,7)
order(5,7)
order(6,7)
The following is the result of running the above code.

Silverman. (2012). A Friendly Introduction to Number Theory (4th Edition): p211. ↩︎
