CANTON - Count on Cantor

no tags 

One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in the diagram below.

1/1 1/2 1/3 1/4 1/5 ...
2/1 2/2 2/3 2/4
3/1 3/2 3/3
4/1 4/2
5/1

In the above diagram, the first term is 1/1, the second term is 1/2, the third term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and so on.

Input

The input starts with a line containing a single integer t <= 20, the number of test cases. t test cases follow.

Then, it contains a single number per line.

Output

You are to write a program that will read a list of numbers in the range from 1 to 10^7 and will print for each number the corresponding term in Cantor's enumeration as given below.

Example

Input:
3
3
14
7

Output:
TERM 3 IS 2/1
TERM 14 IS 2/4
TERM 7 IS 1/4

hide comments
Jumpy: 2013-12-16 14:28:47

getting annoyed what the hell my answers are correct to all input including 10^7
but still getting WA.

got the case where i was mistaken it's in zigzag manner

Last edit: 2013-12-17 19:17:39
Grzegorz Utnik: 2013-09-18 10:02:43

I was so angry at myself for struggling with this one. After AC I can tell it was worth it.

ganpya: 2013-06-18 19:49:27

diagonal grows in triangular series...

Erti-Chris Eelmaa: 2013-05-27 21:12:40

cool number magic.. takes O(1) time to find out the N'th term. just have fun with arithmetic series & you'll eventually discover something fun ;)

vikash singh: 2013-05-16 04:48:58

(chandu)
it can continues upto infinity (whole matrix) no problem u have to find the term
(http://en.wikipedia.org/wiki/Cantor_pairing_function#Cantor_pairing_function)

Last edit: 2013-05-16 04:55:03
CHANDRA DEO MAHTO: 2013-05-10 13:07:29

can any one pls specify that upto which fraction each row will continue;the first row with ......;ps clarify

:-): 2013-04-09 17:46:30

don't forget to print 'TERM' and 'IS' in capital letters.

Sakthivel Subramaniyan: 2013-03-08 09:56:01

i did everything correct and it also works in ideone and codeblocks compilers but when i submit it shows error

me codesta a.k.a w@s!m: 2013-02-11 17:56:03

just play with numbers :D

Honza: 2013-02-04 01:53:03

Nice one! I chose the analytical way and solved it on paper actually. :)
This math doesn't even require any algorithms...


Added by:Thanh-Vy Hua
Date:2005-02-27
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource: ACM South Eastern European Region 2004