- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find First element in AP which is multiple of given Prime in Python

Suppose we have a first term (A) and common difference (d) of an AP series, and we also have a prime number P, we have to find the position of the first element in the given AP which is the a multiple of the given prime number P.

So, if the input is like A = 3, D = 4, P = 5, then the output will be 3 as fourth term of the given AP is a multiple of the prime number 5. So, first term = 3, second term = 3+4 = 7, third term = 3+2*4 = 11 and fourth term = 3+3*4 = 15.

To solve this, we will follow these steps −

Define a function get_pow() . This will take x, y, p

ans := 1

x := x mod p

while y > 0, do

if y AND 1 is non-zero, then

ans :=(ans * x) mod p

y := y/2

x :=(x * x) mod p

return ans

From the main method, do the following −

A := A mod P

D := D mod P

if A is same as 0, then

return 0

otherwise when D is same as 0, then

return -1

otherwise,

X := get_pow(D, P - 2, P)

return(X *(P - A)) mod P

## Example

Let us see the following implementation to get better understanding −

def get_pow(x, y, p) : ans = 1 x = x % p while y > 0 : if y & 1 : ans = (ans * x) % p y = y >> 1 x = (x * x) % p return ans def get_nearest(A, D, P) : A %= P D %= P if A == 0 : return 0 elif D == 0 : return -1 else : X = get_pow(D, P - 2, P) return (X * (P - A)) % P A = 3 D = 4 P = 5 print(get_nearest(A, D, P))

## Input

A = 3 D = 4 P = 5

## Output

3

- Related Articles
- Find First element in AP which is multiple of given Prime in C++
- Find if a number is part of AP whose first element and difference are given using C++.
- Find the sum of first 22 terms of an AP in which $d = 7$ and 22nd term is 149.
- The sum of first ( n ) terms of an AP is given by ( mathrm{S}_{n}=4 n^{2}+n ). Find that ( mathrm{AP} ).
- Which term of the AP: ( 53,48,43, ldots ) is the first negative term?
- Find First and Last Position of Element in Sorted Array in Python
- Find the Product of first N Prime Numbers in C++
- Which term of the AP: 121, 117. 113, ….., is its first negative term?
- Find element position in given monotonic sequence in Python
- Python Program to Find the first Common Element between the 2 given Linked Lists
- Program to find all prime factors of a given number in sorted order in Python
- Find Largest Special Prime which is less than or equal to a given number in C++
- Get first element of each sublist in Python
- Find the maximum element in an array which is first increasing and then decreasing in C++
- Find the mean of first five prime numbers