Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 241434

Submitted on 7 May 2013 by James

Correct

80Size
Leading solution size is 18.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
p=1; q=3;
y_correct = 6;
assert(isequal(cantor_rational2index(p,q),y_correct))
M =
     4
r =
     1     3     6
w =
     3
2
Pass
 
%%
p=2; q=3;
y_correct = 9;
assert(isequal(cantor_rational2index(p,q),y_correct))
M =
     5
r =
     2     5     9
w =
     3
3
Pass
 
%%
p=22; q=7;
y_correct = 385;
assert(isequal(cantor_rational2index(p,q),y_correct))
M =
    29
r =
   232   255   279   304   330   357   385
w =
     7
4
Pass
 
%%
p=355; q=113;
y_correct = 108924;
assert(isequal(cantor_rational2index(p,q),y_correct))
M =
   468
r =
  Columns 1 through 8
       62836       63192       63549       63907       64266       64626       64987       65349
  Columns 9 through 16
       65712       66076       66441       66807       67174       67542       67911       68281
  Columns 17 through 24
       68652       69024       69397       69771       70146       70522       70899       71277
  Columns 25 through 32
       71656       72036       72417       72799       73182       73566       73951       74337
  Columns 33 through 40
       74724       75112       75501       75891       76282       76674       77067       77461
  Columns 41 through 48
       77856       78252       78649       79047       79446       79846       80247       80649
  Columns 49 through 56
       81052       81456       81861       82267       82674       83082       83491       83901
  Columns 57 through 64
       84312       84724       85137       85551       85966       86382       86799       87217
  Columns 65 through 72
       87636       88056       88477       88899       89322       89746       90171       90597
  Columns 73 through 80
       91024       91452       91881       92311       92742       93174       93607       94041
  Columns 81 through 88
       94476       94912       95349       95787       96226       96666       97107       97549
  Columns 89 through 96
       97992       98436       98881       99327       99774      100222      100671      101121
  Columns 97 through 104
      101572      102024      102477      102931      103386      103842      104299      104757
  Columns 105 through 112
      105216      105676      106137      106599      107062      107526      107991      108457
  Column 113
      108924
w =
   113