Is it within the Cantor set? The Challenge

For this challenge, you are supposed to determine if a given number is in the Cantor set. So first, let's define the Cantor set.

First, start with the numbers between 0 and 1. Any numbers outside this range are not in the Cantor set. Now, let's divide the numbers into three equal parts: [0,1/3], [1/3,2/3], [2/3, 1]. Any numbers not inside the ranges of the first and last parts are not in the Cantor set. Now, you repeat this process for the segments [0,1/3] and [2/3, 1]. Then you repeat on what is leftover. You keep on doing this forever. In the end, all numbers that are remaining are in the Cantor set. Here is a diagram of the first six iterations:





Input

Two integer x and y.
0 &lt; y &lt; 2^15
0 &lt;= x &lt;= y
The greatest common denominator of x and y is 1, unless x == 0.



Output

Truthy if x/y is in the Cantor set.
Falsy if x/y is not in the Cantor set.



Examples

Now, let's see some examples of numbers that are in the Cantor set.


1/3 -&gt; true  


It is on a boundary, and boundaries are never removed.

1/4 -&gt; true  


1/4 is never in the middle third of a segment, though it is never on the boundary either. If you follow it's path, you will actually find that it alternates between being in the first and last thirds of a section.

1/13 -&gt; true  


1/13 alternates between the first, first, and last sections.

1/5 -&gt; false


1/5 falls into the first empty block of the third row in the above diagram, between 1/9 and 2/9.

Other test cases:

0/4 -&gt; true
3/10 -&gt; true
3/4 -&gt; true
10/13 -&gt; true
1/1 -&gt; true
12/19 -&gt; false
5/17 -&gt; false
3/5 -&gt; false
1/7 -&gt; false
1/2 -&gt; false


You can try other numbers with this snippet:



function isCantor(t,r){var n=new Set;if(0&gt;r)return isCantor(-t,-r);if(0==r)return!1;if(0&gt;t||t&gt;r)return!1;for(;;){if(t%r===0)return!0;if(1===Math.floor(t/r))return!1;if(n.has(t))return!0;n.add(t),t=t%r*3}}$("#test").click(function(){var t=parseInt($("#x").val()),r=parseInt($("#y").val());if(isNaN(t)||isNaN(r)||0==r)return void $("#result").text("Invalid input");var n=isCantor(t,r),i=(n?"I":"Not i")+"n the Cantor set.";$("#result").text(i)});
input{width:30px}button{margin-left:7px}
&lt;script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&gt;&lt;/script&gt;&lt;input id=x&gt;/&lt;input id=y&gt;&lt;button id=test&gt;Test&lt;/button&gt;&lt;p id=result&gt;






Objective

The person with the least bytes wins.
Mathematica, 54 bytes

If[Last@#===1,Most@#,#]&amp;@RealDigits[#,3][[1]]~FreeQ~1&amp;


Unnamed function taking a fraction x/y as input, where y &gt; 0 and 0 ≤ x ≤ y, and returning True or False.

A real number between 0 and 1 is in the Cantor set precisely when none of the digits in its base-3 expansion equals 1; the exception is that a fraction whose denominator is a power of 3 (whose base-3 expansion therefore terminates) is allowed to end in a 1.

RealDigits[#,3][[1]] gives all of the digits in the base-3 expansion of the fractional input #, in a form like {1, 0, 2, {0, 1, 0, 2}}: the last list is the periodic part of the expansion, while the integers beforehand are the digits before the periodicity starts. If the base-3 expansion is periodic right away, the output is like {{0, 1, 0, 2}}; if the base-3 expansion terminates, the form is like {1, 0, 2}.

So we want to check, using ~FreeQ~1, whether the list is free of 1s or not. However, because of the terminating expansion thing, we want to delete the last element of the list if it equals 1; that's what If[Last@#===1,Most@#,#] accomplishes. (The === is needed to compare a potential list with 1: == alone remains unevaluated in that situation.)