TITLE: Interesting cyclic infinite nested square roots of 2 and cosine values
QUESTION [5 upvotes]: It is interesting to note that any angle between 45° to 90° satisfying $1\over4$ < $p \over q$ <$1\over2$ where $ p \over q$ is of form $p = 2^n $ and $q$ is an odd number satisfying $2^{n+1} <q <2^{n+2}$ can be represented as cyclic infinite nested square roots of 2 (Hereafter referred as $cin\sqrt2$) refer here for an example of interesting cyclic infinite nested radical

Let us consider certain cosine angles with odd numbers starting from 5 and its exponents and see the number of $'+'$ and $'-'$ signs in single cycle

$2\cos(\frac{2\pi}{5})=\sqrt{2+2\cos(\frac{4\pi}{5}})=\sqrt{2-2\cos(\frac{\pi}{5}})=\sqrt{2-\sqrt{2+2\cos(\frac{2\pi}{5}}})$. We can observe expansion goes infinite and results in cyclic infinite nested square roots of 2 for $2\cos(\frac{2\pi}{5})$ or $2\cos72^\circ$
$2\cos\frac{2\pi}{5} = cin\sqrt2[1-1+]$ single cycle contains $1 -$ sign and $1 +$ sign and is simple representation of $\sqrt{2-\sqrt{2+...}}$
$2\cos(\frac{8\pi}{25}) = \sqrt{2+2\cos(\frac{16\pi}{25}}) = \sqrt{2-2\cos(\frac{9\pi}{25}}) = \sqrt{2-\sqrt{2+2\cos(\frac{18\pi}{25}}}) = \sqrt{2-\sqrt{2-2\cos(\frac{7\pi}{25}}}) = \sqrt{2-\sqrt{2-\sqrt{2+2\cos(\frac{14\pi}{25}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-2\cos(\frac{11\pi}{25}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+2\cos(\frac{22\pi}{25}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-2\cos(\frac{3\pi}{25}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+2\cos(\frac{6\pi}{25}}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+\sqrt{2+2\cos(\frac{12\pi}{25}}}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2+2\cos(\frac{24\pi}{25}}}}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2-2\cos(\frac{\pi}{25}}}}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2-\sqrt{2+2\cos(\frac{2\pi}{25}}}}}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2-\sqrt{2+\sqrt{2+2\cos(\frac{4\pi}{25}}}}}}}}}}) = \sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2+2\cos(\frac{8\pi}{25}}}}}}}}}}})$
Above cycle repeats infinitely
$2\cos\frac{8\pi}{25} = cin\sqrt2[4-2+1-3+]$ single cycle contains $5 +$ signs and $5 -$ signs and is simple representation of $\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2+...}}}}}}}}}}$
Steps can be simplified as follows.

When the angle exceeds $\frac{\pi}{2}$ we are applying the basic cosine angle identity $2\cos(\pi-\theta) = -2\cos(\theta)$
Doubling happens infinitely with $\frac{\pi}{4} < \theta < \pi$
As the denominator in the angle is odd number, when the $\frac{numerator}{denominator} > \frac{1}{2}$ the signs changes while doubling the cosine angle expansion as nested radical.

Above steps are programmable.
I have created a program to calculate the number of $'+'$ and $'-'$ signs in cyclic infinite nested square roots of 2 for cosine values in Python as follows and anyone can verify which will provide the result for any odd number other than 3 (I am not expert in this)
import time
n = int(input("Enter an odd number to get single cycle cinsqrt2 ")) #odd number is denominator
# steps to calculate numerator as $2^n$ so that fraction lies between 0.25 and 0.5
i = 0
for i in range(n):
    if 2 ** i > n and n < 2 ** (i + 1):
        break
