Find First element in AP which is multiple of given prime
Last Updated :
23 May, 2022
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.
Examples:
Input: A = 4, D = 9, P = 11
Output: 2
Explanation :
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
Output: 5
Explanation :
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
Approach:
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:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int NearestElement( int A, int D, int P)
{
if (A == 0)
return 0;
else if (D == 0)
return -1;
else {
int X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
int main()
{
int A = 4, D = 9, P = 11;
A %= P;
D %= P;
cout << NearestElement(A, D, P);
return 0;
}
|
C
#include <stdio.h>
int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int NearestElement( int A, int D, int P)
{
if (A == 0)
return 0;
else if (D == 0)
return -1;
else {
int X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
int main()
{
int A = 4, D = 9, P = 11;
A %= P;
D %= P;
printf ( "%d" ,NearestElement(A, D, P));
return 0;
}
|
Java
class GFG
{
static int power( int x,
int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
if ((y & 1 ) != 0 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int NearestElement( int A,
int D, int P)
{
if (A == 0 )
return 0 ;
else if (D == 0 )
return - 1 ;
else
{
int X = power(D, P - 2 , P);
return (X * (P - A)) % P;
}
}
public static void main(String args[])
{
int A = 4 , D = 9 , P = 11 ;
A %= P;
D %= P;
System.out.println(NearestElement(A, D, P));
}
}
|
Python 3
def power(x, y, p) :
res = 1
x = x % p
while y > 0 :
if y & 1 :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def NearestElement(A, D, P) :
if A = = 0 :
return 0
elif D = = 0 :
return - 1
else :
X = power(D, P - 2 , P)
return (X * (P - A)) % P
if __name__ = = "__main__" :
A, D, P = 4 , 9 , 11
A % = P
D % = P
print (NearestElement(A, D, P))
|
C#
using System;
class GFG
{
static int power( int x,
int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int NearestElement( int A,
int D, int P)
{
if (A == 0)
return 0;
else if (D == 0)
return -1;
else
{
int X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
public static void Main()
{
int A = 4, D = 9, P = 11;
A %= P;
D %= P;
Console.WriteLine(NearestElement(A, D, P));
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function NearestElement( $A , $D , $P )
{
if ( $A == 0)
return 0;
else if ( $D == 0)
return -1;
else
{
$X = power( $D , $P - 2, $P );
return ( $X * ( $P - $A )) % $P ;
}
}
$A = 4; $D = 9; $P = 11;
$A %= $P ;
$D %= $P ;
echo NearestElement( $A , $D , $P );
?>
|
Javascript
<script>
function power(x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function NearestElement(A, D, P)
{
if (A == 0)
return 0;
else if (D == 0)
return -1;
else
{
let X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
let A = 4, D = 9, P = 11;
A %= P;
D %= P;
document.write(NearestElement(A, D, P));
</script>
|
Time Complexity: O(log y)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...