Given three integers A, B and C. In an infinite sequence, A is the first number, C is the common difference (Si – Si – 1 = C). The task is to check if the number B will appear in the sequence or not.
Input: A = 1, B = 7, C = 3
The sequence will be 1, 4, 7, 10, …
Input: A = 1, B = -4, C = 5
Approach: There are two cases:
- When C = 0, print Yes if A = B else No as the sequence will consist only the number A
- When C > 0, for any non-negative integer k the equation B = A + k * C must be satisfied i.e. (B – A) / C must be a non-negative integer.
Below is the implementation of the above approach:
($b – $a) % $c == 0)
// Driver code
$a = 1; $b = 7; $c = 3;
if (doesContainB($a, $b, $c))
// This code is contributed
// by Akanksha Rai
- Minimum steps needed to cover a sequence of points on an infinite grid
- Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +.......
- Find minimum moves to reach target on an infinite line
- Count number of subsets whose median is also present in the same subset
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Program to find sum of the given sequence
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find Recurring Sequence in a Fraction
- k-th number in the Odd-Even sequence
- Find element position in given monotonic sequence
- Find nth term of the Dragon Curve Sequence
- Ulam Number Sequence
- Number of words in a camelcase sequence
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Minimum number of operations to convert a given sequence into a Geometric Progression
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.