20
4
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 < y < 2^15
0 <= x <= 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 -> true
It is on a boundary, and boundaries are never removed.
1/4 -> 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 -> true
1/13
alternates between the first, first, and last sections.
1/5 -> 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 -> true
3/10 -> true
3/4 -> true
10/13 -> true
1/1 -> true
12/19 -> false
5/17 -> false
3/5 -> false
1/7 -> false
1/2 -> false
You can try other numbers with this snippet:
function isCantor(t,r){var n=new Set;if(0>r)return isCantor(-t,-r);if(0==r)return!1;if(0>t||t>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}
<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script><input id=x>/<input id=y><button id=test>Test</button><p id=result>
Objective
The person with the least bytes wins.
Are we guaranteed that the input is not (0,0)? Is the fraction given in simplest form? – xnor – 2017-02-01T06:43:35.377
1@xnor look at the range given for y. I'm going to say that the fraction is in simplest form unless
x == 0
– TheNumberOne – 2017-02-01T06:44:05.653Some test cases where x!=1 would be good. Also, your snippet says 1/3 is not in the cantor set. – xnor – 2017-02-01T06:48:21.473
@xnor Added and fixed ;) – TheNumberOne – 2017-02-01T06:58:52.590
Can we take the input directly as a fraction
x/y
? – Greg Martin – 2017-02-01T07:39:19.373If i enter 1/13 in your test snipet i get a recursion error – Roman Gräf – 2017-02-01T10:15:44.737
@RomanGräf Odd, I don't have that problem. – TheNumberOne – 2017-02-01T14:46:43.473
@RomanGräf Tell me if you have that problem again. The snippet no longer uses recursion. – TheNumberOne – 2017-02-01T16:14:16.413
6Cantor can it be found? – mbomb007 – 2017-02-01T16:16:23.993
@mbomb007 Lol, that's pretty good. – TheNumberOne – 2017-02-01T16:17:47.860
Related: OEIS A054591
– Engineer Toast – 2017-02-01T19:42:38.267Not sure if "within" in the title is the best word considering that the Cantor set is meagre and its interior is empty. Maybe use "in" instead (as you do in the text)?
– Luis Mendo – 2017-02-01T23:26:15.263