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;
// Iterative Function to calculate
// (x^y)%p in O(log y) */
int power(int x, int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// function to find nearest element in common
int NearestElement(int A, int D, int P)
{
// base conditions
if (A == 0)
return 0;
else if (D == 0)
return -1;
else {
int X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
// Driver code
int main()
{
int A = 4, D = 9, P = 11;
// module both A and D
A %= P;
D %= P;
// function call
cout << NearestElement(A, D, P);
return 0;
}
C
#include <stdio.h>
// Iterative Function to calculate
// (x^y)%p in O(log y) */
int power(int x, int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// function to find nearest element in common
int NearestElement(int A, int D, int P)
{
// base conditions
if (A == 0)
return 0;
else if (D == 0)
return -1;
else {
int X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
// Driver code
int main()
{
int A = 4, D = 9, P = 11;
// module both A and D
A %= P;
D %= P;
// function call
printf("%d",NearestElement(A, D, P));
return 0;
}
// This code is contributed by kothavvsaakash.
Java
// Java Program to Find First
// element in AP which is
// multiple of given prime
class GFG
{
// Iterative Function to
// calculate (x^y)%p in
// O(log y) */
static int power(int x,
int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is
// more than or equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// function to find nearest
// element in common
static int NearestElement(int A,
int D, int P)
{
// base conditions
if (A == 0)
return 0;
else if (D == 0)
return -1;
else
{
int X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
// Driver code
public static void main(String args[])
{
int A = 4, D = 9, P = 11;
// module both A and D
A %= P;
D %= P;
// function call
System.out.println(NearestElement(A, D, P));
}
}
// This code is contributed
// by Arnab Kundu
Python 3
# Python 3 Program to Find First
# element in AP which is
# multiple of given prime
# Iterative Function to calculate
# (x^y)%p in O(log y)
def power(x, y, p) :
# Initialize result
res = 1
# Update x if it is more than or
# equal to p
x = x % p
while y > 0 :
# If y is odd, multiply x with result
if y & 1 :
res = (res * x) % p
# y must be even now
# y = y/2
y = y >> 1
x = (x * x) % p
return res
# function to find nearest element in common
def NearestElement(A, D, P) :
# base conditions
if A == 0 :
return 0
elif D == 0 :
return -1
else :
X = power(D, P - 2, P)
return (X * (P - A)) % P
# Driver Code
if __name__ == "__main__" :
A, D, P = 4, 9, 11
# module both A and D
A %= P
D %= P
# function call
print(NearestElement(A, D, P))
# This code is contributed by ANKITRAI1
C#
// C# Program to Find First
// element in AP which is
// multiple of given prime
using System;
class GFG
{
// Iterative Function to
// calculate (x^y)%p in
// O(log y) */
static int power(int x,
int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is
// more than or equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// function to find nearest
// element in common
static int NearestElement(int A,
int D, int P)
{
// base conditions
if (A == 0)
return 0;
else if (D == 0)
return -1;
else
{
int X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
// Driver code
public static void Main()
{
int A = 4, D = 9, P = 11;
// module both A and D
A %= P;
D %= P;
// function call
Console.WriteLine(NearestElement(A, D, P));
}
}
// This code is contributed
// by chandan_jnu.
PHP
<?php
// Iterative Function to calculate
// (x^y)%p in O(log y) */
function power($x, $y, $p)
{
// Initialize result
$res = 1;
// Update x if it is more
// than or equal to p
$x = $x % $p;
while ($y > 0)
{
// If y is odd, multiply
// x with result
if ($y & 1)
$res = ($res * $x) %$p;
// y must be even now
$y = $y >> 1; // y = y/2
$x = ($x * $x) %$p;
}
return $res;
}
// function to find nearest
// element in common
function NearestElement($A, $D, $P)
{
// base conditions
if ($A == 0)
return 0;
else if ($D == 0)
return -1;
else
{
$X = power($D, $P - 2, $P);
return ($X * ($P - $A)) %$P;
}
}
// Driver code
$A = 4; $D = 9; $P = 11;
// module both A and D
$A %= $P;
$D %= $P;
// function call
echo NearestElement($A, $D, $P);
// This code is contributed
// by chandan_jnu.
?>
JavaScript
<script>
// Javascript Program to Find First
// element in AP which is
// multiple of given prime
// Iterative Function to
// calculate (x^y)%p in
// O(log y) */
function power(x, y, p)
{
// Initialize result
let res = 1;
// Update x if it is
// more than or equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// function to find nearest
// element in common
function NearestElement(A, D, P)
{
// base conditions
if (A == 0)
return 0;
else if (D == 0)
return -1;
else
{
let X = power(D, P - 2, P);
return (X * (P - A)) % P;
}
}
// driver program
let A = 4, D = 9, P = 11;
// module both A and D
A %= P;
D %= P;
// function call
document.write(NearestElement(A, D, P));
</script>
Time Complexity: O(log y)
Auxiliary Space: O(1)
Similar Reads
Find if a number is part of AP whose first element and difference are given Given three non-negative integers a, d and x. Here, a is the first element, d is the difference of an AP (Arithmetic Progression). We need to find if x is part of the given AP or not. Examples : Input : a = 1, d = 3, x = 7Output : Yes7 is part of given AP, 1 + 3 + 3 = 7Input : a = 10, d = 0, x = 10O
5 min read
Sum of array elements which are prime factors of a given number Given an array arr[] of size N and a positive integer K, the task is to find the sum of all array elements which are prime factors of K. Examples: Input: arr[] = {1, 2, 3, 5, 6, 7, 15}, K = 35Output: 12Explanation: From the given array, 5 and 7 are prime factors of 35. Therefore, required sum = 5 +
8 min read
Find numbers in range [L, R] that are coprime with given Array elements Given an array arr[] consisting of N distinct positive integers and a range [L, R], the task is to find the element in the given range [L, R] that are coprime with all array elements. Examples: Input: L = 3, R = 11, arr[ ] = {4, 7, 9, 6, 13, 21}Output: {5, 11}Explanation:The elements in the range [3
7 min read
Number which is co-prime with all integers from a given range Given two positive integers L and R, the task is to find an integer X greater than 1 such that X is co-prime with all the integers from the range [L, R]. Examples: Input: L = 16, R = 17 Output: 19Explanation: Only number which is co-prime with all the integers from the range [16, 17] is 9. Input: L
6 min read
Find Prime number just less than and just greater each element of given Array Given an integer array A[] of size N, the task is to find the prime numbers just less and just greater than A[i] (for all 0<=i<N). Examples: Input: A={17, 28}, N=2Output:13 1923 29Explanation:13 is the prime number just less than 17.19 is the prime number just greater than 17.23 is the prime n
15+ min read