Given an integer N, the task is to print N integers ≤ 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime.
Input: N = 3
Output: 6 15 10
Input: N = 4
Output: 6 15 35 14
- We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do this so that the (n – 1)th number, nth and 1st number can also follow the property mentioned in the problem statement.
- Another important part of the problem is the fact that the numbers should be ≤ 109. If you just multiply consecutive prime numbers, after around 3700 numbers the value will cross 109. So we need to only use those prime numbers whose product wont cross 109.
- To do this in an efficient way, consider a small number of primes say the first 550 primes and select them in a way such that on making a product no number gets repeated. We first choose every prime consecutively and then choose the primes with an interval of 2 and then 3 and so on, doing that we already make sure that no number gets repeated.
So we will select
5, 11, 17, …
Next time, we can start with 7 and select,
7, 13, 19, …
Below is the implementation of the above approach:
6 15 35 14
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Express a number as sum of consecutive numbers
- Expressing factorial n as sum of consecutive numbers
- Fibbinary Numbers (No consecutive 1s in binary)
- Prove that atleast one of three consecutive even numbers is divisible by 6
- Check if a number can be expressed as a sum of consecutive numbers
- Find the number of consecutive zero at the end after multiplying n numbers
- Find the prime numbers which can written as sum of most consecutive primes
- Count ways to express a number as sum of consecutive numbers
- Generate a list of n consecutive composite numbers (An interesting method)
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Composite XOR and Coprime AND
- Largest number less than or equal to N/2 which is coprime to N
- Finding a Non Transitive Coprime Triplet in a Range
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
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.