numerator = 2 ** (i - 2)
print("Numerator is", numerator)
#print("The Angle generated is", (numerator*180)/n, (numerator*180)//n,'+', ((numberator*180)%n)/n)
halfway_of_n = (n - 1) // 2 #to decide the sign, we need halfway number
# print("Half way to n is", halfway_of_n)
lst = []
r = numerator * 2
begin = time.time()
while r != numerator:
    if r > halfway_of_n:
        r = n - r
        r = r * 2
        lst = lst + ['-']

    else:
        r = r * 2
        lst = lst + ['+']
lst = lst + ['+']
print(len(lst))
count = lst.count('-')
print('No of minus signs is/are ', count)
count = lst.count('+')
print('No of plus signs is/are ', count)
print(lst) #list containing '+' and '-' signs

end = time.time()

print('Program execution time is', end - begin)

$2\cos\frac{32\pi}{125} = cin\sqrt2[2-4+1-1+1-2+2-1+2-1+1-1+2-2+3-1+3-3+1-2+1-1+5-1+1-5+]$ single cycle contains $25 +$ signs and $25 -$ signs and (representing this as a infinite nested radical will be big and occupy big space. Therefore I'm representing in the simplified form as shown above)
Exciting pattern emerges for cosine values having exponent of 5 in the denominator and number of + and - signs in the $cin\sqrt2$ which also exponentially growing as follows

for $5^1$ in denominator $5^0 + 5^0$ signs in total (in single cycle)


for $5^2$ in denominator  $5^1 + 5^1$ signs in total (in single cycle)


for $5^3$ in denominator  $5^2 + 5^2$ signs in total (in single cycle)


for $5^4$ in denominator  $5^3 + 5^3$ signs in total (in single cycle)

and so on
Question:
Any other way to get these signs inside the nested radicals in a better way?

REPLY [4 votes]: Main Question(s)
I'd like to generalize the setup of the OP a bit, and strip out some of the context to focus on the main idea.
Setup
Let $q>2$ be an odd integer and let $p$ be an integer with $0<p<\dfrac{q}2$.
Then note that $0<\cos\left(\dfrac{p\pi}{q}\right)$. We can use a version of the cosine half angle formula to find that $2\cos\left(\dfrac{p\pi}{q}\right)=\sqrt{2+2\cos\left(\dfrac{2p\pi}{q}\right)}$. If $2p>\dfrac{q}2$, the inner cosine is no longer positive; in that case, we can first use $\cos(\pi-x)=-\cos(x)$ to write $2\cos\left(\dfrac{2p\pi}{q}\right)=-2\cos\left(\dfrac{(q-2p)\pi}{q}\right)$, and have $\cos\left(\dfrac{(q-2p)\pi}{q}\right)>0$.
This means that, for $0<p_1<\dfrac{q}2$, we can choose $0<p_2<\dfrac{q}2$ so that $2\cos\left(\dfrac{p_1\pi}{q}\right)=\sqrt{2\pm2\cos\left(\dfrac{p_2\pi}{q}\right)}$ where we have a $+$ sign and $p_2=2p_1$ if $2p_1<\dfrac{q}2$ and we have a $-$ sign and $p_2=q-2p_1$ otherwise. Repeating this step gives rise to a "$p$ sequence": $p_1,p_2,\ldots$.
But by doing calculations, it seems empirically that all the sequences for a given $q$ and starting $p$ are actually periodic, which would mean that we can always write $2\cos\left(\dfrac{p\pi}{q}\right)$ as a nested radical of the form described above, with $2\cos\left(\dfrac{p\pi}{q}\right)$ on the inside (suggesting a sort of infinite nesting).
For example, with $q=17$ and an initial $p=8$, we obtain $$2\cos\left(\dfrac{8\pi}{17}\right)=\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2+2\cos\left(\dfrac{8\pi}{17}\right)}}}}$$
Questions

Is the sequence of $p$ values guaranteed to be periodic (so that we can always write these cosines as these special nested radicals)?
When it is periodic, is there another way to find/understand the pattern of $+$ and $-$ signs in the radical expression?

