Matrix Based Questions | Spiral Prime Numbers | Codewindow.in

The prime numbers are written in a spiral form staring at (0,0) and moving as shown in the diagram below. The numbers shown on the right column and the bottom row are the column numbers and row numbers respectively (y and x coordinate frames). The objective is to find the position (x and y coordinates) of a given prime. 

Input Format:
The input consists of multiple lines.
The first line gives the number of primes (N) in this test case.
The next N lines contain one prime in each line.

Output Format:
The output consists of N lines. Each consists of a space separated pair of integers giving the x and y coordinates of the corresponding prime in the input.

Constraints:
N≤10
Each prime < 1000000

Example 1
Input:
2
3
7

Output:
1 0
0 1

Explanation:
There are 2 primes in this test case (N=2). The primes are 3 and 7. The coordinates of these in the spiral is (1,0) and (0,1). The output hence has these in space separated form.

Example 2
Input:
3
5
11
13

Output:
1 1
1
1
1
0

Explanation:
There are 3 primes in this test case (N=2). The primes are 5, 11 and 13. The coordinates of these in the spiral is (1,1), (1,1) and (1,0). The output hence has these in space separated form.

Solution: In C

Solution: In Java

Solution: In Python 3

Also Checkout

Recent Posts
Categories
Pages