Count on Cantor
Count on Cantor |
One of the famous proofs of modern mathematics is Georg Cantor's demonstrationthat the set of rational numbers is enumerable. The proof works by usingan explicit enumeration of rational numbers as shown in the diagram below.
In the above diagram, the first term is 1/1, the second term is 1/2, thethird term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and soon.
Input and Output
You are to write a program that will read a list of numbers in the rangefrom 1 to and will print for each number the corresponding term inCantor's enumeration as given below. No blank line should appear after thelast number.
The input list contains a single number per line and will beterminated by end-of-file.
Sample input
3 14 7
Sample output
TERM 3 IS 2/1 TERM 14 IS 2/4 TERM 7 IS 1/4
#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int a,st,sum=0,x,y;
for(a=1;;a++)
{sum+=a;
if(sum>=n) break;}
st=sum-n;
if(a%2)
{x=st+1;y=a-st;}
else
{x=a-st;y=st+1;}
cout<<"TERM "<<n<<" IS "<<x<<"/"<<y<<endl;
}
return 0;
}