Main Answers
Answer 1
Yes, under our assumption that $q$ is odd, the sequence of $p$ values is guaranteed to be periodic. Note that if $q$ is not odd, then this would not be guaranteed as in the calculation of $2\cos(1^\circ)=2\cos\left(\dfrac{1\pi}{180}\right)$ from the previous question.
Answer 2
Starting with $p$, the sequence of $+$/$-$ signs can be written with the modulo operation, borrowing % from Python, as follows:
$$n^\text{th}\text{ sign}=\begin{cases}
+ & \text{ if }\left(2^{n}p\right)\%\left(2q\right)<\dfrac{q}{2}\\
+ & \text{ if }\left(2^{n}p\right)\%\left(2q\right)>\dfrac{3q}{2}\\
- & \text{ otherwise}
\end{cases}$$
We can also phrase this in an equivalent and suggestive way using quadrants of the plane and the standard position of angles greater than $2\pi$.
$$n^\text{th}\text{ sign}=\begin{cases}
+ & \text{ if }\dfrac{2^{n}p\pi}{q}\text{ is in Quadrant I or IV}\\
- & \text{ otherwise}
\end{cases}$$
Using $+1$ and $-1$ to represent the signs, and the sign function, this is equivalent to
$$n^\text{th}\text{ sign}=\mathop{\mathrm{sgn}}\left(\cos\left(\dfrac{2^{n}p\pi}{q}\right)\right)$$
Proof of 1
Even without our assumption that $q$ is odd, since there are only finitely many values that the $p$s can take on, the sequence must be eventually periodic by the pigeonhole principle.
But if $q$ is odd, the sequence is guaranteed to be periodic. It may be easiest to show this using some facts about groups.
Let $G_q$ be the multiplicative group of integers modulo $q$. Then we can quotient out by the $2$-element subgroup represented by $\{1,-1\}$ to get a new group $G_q^*$, with elements of the form $[n]$ where $0<n<q/2$ and $n$ is relatively prime to $q$. Since $q$ is odd, $2$ is a representative of an element of $G_q$, and hence $G_q^*$. And since $p$ and $q$ only appear in a fraction, we can assume WLOG that $p$ and $q$ are relatively prime, so that $[p]\in G_q^*$.
Since $[2]$ generates a subgroup of the finite group $G_q^*$, the sequence $[p],[2][p],[2]^2[p],\ldots$ must be periodic. But note that if $[n]\in G_q^*$, then $[2][n]=[2n]=[-2n]=[q-2n]$, so that the smallest positive representatives of $[p],[2][p],[2]^2[p],\ldots$ are exactly the terms of the original $p$ sequence.
Proof of 2
Note that the first sign for $p$ corresponds to $\mathop{\mathrm{sgn}}\left(\cos\left(\dfrac{2p\pi}{q}\right)\right)$, and then for the next sign we start from a new value of $p$ based on that sign. By the $2\pi$ periodicity of $\cos$ and the denominator of $q$ in $\cos\left(\dfrac{m\pi}{q}\right)$, the signs can only depend on $(2^np)\%(2q)$.
Certainly, if $(2^np)\%(2q)<\dfrac{q}{2}$, then the residue $r=(2^np)\%(2q)$ of $2^np$ modulo $2q$ is the same as the residue modulo $q$; and this residue is small enough that there is no $q-r$ step needed, so the sign is $+1$.
Similarly, if $\dfrac{q}{2}<(2^np)\%(2q)<q$, then the residue $r$ of $2^np$ modulo $2q$ is the same as the residue modulo $q$; but this residue is large enough we have a $q-r$ step and the sign is $-1$.
And if $\dfrac{3q}{2}<(2^np)\%(2q)$, then if $R,r$ are the residues modulo $2q,q$, respectively, we have $R-q=r$. And since $R>\dfrac{3q}{2}$, $r>\dfrac{q}2$, so that we are dealing with $q-r=q-(R-q)=2q-R<\dfrac{q}{2}$. Thus, the sign is $+1$.
Similarly, if $q<(2^np)\%(2q)<\dfrac{3q}{2}$, then the sign is $-1$.
Context
I'd like to share how I found references covering these results, and some other related facts about this construction. For $q=25$, I noted the $p$ sequence (starting from $p=1$ instead of $p=8$) $1,2,4,8,9_{=25-16},7,11,3,6,12,1,\ldots$. If you search Sloane's for 1,2,4,8,9,7, you find just one sequence: A334430. This sequence basically lists the all the $p$ sequences ("cycles") for all the odd $q$, except that they start cycles at twice the lowest number of the cycle, by convention. The $1$, in "$1,2,4,8,9,7$", that was found was actually the end of the sole cycle for $q=23$, not the beginning of a cycle for $q=25$.
References
The page for A334430 mentions and links many relevant texts, including (in chronological order of first publication):

