The Pirate's code

0

0 votes
Medium
Problem

Edward Kenway, a well known pirate got a map to find the treasure. He went to a very large island, and reached to the point where to start given on the map, let it be (Px, Py). He is pointing to the north now, on the map its also given the number, which is known as Initial Parameter(I), from this number, the counting will start up to a Final Parameter(F) also given on the map, the condition is that starting from I,

1) If I is composite number, then Edward will turn to his right and move one step towards right.

2) If I is prime then will turn to his left and will move one step towards left.

Then after movement the value of I is incremented by 1, then again, it is tested and Edward moves accordingly, this process is done, until I and F are equal.

As this movement is very time expensive task for a most wanted Pirate Edward as the British Navy is behind him and can reach him any minute, so he needs your help to get the direct Co-ordinates of the treasures, so he takes treasure and leave.

Input Format-

The first line contains t, the number of the test cases for the program. The next t cases are as follow. First line of each test case contains 2 space separated integers, known as Px and Py the initial co-ordinates of Edward's position, next line contains the two space separated integers I and F, the Initial Parameter and the Final Parameter.

Output Format-

Output the 2 space separated integers the co-ordinates of the final position.

Constraints-

0 <= t <= 100

0 <= Px, Py <= 1000000

0 <= I <= F <= 1000000

F - I <= 100000

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Firstly, Edward is at (1, 1), and he is looking towards north, now I is now 7 and 7 != 10, and 7 is prime, thus Edward will turn left, so new position is (0, 1), and now he is looking -x direction, then I is incremented by 1 so I is now 8 and 8 != 10, as 8 is composite so he will turn to his right and looking to +y direction, so his new position is (0, 2), then I is now 9, which is not 10 and is composite, so he will turn to his right, so his new position is (1, 2), and then I will 10, here I == F == 10, so the process ends here, so the final position is (1, 2).

Editor Image

?