Primes & Modulo
What I have observed is that for the following expression, choose a positive integer $m$, and if it is prime then for positive integers $n=1,2,3,\ldots$ the results will be $0,1,1,1,\ldots$ such that the sequence is made of $0$'s and $1$'s which repeat in a way that there is a $0$, then $(m-1)$ $\times$ $1$'s and then again $0$ and $1$'s...
$$\sum^{m-1}_{k=0} n^k\mod{m}$$
For $m=2$ we have for $n=1,2,3,\ldots$ following results: $0,1,0,1,0,1,0,1,0,1,\ldots$
For $m=3$ we have: $0,1,1,0,1,1,0,1,1,0,1,1,\ldots$
For $m=5$ we have: $0,1,1,1,1,0,1,1,1,1,0,1,\ldots$
And for non-prime numbers we get a specific mix of repeating numbers that repeat every $m$ numbers, for example:
$m=6$, We get a repeating sets of $0,3,4,3,0,1$
$m=8$, We have: $0,7,0,5,0,3,0,1$
I have accidentally stumbled upon this property, in fact I don't know if its 100% true since I've checked a handful of numbers only.
Questions
Is there an explanation for this property, why does it work for prime numbers like that?
Is there any significance or any kind of a pattern in numbers that appear for non-prime numbers?
Is there any significance to this expression, and does this expression or something similar appear somewhere in mathematics? In a specific field, equations, theorems... somewhere in the mathematical history... or simply mentioned somewhere else before?
The sum of the power series can be written as:
$$\sum_{k=0}^{m-1} n^k = \frac{n^m-1}{n-1}$$
You then need to know the result:
So if $n-1$ is not divisible by $m$, then $\frac{n^m-1}{n-1} = \frac{n^m-n}{n-1} + 1\equiv 1\pmod {m}$.
When $n-1$ is divisible by $m$, then $n^k-1$ is divisible by $m$ for all $k$, and thus $$\sum_{k=0}^{m-1} n^k\equiv \sum_{k=0}^{m-1} (n^k-1) \equiv 0\pmod {m}$$