Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Closed-form expression for sum of Modulo Arithmetic Progression

+3
−0

Is there any closed-form expression or at least an efficient way to calculate this sum?

$$ \sum_{i=1}^{N} (a \cdot i) \bmod{M} $$

we can assume $N$, $a$, and $M$ are large enough such that simple looping is not feasible and that period of the progression is also large.

I am aware of the way to calculate sum of first $N$ modulo natural numbers, but I am not able to extend that idea to this series.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Do you intend the mod to be outside the sum or inside it—i.e., are you expecting the final result to ... (2 comments)

1 answer

+1
−0

Assumed question

Placement of parentheses

Your final paragraph suggests that you would already know how to evaluate the following expression:

$$ \Bigl( \sum_{i=1}^{N} (a \cdot i) \Bigr) \bmod{M} $$

I will therefore assume that your question concerns the following expression:

$$ \sum_{i=1}^{N} \Bigl( (a \cdot i) \bmod{M} \Bigr) $$

Restriction to natural numbers

I will also assume that you are only interested in the cases where all of $N$, $a$, and $M$ are positive integers.

Hints towards a solution

You may find it easier to reach a solution by breaking this problem down into 2 cases.

Case 1: $a$ and $M$ are coprime

Here every contiguous set of $M$ terms will contain every non-negative integer less than $M$. This means that when $N$ is a multiple of $M$, the sum will be that same multiple $\frac{N}{M}$ of the sum of the first $M-1$ positive integers. Since the sum of the first $n$ positive integers is $\frac{n}{2}(n+1)$, the sum of the first $M-1$ positive integers (equivalently, the sum of the first $M$ non-negative integers) is $\frac{M(M-1)}{2}$.

When $N$ is not a multiple of $M$, there will be an additional $N\bmod{M}$ terms to add onto that sum. Note that these $N\bmod{M}$ terms will be identical to the first $N\bmod{M}$ terms, so the problem reduces to finding:

$$ \sum_{i=1}^{N \bmod{M}} \Bigl( (a \cdot i) \bmod{M} \Bigr) $$

So the final solution will be the sum of the identical repetitions, plus any left over terms:

$$ \left\lfloor\frac{N}{M}\right\rfloor \frac{M(M-1)}{2} + \sum_{i=1}^{N \bmod{M}} \Bigl( (a \cdot i) \bmod{M} \Bigr) $$

Case 2: $a$ and $M$ are not coprime

In this more general case, instead of repeating every $M$ terms, there will be repetition every $\frac{M}{\gcd(a,M)}$ terms.

When $N$ is a multiple of $\frac{M}{\gcd(a,M)}$ the sum will be that multiple $\frac{N\gcd(a,M)}{M}$ of the sum of the first $\frac{M}{\gcd(a,M)}$ terms.

When $N$ is not a multiple of $\frac{M}{\gcd(a,M)}$ there will be an additional $N \bmod{\frac{M}{\gcd(a,M)}}$ terms to add onto that sum. Note that these terms will be identical to the first $N \bmod{\frac{M}{\gcd(a,M)}}$ terms, so the problem reduces to finding:

$$ \sum_{i=1}^{N \bmod{\frac{M}{\gcd(a,M)}}} \Bigl( (a \cdot i) \bmod{M} \Bigr) $$

So the final solution will be the sum of the identical repetitions, plus any left over terms:

$$ \left\lfloor\frac{N\gcd(a,M)}{M}\right\rfloor \sum_{i=1}^{\frac{M}{\gcd(a,M)}} \Bigl( (a \cdot i) \bmod{M} \Bigr) + \sum_{i=1}^{N \bmod{\frac{M}{\gcd(a,M)}}} \Bigl( (a \cdot i) \bmod{M} \Bigr) $$

This does not give you a closed form solution, but it does give you something that in some cases will give a far faster solution by looping, and may also hint at potential further improvements.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »