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 211072

Submitted on 1 Mar 2013 by Khaled Hamed

Correct

18Size
Leading solution size is 10.
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
 
feval(@assignin,'caller','score',5000); % msec
forbidden = '(feval|eval|regexp|inline|tic|assert)';
assert(isempty(regexp(evalc('type hamming1'),forbidden)));

                    
2
Pass
 
%%
tic

                    
3
Pass
 
%%
x = 5; y_correct = 5; assert(isequal(hamming1(x),y_correct))
z =
    2.3219         0
m =
    2.3219    0.7370
m =
     0
4
Pass
 
%%
x = 100; y_correct = 34; assert(isequal(hamming1(x),y_correct))
z =
    6.6439    4.3219    2.0000
m =
    6.6439    5.0589    3.4739    1.8890    0.3040
m =
    4.3219    2.7370    1.1520
m =
    2.0000    0.4150
5
Pass
 
%%
x = 123456; y_correct = 327; assert(isequal(hamming1(x),y_correct))
z =
   16.9136   14.5917   12.2698    9.9479    7.6259    5.3040    2.9821    0.6601
m =
  Columns 1 through 9
   16.9136   15.3287   13.7437   12.1587   10.5738    8.9888    7.4039    5.8189    4.2339
  Columns 10 through 11
    2.6490    1.0640
m =
  Columns 1 through 9
   14.5917   13.0067   11.4218    9.8368    8.2519    6.6669    5.0819    3.4970    1.9120
  Column 10
    0.3270
m =
   12.2698   10.6848    9.0999    7.5149    5.9299    4.3450    2.7600    1.1750
m =
    9.9479    8.3629    6.7779    5.1930    3.6080    2.0230    0.4381
m =
    7.6259    6.0410    4.4560    2.8710    1.2861
m =
    5.3040    3.7190    2.1341    0.5491
m =
    2.9821    1.3971
m =
    0.6601
6
Pass
 
%%
x = 10^13; y_correct = 4301; assert(isequal(hamming1(x),y_correct))
z =
  Columns 1 through 9
   43.1851   40.8631   38.5412   36.2193   33.8974   31.5754   29.2535   26.9316   24.6096
  Columns 10 through 18
   22.2877   19.9658   17.6439   15.3219   13.0000   10.6781    8.3561    6.0342    3.7123
  Column 19
    1.3904
m =
  Columns 1 through 9
   43.1851   41.6001   40.0151   38.4302   36.8452   35.2603   33.6753   32.0903   30.5054
  Columns 10 through 18
   28.9204   27.3354   25.7505   24.1655   22.5806   20.9956   19.4106   17.8257   16.2407
  Columns 19 through 27
   14.6557   13.0708   11.4858    9.9009    8.3159    6.7309    5.1460    3.5610    1.9760
  Column 28
    0.3911
m =
  Columns 1 through 9
   40.8631   39.2782   37.6932   36.1082   34.5233   32.9383   31.3534   29.7684   28.1834
  Columns 10 through 18
   26.5985   25.0135   23.4285   21.8436   20.2586   18.6737   17.0887   15.5037   13.9188
  Columns 19 through 26
   12.3338   10.7488    9.1639    7.5789    5.9940    4.4090    2.8240    1.2391
m =
  Columns 1 through 9
   38.5412   36.9562   35.3713   33.7863   32.2014   30.6164   29.0314   27.4465   25.8615
  Columns 10 through 18
   24.2765   22.6916   21.1066   19.5217   17.9367   16.3517   14.7668   13.1818   11.5968
  Columns 19 through 25
   10.0119    8.4269    6.8420    5.2570    3.6720    2.0871    0.5021
m =
  Columns 1 through 9
   36.2193   34.6343   33.0494   31.4644   29.8794   28.2945   26.7095   25.1245   23.5396
  Columns 10 through 18
   21.9546   20.3697   18.7847   17.1997   15.6148   14.0298   12.4448   10.8599    9.2749
  Columns 19 through 23
    7.6900    6.1050    4.5200    2.9351    1.3501
