TITLE: Find $\gcd(p^n-1,p^m+1)$.
QUESTION [8 upvotes]: Let $p$ be a prime number and $n, m$ positive integers. I want to find 

$\gcd(p^n+1, p^m+1)$ 
$\gcd(p^n-1, p^m+1)$

I know that 
$$\gcd(p^n-1, p^m-1)=p^{\gcd(n,m)}-1,$$
and it seems that if I can solve the first one, then the second one can be solved too, since (assume that $n>m$)
$$\gcd(p^n+1, p^m+1)=\gcd(p^n-p^m, p^m+1)=\gcd(p^m(p^{n-m}-1), p^m+1).$$
At last I have 
$$\gcd(p^n+1, p^m+1)=\gcd(p^{n-m}-1, p^m+1).$$
So, my problem is

Find $\gcd(p^n-1, p^m+1)$.

It seems to me that it should have some formula which appears in some where, but I can't remember it.

REPLY [2 votes]: An alternate answer for $\gcd(p^m+1,p^n+1)$. The other case can be converted to this form via
$$
\gcd(p^n-1,p^m+1) = \gcd(-(p^n-1)+p^n(p^m+1),p^m+1) = \gcd(p^{m+n}+1,p^m+1)
$$
We remark that $p$ can be any integer $\geq 1$ and not necessarily prime.  
Let $d=\gcd(m,n)$ and $m=ud,n=vd$. Denote
$$
x = p^d, \;\;\; M=p^m+1 = x^u+1, \;\;\;N=p^n+1 = x^v+1
$$
In the following the key method we use is the evaluation of a telescoping series
$$
\begin{align*}
(1+x^k)\sum_{i=0}^{l}(-x^k)^i &= (1+x^k)-(x^k+x^{2k})+(x^{2k}+x^{3k}) + \cdots +(-x^k)^l(1+x^k)\\
&= 1 + x^k(-x^k)^l
\end{align*}
$$
i.e. only first and last term remains.  
Case 1: exactly one of $u,v$ is even
We show that $D=\gcd(M,N)=1,2$ for even and odd $p$ respectively.
Let $A,B$ be
$$
\begin{align*}
A &= \sum_{i=0}^{v-1} (-x^u)^i, & B&= \sum_{i=0}^{u-1}(-x^v)^i
\end{align*}
$$
Then
$$
\begin{align*}
MA+NB &= \left((x^u+1)\sum_{i=0}^{v-1}(-x^u)^i\right) + \left((x^v+1)\sum_{i=0}^{u-1}(-x^v)^i \right) \\
&= (1+x^u(-x^u)^{v-1}) + (1+x^v(-x^v)^{u-1})\\
&= 2+(-1)^{v-1}x^{uv}+(-1)^{u-1}x^{uv}\\
&= 2
\end{align*}
$$
since $u,v$ have different parity. Therefore $D=\gcd(M,N)$ must divide 2. If $p$ is odd then $M,N$ are both even, giving $D=2$. Otherwise if $p$ is even, $M,N$ are both odd so we must have $D=1$.   
Case 2: $u,v$ both odd
We show that $D=\gcd(M,N) = p^d+1$ for any $p$.
Since $\gcd(u,v)=1$, we can find $r,s>0$ such that
$$
1+ur = vs
$$
As both $u,v$ are odd,
$$
1+r \equiv s \pmod 2
$$
i.e. $r$ and $s$ have different parity. Now let $A,B$ be
$$
\begin{align*}
A &= \sum_{i=0}^{r-1}(-x^u)^i &, \;\;\;\;B &= \sum_{i=0}^{s-1}(-x^v)^i
\end{align*}
$$
Then
$$
\begin{align*}
xMA+NB &= x\left((x^u+1)\sum_{i=0}^{r-1}(-x^u)^i\right)+\left( (x^v+1)\sum_{i=0}^{s-1}(-x^v)^i\right)\\
&= x(1+x^u(-x^u)^{r-1}) + (1 + x^v(-x^v)^{s-1})\\
&= x + (-1)^{r-1}x^{ur+1} + 1 + (-1)^{s-1} x^{vs}\\
&= (x+1) + (-1)^{r-1}x^{vs} + (-1)^{s-1}x^{vs}\\
&= x+1 \\
&= p^d+1
\end{align*}
$$
since $r,s$ have different parity. Once again $D=\gcd(M,N)$ must divide $p^d+1$. However since $m=ud,n=vd$ and $u,v$ are both odd, we can show that (*) $p^d+1 | p^{ud}+1$ and $p^d+1 | p^{vd}+1$. Therefore $D = p^d+1$.  $$\tag*{$\Box$}$$
(*) Since $u-1$ is even, we have
$$
\begin{align*}
(1+x)\sum_{i=0}^{u-1} (-x)^i &= 1 + x(-x)^{u-1} = 1 + x^u
\end{align*}
$$
Same applies for $v$.