Cody

# Problem 138. Number of 1s in the Binary Representation of a Number

Solution 419889

Submitted on 18 Mar 2014 by Swapnali Gujar
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% x = 0; y_correct = 0; assert(isequal(your_fcn_name(x),y_correct))

ans = 0

2   Pass
%% x = 215; y_correct = 6; assert(isequal(your_fcn_name(x),y_correct))

ans = 6

3   Pass
%% x = 1:100; y_correct = [1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 2 3 3 4 3]; assert(isequal(your_fcn_name(x),y_correct))

r = 1 c = 100 one = 1 one = 1 1 one = 1 1 2 one = 1 1 2 1 one = 1 1 2 1 2 one = 1 1 2 1 2 2 one = 1 1 2 1 2 2 3 one = 1 1 2 1 2 2 3 1 one = 1 1 2 1 2 2 3 1 2 one = 1 1 2 1 2 2 3 1 2 2 one = 1 1 2 1 2 2 3 1 2 2 3 one = 1 1 2 1 2 2 3 1 2 2 3 2 one = 1 1 2 1 2 2 3 1 2 2 3 2 3 one = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 one = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 one = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Column 17 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 18 2 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 19 2 2 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 20 2 2 3 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 21 2 2 3 2 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 22 2 2 3 2 3 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 23 2 2 3 2 3 3 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 24 2 2 3 2 3 3 4 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 25 2 2 3 2 3 3 4 2 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 26 2 2 3 2 3 3 4 2 3 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 27 2 2 3 2 3 3 4 2 3 3 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 28 2 2 3 2 3 3 4 2 3 3 4 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 29 2 2 3 2 3 3 4 2 3 3 4 3 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 30 2 2 3 2 3 3 4 2 3 3 4 3 4 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 31 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Column 33 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 34 2 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 35 2 2 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 36 2 2 3 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 37 2 2 3 2 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 38 2 2 3 2 3 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 39 2 2 3 2 3 3 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 40 2 2 3 2 3 3 4 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 41 2 2 3 2 3 3 4 2 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 42 2 2 3 2 3 3 4 2 3 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 43 2 2 3 2 3 3 4 2 3 3 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 44 2 2 3 2 3 3 4 2 3 3 4 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 45 2 2 3 2 3 3 4 2 3 3 4 3 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 46 2 2 3 2 3 3 4 2 3 3 4 3 4 4 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 47 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 48 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 48 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 Column 49 3 one = Columns 1 through 16 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 Columns 17 through 32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 Columns 33 through 48 2 2 ...

4   Pass
%% x = magic(10); y_correct = [ 4 4 1 1 4 3 3 4 4 2 3 2 3 3 1 3 5 4 1 3 1 3 3 2 3 4 3 6 3 5 4 5 3 3 2 4 5 3 4 3 4 5 3 1 2 5 2 4 3 2 2 2 3 4 4 3 3 3 2 2 4 2 3 4 5 2 4 1 4 2 5 2 3 6 3 4 5 3 4 2 2 2 5 2 4 3 3 3 4 4 3 2 3 4 3 2 4 3 4 5 ]; assert(isequal(your_fcn_name(x),y_correct))

r = 10 c = 10 one = 4 one = 4 3 one = 4 3 1 one = 4 3 1 4 one = 4 3 1 4 4 one = 4 3 1 4 4 2 one = 4 3 1 4 4 2 4 one = 4 3 1 4 4 2 4 5 one = 4 3 1 4 4 2 4 5 2 one = 4 3 1 4 4 2 4 5 2 3 one = 4 3 1 4 4 2 4 5 2 3 4 one = 4 3 1 4 4 2 4 5 2 3 4 2 one = 4 3 1 4 4 2 4 5 2 3 4 2 3 one = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 one = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 one = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Column 17 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 18 2 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 19 2 2 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 20 2 2 2 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 21 2 2 2 2 1 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 22 2 2 2 2 1 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 23 2 2 2 2 1 3 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 24 2 2 2 2 1 3 3 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 25 2 2 2 2 1 3 3 3 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 26 2 2 2 2 1 3 3 3 3 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 27 2 2 2 2 1 3 3 3 3 3 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 28 2 2 2 2 1 3 3 3 3 3 3 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 29 2 2 2 2 1 3 3 3 3 3 3 3 5 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 30 2 2 2 2 1 3 3 3 3 3 3 3 5 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 31 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Column 33 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 34 2 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 35 2 3 1 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 36 2 3 1 4 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 37 2 3 1 4 4 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 38 2 3 1 4 4 6 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 39 2 3 1 4 4 6 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 40 2 3 1 4 4 6 2 4 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 41 2 3 1 4 4 6 2 4 4 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 42 2 3 1 4 4 6 2 4 4 1 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 43 2 3 1 4 4 6 2 4 4 1 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 44 2 3 1 4 4 6 2 4 4 1 3 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 45 2 3 1 4 4 6 2 4 4 1 3 2 2 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 46 2 3 1 4 4 6 2 4 4 1 3 2 2 4 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 47 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 48 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 48 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 Column 49 4 one = Columns 1 through 16 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 Columns 17 through 32 2 2 2 2 1 3 3 3 3 3 3 3 5 3 1 3 Columns 33 through 48 2 3 ...