m =
  Columns 1 through 9
   33.8974   32.3124   30.7274   29.1425   27.5575   25.9725   24.3876   22.8026   21.2177
  Columns 10 through 18
   19.6327   18.0477   16.4628   14.8778   13.2928   11.7079   10.1229    8.5380    6.9530
  Columns 19 through 22
    5.3680    3.7831    2.1981    0.6131
m =
  Columns 1 through 9
   31.5754   29.9905   28.4055   26.8205   25.2356   23.6506   22.0656   20.4807   18.8957
  Columns 10 through 18
   17.3108   15.7258   14.1408   12.5559   10.9709    9.3859    7.8010    6.2160    4.6311
  Columns 19 through 20
    3.0461    1.4611
m =
  Columns 1 through 9
   29.2535   27.6685   26.0836   24.4986   22.9136   21.3287   19.7437   18.1588   16.5738
  Columns 10 through 18
   14.9888   13.4039   11.8189   10.2339    8.6490    7.0640    5.4791    3.8941    2.3091
  Column 19
    0.7242
m =
  Columns 1 through 9
   26.9316   25.3466   23.7616   22.1767   20.5917   19.0068   17.4218   15.8368   14.2519
  Columns 10 through 17
   12.6669   11.0819    9.4970    7.9120    6.3271    4.7421    3.1571    1.5722
m =
  Columns 1 through 9
   24.6096   23.0247   21.4397   19.8548   18.2698   16.6848   15.0999   13.5149   11.9299
  Columns 10 through 16
   10.3450    8.7600    7.1751    5.5901    4.0051    2.4202    0.8352
m =
  Columns 1 through 9
   22.2877   20.7027   19.1178   17.5328   15.9479   14.3629   12.7779   11.1930    9.6080
  Columns 10 through 15
    8.0230    6.4381    4.8531    3.2682    1.6832    0.0982
m =
  Columns 1 through 9
   19.9658   18.3808   16.7959   15.2109   13.6259   12.0410   10.4560    8.8710    7.2861
  Columns 10 through 13
    5.7011    4.1162    2.5312    0.9462
m =
  Columns 1 through 9
   17.6439   16.0589   14.4739   12.8890   11.3040    9.7190    8.1341    6.5491    4.9642
  Columns 10 through 12
    3.3792    1.7942    0.2093
m =
  Columns 1 through 9
   15.3219   13.7370   12.1520   10.5670    8.9821    7.3971    5.8122    4.2272    2.6422
  Column 10
    1.0573
m =
   13.0000   11.4150    9.8301    8.2451    6.6601    5.0752    3.4902    1.9053    0.3203
m =
   10.6781    9.0931    7.5081    5.9232    4.3382    2.7533    1.1683
m =
    8.3561    6.7712    5.1862    3.6013    2.0163    0.4313
m =
    6.0342    4.4493    2.8643    1.2793
m =
    3.7123    2.1273    0.5424
m =
    1.3904
7
Pass
 
%%
x=1:20; y=arrayfun(@(z) hamming1(z),2.^x);
y_correct=[2 4 7 12 19 27 38 52 68 87 110 137 167 201 240 284 332 386 446 511];
assert(isequal(y,y_correct));
tval=1000*toc
feval(  @assignin,'caller','score',floor(min(5000,tval ))  );
z =
     1
m =
     1
z =
     2
m =
    2.0000    0.4150
z =
    3.0000    0.6781
m =
    3.0000    1.4150
m =
    0.6781
z =
    4.0000    1.6781
m =
    4.0000    2.4150    0.8301
m =
    1.6781    0.0931
z =
    5.0000    2.6781    0.3561
m =
    5.0000    3.4150    1.8301    0.2451
m =
    2.6781    1.0931
m =
    0.3561
z =
    6.0000    3.6781    1.3561
m =
    6.0000    4.4150    2.8301    1.2451
m =
    3.6781    2.0931    0.5081
m =
    1.3561
z =
    7.0000    4.6781    2.3561    0.0342
m =
    7.0000    5.4150    3.8301    2.2451    0.6601
m =
    4.6781    3.0931    1.5081
m =
    2.3561    0.7712
m =
    0.0342
z =
    8.0000    5.6781    3.3561    1.0342
m =
    8.0000    6.4150    4.8301    3.2451    1.6601    0.0752
m =
    5.6781    4.0931    2.5081    0.9232
m =
    3.3561    1.7712    0.1862
