Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If $a$ and $p$ are relatively coprime integers, then is there any efficient way of calculating the following: $(\sum_{k=0}^n\frac{1}{a^k}) \% p$ ? I'm interested in the cases when $0 \leq n ≤ 2147483647$ and $2 ≤ p ≤ 2147483647$. Thanks.

share|cite|improve this question

Note that $\sum_{k=0}^n \frac{1}{a^k} = \frac{a^{n+1}-1}{a-1}$. If $n$ is prime then the Euler-Fermat theorem simplifies things a little: $a^{n+1} \equiv a^2 \mod n$. Then your answer becomes $\frac{a^2-1}{a-1} = a+1 \mod n$. But in other situations I'm not sure you get such a nice answer.

share|cite|improve this answer
thanks..but here p is prime and co-prime to a and not n , still no standard formula? – pranay Nov 13 '11 at 21:43
You're getting an answer mod $n$, but the question asked for the sum mod $p$. – Gerry Myerson Nov 13 '11 at 23:27
@pranay, there's no formula for $a^{n+1}\pmod p$ better than $a^{n+1}\pmod p$ (unless there is some relation between $p$ and $n$), so do you think there will be a better formula than $(a^{n+1}-1)/(a-1)$ for your question? – Gerry Myerson Nov 13 '11 at 23:31
no. but besides using exponentiation by squaring, can we use modular exponentiation here ? – pranay Nov 14 '11 at 3:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.