Given an integer N, find and show the number of pairs which satisfies the following conditions:
- Square of distance between those two numbers is equal to the LCM of those two numbers.
- The GCD of those two numbers is equal to the product of two consecutive integers.
- Both numbers in the pair should be less than or equal to N.
NOTE: Only those pairs should be displayed which follows both the above conditions simultaneously and those numbers must be less than or equal to N.
Input: 10 Output: No. of pairs = 1 Pair no. 1 --> (2, 4) Input: 500 Output: No. of pairs = 7 Pair no. 1 --> (2, 4) Pair no. 2 --> (12, 18) Pair no. 3 --> (36, 48) Pair no. 4 --> (80, 100) Pair no. 5 --> (150, 180) Pair no. 6 --> (252, 294) Pair no. 7 --> (392, 448)
The tables shown below will give a clear view of what is to be found :
Above tables show GCD formed by the product of two consecutive numbers and its corresponding multiples in which UNIQUE PAIR exists corresponding to each value. Green entries in each row form a unique pair for corresponding GCD.
Note: In the above tables,
- For 1st entry, GCD=2, 1st and the 2nd multiple of 2 form the Unique Pair, (2, 4)
- Similarly, for the 2nd entry, GCD=6, 2nd and the 3rd multiple of 6 form the Unique Pair, (12, 18)
- Similarly, moving on, for Zth entry, i.e for GCD = Z*(Z+1), it is clear that the unique pair will comprise of Zth and (Z+1)th multiple of GCD = Z*(Z+1). Now, Zth multiple of GCD is Z * (Z*(Z+1)) and (Z+1)th multiple of GCD will be (Z + 1) * (Z*(Z+1)).
- And as the limit is N, so the second number in the unique pair must be less than or equal to the N. So, (Z + 1) * (Z*(Z+1)) <= N. Simplifying it further, the desired relation is derived Z3 + (2*Z2) + Z <=N
This forms a pattern and from the mathematical calculation, it is derived that for a given N, the total number of such unique pairs (say, Z) will follow a mathematical relation shown below:
Z3 + (2*Z2) + Z <= N
Below is the required implementation:
No. of pairs = 7 Pair no. 1 --> (2, 4) Pair no. 2 --> (12, 18) Pair no. 3 --> (36, 48) Pair no. 4 --> (80, 100) Pair no. 5 --> (150, 180) Pair no. 6 --> (252, 294) Pair no. 7 --> (392, 448)
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.
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Find three integers less than or equal to N such that their LCM is maximum
- Sum of largest prime factor of each number less than equal to n
- Highest and Smallest power of K less than and greater than equal to N respectively
- Print all numbers less than N with at-most 2 unique digits
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Find all factorial numbers less than or equal to n
- Find Largest Special Prime which is less than or equal to a given number
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Find maximum product of digits among numbers less than or equal to N
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find all powers of 2 less than or equal to a given number
- Find all possible subarrays having product less than or equal to K
- Find largest factor of N such that N/F is less than K
- Check if frequency of each digit is less than the digit
- Largest number less than N whose each digit is prime number
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Count pairs with sum as a prime number and less than n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.