Given the first term (A) and common difference (D) of an Arithmetic Progression, and a prime number (P). The task is to find the position of the first element in the given AP which is a multiple of the given prime number P.
Input: A = 4, D = 9, P = 11
The third term of the given AP is
a multiple of prime number 11.
First Term = 4
Second Term = 4+9 = 13
Third Term = 4+2*9 = 22
Input: A = 5, D = 6, P = 7
The sixth term of the given AP is
a multiple of prime number 7.
First Term = 5
Second Term = 5+6 = 11
Third Term = 5+2*6 = 17
Fourth Term = 5+3*6 = 23
Fifth Term = 5+4*6 = 29
Sixth Term = 5+5*5 = 35
Let the term be AN. Therefore,
AN = (A + (N-1)*D)
Now, it is given that AN is a multiple of P. Then,
A + (N-1)*D = k*P Where, k is a constant.
Now let A be (A % P) and D be (D % P). So, we have (N-1)*D = (k*P – A).
Adding and subtracting P on RHS, we get:
(N-1)*D = P(k-1) + (P-A), Where P-A is a non-negative number (since A is replaced by A%P which is less than P)
Finally taking mod on both sides:
((N-1)*D)%P = (P-A)%P or, ((N-1)D)%P = P-A
Lets find a X < P, such that (D*X)%P = 1. This X is known as the inverse modulo of D with respect to P.
Thus answer N is:
((X*(P-A)) % P) + 1.
Below is the implementation of above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find Nth number in a sequence which is not a multiple of a given number
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Rearrange the given string such that all prime multiple indexes have same character
- Find the multiple of x which is closest to a^b
- Smallest multiple of 3 which consists of three given non-zero digits
- Sum of each element raised to (prime-1) % prime
- Find Largest Special Prime which is less than or equal to a given number
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Prime Factorization using Sieve O(log n) for multiple queries
- Smallest N digit number which is a multiple of 5
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- First N terms whose sum of digits is a multiple of 10
- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in a given range whose count of prime factors is a Prime Number
- Find the prime numbers which can written as sum of most consecutive primes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.