m =
    1.0342
z =
    9.0000    6.6781    4.3561    2.0342
m =
    9.0000    7.4150    5.8301    4.2451    2.6601    1.0752
m =
    6.6781    5.0931    3.5081    1.9232    0.3382
m =
    4.3561    2.7712    1.1862
m =
    2.0342    0.4493
z =
   10.0000    7.6781    5.3561    3.0342    0.7123
m =
   10.0000    8.4150    6.8301    5.2451    3.6601    2.0752    0.4902
m =
    7.6781    6.0931    4.5081    2.9232    1.3382
m =
    5.3561    3.7712    2.1862    0.6013
m =
    3.0342    1.4493
m =
    0.7123
z =
   11.0000    8.6781    6.3561    4.0342    1.7123
m =
   11.0000    9.4150    7.8301    6.2451    4.6601    3.0752    1.4902
m =
    8.6781    7.0931    5.5081    3.9232    2.3382    0.7533
m =
    6.3561    4.7712    3.1862    1.6013    0.0163
m =
    4.0342    2.4493    0.8643
m =
    1.7123    0.1273
z =
   12.0000    9.6781    7.3561    5.0342    2.7123    0.3904
m =
   12.0000   10.4150    8.8301    7.2451    5.6601    4.0752    2.4902    0.9053
m =
    9.6781    8.0931    6.5081    4.9232    3.3382    1.7533    0.1683
m =
    7.3561    5.7712    4.1862    2.6013    1.0163
m =
    5.0342    3.4493    1.8643    0.2793
m =
    2.7123    1.1273
m =
    0.3904
z =
   13.0000   10.6781    8.3561    6.0342    3.7123    1.3904
m =
   13.0000   11.4150    9.8301    8.2451    6.6601    5.0752    3.4902    1.9053    0.3203
m =
   10.6781    9.0931    7.5081    5.9232    4.3382    2.7533    1.1683
m =
    8.3561    6.7712    5.1862    3.6013    2.0163    0.4313
m =
    6.0342    4.4493    2.8643    1.2793
m =
    3.7123    2.1273    0.5424
m =
    1.3904
z =
   14.0000   11.6781    9.3561    7.0342    4.7123    2.3904    0.0684
m =
   14.0000   12.4150   10.8301    9.2451    7.6601    6.0752    4.4902    2.9053    1.3203
m =
   11.6781   10.0931    8.5081    6.9232    5.3382    3.7533    2.1683    0.5833
m =
    9.3561    7.7712    6.1862    4.6013    3.0163    1.4313
m =
    7.0342    5.4493    3.8643    2.2793    0.6944
m =
    4.7123    3.1273    1.5424
m =
    2.3904    0.8054
m =
    0.0684
z =
   15.0000   12.6781   10.3561    8.0342    5.7123    3.3904    1.0684
m =
  Columns 1 through 9
   15.0000   13.4150   11.8301   10.2451    8.6601    7.0752    5.4902    3.9053    2.3203
  Column 10
    0.7353
m =
   12.6781   11.0931    9.5081    7.9232    6.3382    4.7533    3.1683    1.5833
m =
   10.3561    8.7712    7.1862    5.6013    4.0163    2.4313    0.8464
m =
    8.0342    6.4493    4.8643    3.2793    1.6944    0.1094
m =
    5.7123    4.1273    2.5424    0.9574
m =
    3.3904    1.8054    0.2204
m =
    1.0684
z =
   16.0000   13.6781   11.3561    9.0342    6.7123    4.3904    2.0684
m =
  Columns 1 through 9
   16.0000   14.4150   12.8301   11.2451    9.6601    8.0752    6.4902    4.9053    3.3203
  Columns 10 through 11
    1.7353    0.1504
m =
   13.6781   12.0931   10.5081    8.9232    7.3382    5.7533    4.1683    2.5833    0.9984
m =
   11.3561    9.7712    8.1862    6.6013    5.0163    3.4313    1.8464    0.2614
m =
    9.0342    7.4493    5.8643    4.2793    2.6944    1.1094
m =
    6.7123    5.1273    3.5424    1.9574    0.3724
m =
    4.3904    2.8054    1.2204
m =
    2.0684    0.4835
