Consider the formula:
$$ f : \Bbb{N} \to \Bbb{C} \\ f(n) := 2 + \sum_{a = 1}^{n-2} \exp\left({\dfrac{2\pi i}{\gcd(a, n^2-1)}}\right) $$
If $n \pm 1$ is a pair of twin primes, then $f(n) = n$. This is because the power of $e$ will be an integer $m$ and thus $e^{i2\pi m} = e^{0} = 1$ gives us in the sum a total number of $n-2$ ones.
Question.
Conversely, if $f(n) = n$ does it follow that $n \pm 1$ is a pair of twin primes?
Another map $g : \Bbb{N} \to \Bbb{N}$ is the related:
$$ g(n) := 2 + \sum_{a = 1}^{n-2} \gcd(a, n^2 -1) $$
and it happens to be the case that $g(n) = n$ is satisfied if and only if $n\pm 1$ is a pair of twin primes, or in other words:
$$ n \pm 1 \text{ are twin primes} \\ \iff \gcd(a, n^2 -1) = 1 \ \ \forall a = 1..(n-2) $$
Evidence for conjecture:
from sympy import isprime
from cmath import exp, pi
from math import gcd
N = 100000
def f(n):
s = 2
for a in range(1, n-2 + 1):
s += exp((2*pi*1j)/gcd(a, n*n - 1))
return s
for n in range(1, N):
p = n - 1
q = n + 1
if isprime(p) and isprime(q):
x = f(n)
if (abs(x.imag) <= 0.00000001 and int(x.real) != n):
print("f(", n, ")=", x)
print("False")
else:
print("f(", n, ")=", n)
which prints:
f( 4 )= 4
f( 6 )= 6
f( 12 )= 12
f( 18 )= 18
f( 30 )= 30
f( 42 )= 42
f( 60 )= 60
f( 72 )= 72
....
f( 34512 )= 34512
f( 34590 )= 34590
f( 34650 )= 34650
f( 34758 )= 34758
f( 34842 )= 34842
f( 34848 )= 34848
f( 34962 )= 34962
f( 35052 )= 35052
....
Note that, if $n-1$ is not $1$ or prime, there is some prime factor $p|n-1$ so that $1\leq p\leq n-2$, and if $n+1$ is not $1$, $4$, or prime, there is some prime factor $p|n+1$ so that $1\leq p\leq n-2$, so we have that $$\gcd(a,n^2-1)=1\text{ for all }1\leq a\leq n-2$$ if and only if $n-1$ and $n+1$ are both primes, or $n\in\{2,3\}$. This should immediately give your second result (or, at least, a true version of it). Your first conjecture is also true for the same reason, since a sum of $n-2$ roots of unity is $n-2$ if and only if they are all $1$.