"Polygons and Chaos" by Jay Kappraff and Gary W. Adamson, published in Journal of Dynamical Systems and Geometric Theories and earlier in the freely-available proceedings of the Bridges conference
Trigonometrie und unterhaltsame Zahlentheorie by Carl Schick (Table of Contents scan provided by Deutsche Nationalbibliothek.
A Mathematical Tapestry: Demonstrating the Beautiful Unity of Mathematics by Peter Hilton and Jean Pedersen
"Modified Congruence Modulo $n$ with Half The Amount of Residues" by Tim Beyne and Gerold Brändli (arXiv)
"On the Equivalence of Three Complete Cyclic Systems of Integers" by Wolfdieter Lang (arXiv)

The above approach to proving periodicity came from sections 3.1 and 3.2 of Beyne and Brändli. And the proof of the sign pattern was a direct adaptation of the proof of Lemma 19 in Lang.
mod* Congruence
Building on the work of Schick, Beyne and Brändli investigate properties of the group $G_n^*$, where we consider integers $a,b$ to be congruent $(\mathrm{mod}^*n)$ if $a-b$ or $a+b$ is a multiple of $n$. Many theorems and conjectures of elementary number theory carry over from $\mathrm{mod}$ to $\mathrm{mod}^*$
Period length
One missing ingredient to finding the exact sign sequence for a single period of the $p$ sequence is the length of the period. Otherwise, it's not clear when to cut off the infinite sign sequence from answer 2 above. This period is mentioned in all 5 references above, and is A003558. It could be written "the order of $[2]$ in $G_q^*$" or "the least number $m>0$ such that $2^m\equiv\pm1\pmod q$".
Cosine Polynomials
Rather than turning a nested radical representation into an infinite one, we can use it to find a polynomial that a given cosine value is a root of. For example, $$2\cos\left(\dfrac{8\pi}{17}\right)=\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2+2\cos\left(\dfrac{8\pi}{17}\right)}}}}$$ means that $2\cos\left(\dfrac{8\pi}{17}\right)$ is a root of $x=\sqrt{2-\sqrt{2+\sqrt{2+\sqrt{2+x}}}}$, and hence of $\left(\left(\left(2-x^{2}\right)^{2}-2\right)^{2}-2\right)^{2}-2=x$. Rewriting, it is a zero of $x^{16}- 16x^{14}+ 104x^{12}- 352x^{10}+ 660x^8 - 672x^6+ 336x^4- 64x^2 +x+2$.
Unfortunately, this is not the minimal polynomial for $2\cos\left(\dfrac{8\pi}{17}\right)$. That would be $x^8-x^7-7x^6+6x^5+15x^4-10x^3-10x^2+4x+1$, whose coefficients are listed in A136571. Minimal polynomials for the cosines whose $p$ sequence contains $1$ are discussed in Lang, as well as its reference "The field $\bf \mathbb Q(2\, cos\left(\frac{\boldsymbol{\pi}}{n}\right))$, its Galois group, and length ratios in the regular $\bf n$-gon" (arXiv), also by Lang.