z =
   17.0000   14.6781   12.3561   10.0342    7.7123    5.3904    3.0684    0.7465
m =
  Columns 1 through 9
   17.0000   15.4150   13.8301   12.2451   10.6601    9.0752    7.4902    5.9053    4.3203
  Columns 10 through 11
    2.7353    1.1504
m =
  Columns 1 through 9
   14.6781   13.0931   11.5081    9.9232    8.3382    6.7533    5.1683    3.5833    1.9984
  Column 10
    0.4134
m =
   12.3561   10.7712    9.1862    7.6013    6.0163    4.4313    2.8464    1.2614
m =
   10.0342    8.4493    6.8643    5.2793    3.6944    2.1094    0.5244
m =
    7.7123    6.1273    4.5424    2.9574    1.3724
m =
    5.3904    3.8054    2.2204    0.6355
m =
    3.0684    1.4835
m =
    0.7465
z =
   18.0000   15.6781   13.3561   11.0342    8.7123    6.3904    4.0684    1.7465
m =
  Columns 1 through 9
   18.0000   16.4150   14.8301   13.2451   11.6601   10.0752    8.4902    6.9053    5.3203
  Columns 10 through 12
    3.7353    2.1504    0.5654
m =
  Columns 1 through 9
   15.6781   14.0931   12.5081   10.9232    9.3382    7.7533    6.1683    4.5833    2.9984
  Column 10
    1.4134
m =
   13.3561   11.7712   10.1862    8.6013    7.0163    5.4313    3.8464    2.2614    0.6764
m =
   11.0342    9.4493    7.8643    6.2793    4.6944    3.1094    1.5244
m =
    8.7123    7.1273    5.5424    3.9574    2.3724    0.7875
m =
    6.3904    4.8054    3.2204    1.6355    0.0505
m =
    4.0684    2.4835    0.8985
m =
    1.7465    0.1615
z =
   19.0000   16.6781   14.3561   12.0342    9.7123    7.3904    5.0684    2.7465    0.4246
m =
  Columns 1 through 9
   19.0000   17.4150   15.8301   14.2451   12.6601   11.0752    9.4902    7.9053    6.3203
  Columns 10 through 12
    4.7353    3.1504    1.5654
m =
  Columns 1 through 9
   16.6781   15.0931   13.5081   11.9232   10.3382    8.7533    7.1683    5.5833    3.9984
  Columns 10 through 11
    2.4134    0.8284
m =
  Columns 1 through 9
   14.3561   12.7712   11.1862    9.6013    8.0163    6.4313    4.8464    3.2614    1.6764
  Column 10
    0.0915
m =
   12.0342   10.4493    8.8643    7.2793    5.6944    4.1094    2.5244    0.9395
m =
    9.7123    8.1273    6.5424    4.9574    3.3724    1.7875    0.2025
m =
    7.3904    5.8054    4.2204    2.6355    1.0505
m =
    5.0684    3.4835    1.8985    0.3135
m =
    2.7465    1.1615
m =
    0.4246
z =
   20.0000   17.6781   15.3561   13.0342   10.7123    8.3904    6.0684    3.7465    1.4246
m =
  Columns 1 through 9
   20.0000   18.4150   16.8301   15.2451   13.6601   12.0752   10.4902    8.9053    7.3203
  Columns 10 through 13
    5.7353    4.1504    2.5654    0.9804
m =
  Columns 1 through 9
   17.6781   16.0931   14.5081   12.9232   11.3382    9.7533    8.1683    6.5833    4.9984
  Columns 10 through 12
    3.4134    1.8284    0.2435
m =
  Columns 1 through 9
   15.3561   13.7712   12.1862   10.6013    9.0163    7.4313    5.8464    4.2614    2.6764
  Column 10
    1.0915
m =
   13.0342   11.4493    9.8643    8.2793    6.6944    5.1094    3.5244    1.9395    0.3545
m =
   10.7123    9.1273    7.5424    5.9574    4.3724    2.7875    1.2025
m =
    8.3904    6.8054    5.2204    3.6355    2.0505    0.4655
m =
    6.0684    4.4835    2.8985    1.3135
m =
    3.7465    2.1615    0.5766
m =
    1.4246
tval =
   18.6320