# Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K

Given N and K, the task is to print N lines where each line contains 4 numbers such that every among those 4 numbers has a GCD K and the maximum number used in N*4 should be minimized.

**Note:** In case of multiple outputs, print any one.

**Examples:**

Input:N = 1, K = 1

Output: 1 2 3 5

Every pair among 1, 2, 3 and 5 gives a GCD K and the largest number among these is 5 which the minimum possible.

Input: 2 2

Output:

2 4 6 22

14 18 10 16In the above input, the maximum number is 22, which is the minimum possible to make 2 lines of 4 numbers.

**Approach: **The first observation is that if we can solve the given problem for K=1, we can solve the problem with GCD K by simply multiplying the answers with K. We know that any three consecutive odd numbers have a GCD 1 always when paired, so three numbers of every line can be easily obtained. Hence the lines will look like:

1 3 5 _ 7 9 11 _ 13 15 17 _ . . .

An even number cannot be inserted always, because inserting 6 in third line will give GCD(6, 9) as 3. So the best number that can be inserted is a number between the first two off numbers of every line. Hence the pattern looks like:

1 2 3 5 7 8 9 11 13 14 15 17 . . .

To obtain given GCD K, one can easily multiply K to the obtained numbers. Hence for i-th line:

- the first number will be k * (6*i+1)
- the second number will be k * (6*i+1)
- the third number will be k * (6*i+3)
- the fourth number will be k * (6*i+5)

The maximum number among N*4 numbers will be **k * (6*i – 1) **

Below is the implementation of the above approach.

## C++

`// C++ implementation of the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print N lines ` `void` `printLines(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Iterate N times to print N lines ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `cout << k * (6 * i + 1) << ` `" "` ` ` `<< k * (6 * i + 2) << ` `" "` ` ` `<< k * (6 * i + 3) << ` `" "` ` ` `<< k * (6 * i + 5) << endl; ` ` ` `} ` `} ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 2, k = 2; ` ` ` `printLines(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the ` `// above approach ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `// Function to print N lines ` `static` `void` `printLines(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Iterate N times to print N lines ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `System.out.println ( k * (` `6` `* i + ` `1` `) + ` `" "` ` ` `+ k * (` `6` `* i + ` `2` `) + ` `" "` ` ` `+ k * (` `6` `* i + ` `3` `) + ` `" "` ` ` `+ k * (` `6` `* i + ` `5` `) ); ` ` ` `} ` `} ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `2` `, k = ` `2` `; ` ` ` `printLines(n, k); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python implementation of the ` `# above approach. ` ` ` `# Function to print N lines ` `def` `printLines(n, k) : ` ` ` ` ` `# Iterate N times to print N lines ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `print` `( k ` `*` `(` `6` `*` `i ` `+` `1` `), ` ` ` `k ` `*` `(` `6` `*` `i ` `+` `2` `), ` ` ` `k ` `*` `(` `6` `*` `i ` `+` `3` `), ` ` ` `k ` `*` `(` `6` `*` `i ` `+` `5` `)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n, k ` `=` `2` `, ` `2` ` ` `printLines(n, k) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Function to print N lines ` `function` `printLines(` `$n` `, ` `$k` `) ` `{ ` ` ` `// Iterate N times to print N lines ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `echo` `(` `$k` `* (6 * ` `$i` `+ 1)); ` ` ` `echo` `(` `" "` `); ` ` ` `echo` `(` `$k` `* (6 * ` `$i` `+ 2)); ` ` ` `echo` `(` `" "` `); ` ` ` `echo` `(` `$k` `* (6 * ` `$i` `+ 3)); ` ` ` `echo` `(` `" "` `); ` ` ` `echo` `(` `$k` `* (6 * ` `$i` `+ 5)); ` ` ` `echo` `(` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `$n` `= 2; ` `$k` `= 2; ` `printLines(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the ` `// above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to print N lines ` `static` `void` `printLines(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Iterate N times to print N lines ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `Console.WriteLine ( k * (6 * i + 1) + ` `" "` `+ ` ` ` `k * (6 * i + 2) + ` `" "` `+ ` ` ` `k * (6 * i + 3) + ` `" "` `+ ` ` ` `k * (6 * i + 5) ); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 2, k = 2; ` ` ` `printLines(n, k); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

**Output:**

2 4 6 10 14 16 18 22

**Time Complexity:** O(4*N)

## Recommended Posts:

- Check if the given two numbers are friendly pair or not
- Find original numbers from gcd() every pair
- Pair of prime numbers with a given sum and minimum absolute difference
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Print first N Mosaic numbers
- Print numbers with digits 0 and 1 only such that their sum is N
- Program to print first n Fibonacci Numbers | Set 1
- Print the kth common factor of two numbers
- Print all numbers less than N with at-most 2 unique digits
- Print prime numbers in a given range using C++ STL
- Print all Good numbers in given range
- Print all integers that are sum of powers of two given numbers
- Print squares of first n natural numbers without using *, / and -
- Print k numbers where all pairs are divisible by m
- Print n numbers such that their sum is a perfect square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.