Cody

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

Solution 2825997

Submitted on 12 Aug 2020 by Ramesh Kumar V
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))

m = 1 n = 1 z = 0 y = [] y = 0 y1 = 0

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

m = 1 n = 1 z = 215 y = [] y = 6 y1 = 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))

m = 1 n = 100 z = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 y = [] y = 1 y = 1 1 y = 1 1 2 y = 1 1 2 1 y = 1 1 2 1 2 y = 1 1 2 1 2 2 y = 1 1 2 1 2 2 3 y = 1 1 2 1 2 2 3 1 y = 1 1 2 1 2 2 3 1 2 y = 1 1 2 1 2 2 3 1 2 2 y = 1 1 2 1 2 2 3 1 2 2 3 y = 1 1 2 1 2 2 3 1 2 2 3 2 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 y = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 2 y = 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 y = 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 y = 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 y = 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 y = 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 y = Columns 1 through 29 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 Column 30 4 y = Columns 1 through 29 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 Columns 30 through 31 4 5 y = Columns 1 through 29 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 Columns 30 through 32 4 5 1 y = Columns 1 through 29 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 Columns 30 through 33 4 5 1 2 y = Columns 1 through 29 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 Columns 30 through 34 4 5 1 2 2 y = Columns 1 through 29 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 Columns 30 through 35 4 5 1 2 2 3 y = Columns 1 through 29 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 Columns 30 through 36 4 5 1 2 2 3 2 y = Columns 1 through 29 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 Columns 30 through 37 4 5 1 2 2 3 2 3 y = Columns 1 through 29 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 Columns 30 through 38 4 5 1 2 2 3 2 3 3 y = Columns 1 through 29 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 Columns 30 through 39 4 5 1 2 2 3 2 3 3 4 y = Columns 1 through 29 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 Columns 30 through 40 4 5 1 2 2 3 2 3 3 4 2 y = Columns 1 through 29 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 Columns 30 through 41 4 5 1 2 2 3 2 3 3 4 2 3 y = Columns 1 through 29 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 Columns 30 through 42 4 5 1 2 2 3 2 3 3 4 2 3 3 y = Columns 1 through 29 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 Columns 30 through 43 4 5 1 2 2 3 2 3 3 4 2 3 3 4 y = Columns 1 through 29 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 Columns 30 through 44 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 y = Columns 1 through 29 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 Columns 30 through 45 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 y = Columns 1 through 29 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 Columns 30 through 46 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 y = Columns 1 through 29 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 Columns 30 through 47 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 y = Columns 1 through 29 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 Columns 30 through 48 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 y = Columns 1 through 29 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 Columns 30 through 49 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 y = Columns 1 through 29 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 Columns 30 through 50 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 y = Columns 1 through 29 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 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))

m = 10 n = 10 z = 92 98 4 85 86 17 23 79 10 11 99 80 81 87 93 24 5 6 12 18 1 7 88 19 25 76 82 13 94 100 8 14 20 21 2 83 89 95 96 77 15 16 22 3 9 90 91 97 78 84 67 73 54 60 61 42 48 29 35 36 74 55 56 62 68 49 30 31 37 43 51 57 63 69 75 26 32 38 44 50 58 64 70 71 52 33 39 45 46 27 40 41 47 28 34 65 66 72 53 59 y = [] y = 4 y = 4 3 y = 4 3 1 y = 4 3 1 4 y = 4 3 1 4 4 y = 4 3 1 4 4 2 y = 4 3 1 4 4 2 4 y = 4 3 1 4 4 2 4 5 y = 4 3 1 4 4 2 4 5 2 y = 4 3 1 4 4 2 4 5 2 3 y = 4 3 1 4 4 2 4 5 2 3 4 y = 4 3 1 4 4 2 4 5 2 3 4 2 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 y = 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Column 30 3 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 31 3 1 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 32 3 1 3 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 33 3 1 3 2 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 34 3 1 3 2 3 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 35 3 1 3 2 3 1 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 36 3 1 3 2 3 1 4 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 37 3 1 3 2 3 1 4 4 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 38 3 1 3 2 3 1 4 4 6 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 39 3 1 3 2 3 1 4 4 6 2 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 40 3 1 3 2 3 1 4 4 6 2 4 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 41 3 1 3 2 3 1 4 4 6 2 4 4 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 42 3 1 3 2 3 1 4 4 6 2 4 4 1 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 43 3 1 3 2 3 1 4 4 6 2 4 4 1 3 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 44 3 1 3 2 3 1 4 4 6 2 4 4 1 3 2 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 45 3 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 46 3 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 47 3 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 48 3 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 49 3 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 4 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 2 2 1 3 3 3 3 3 3 3 5 Columns 30 through 50 3 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 4 3 y = Columns 1 through 29 4 3 1 4 4 2 4 5 2 3 4 2 3 5 5 2 2 2 ...