Cody

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

Solution 1876582

Submitted on 17 Jul 2019 by Steven Beumer
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))

t = 1 1 z = 0 y = 0

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

t = 1 1 z = 6 y = 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))

t = 1 100 z = 1 z = 1 1 z = 1 1 2 z = 1 1 2 1 z = 1 1 2 1 2 z = 1 1 2 1 2 2 z = 1 1 2 1 2 2 3 z = 1 1 2 1 2 2 3 1 z = 1 1 2 1 2 2 3 1 2 z = 1 1 2 1 2 2 3 1 2 2 z = 1 1 2 1 2 2 3 1 2 2 3 z = 1 1 2 1 2 2 3 1 2 2 3 2 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 z = 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 2 z = 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 z = 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 z = 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 z = 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 z = 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 z = 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 z = Columns 1 through 30 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 Column 31 5 z = Columns 1 through 30 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 Columns 31 through 32 5 1 z = Columns 1 through 30 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 Columns 31 through 33 5 1 2 z = Columns 1 through 30 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 Columns 31 through 34 5 1 2 2 z = Columns 1 through 30 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 Columns 31 through 35 5 1 2 2 3 z = Columns 1 through 30 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 Columns 31 through 36 5 1 2 2 3 2 z = Columns 1 through 30 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 Columns 31 through 37 5 1 2 2 3 2 3 z = Columns 1 through 30 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 Columns 31 through 38 5 1 2 2 3 2 3 3 z = Columns 1 through 30 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 Columns 31 through 39 5 1 2 2 3 2 3 3 4 z = Columns 1 through 30 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 Columns 31 through 40 5 1 2 2 3 2 3 3 4 2 z = Columns 1 through 30 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 Columns 31 through 41 5 1 2 2 3 2 3 3 4 2 3 z = Columns 1 through 30 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 Columns 31 through 42 5 1 2 2 3 2 3 3 4 2 3 3 z = Columns 1 through 30 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 Columns 31 through 43 5 1 2 2 3 2 3 3 4 2 3 3 4 z = Columns 1 through 30 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 Columns 31 through 44 5 1 2 2 3 2 3 3 4 2 3 3 4 3 z = Columns 1 through 30 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 Columns 31 through 45 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 z = Columns 1 through 30 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 Columns 31 through 46 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 z = Columns 1 through 30 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 Columns 31 through 47 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 z = Columns 1 through 30 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 Columns 31 through 48 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 z = Columns 1 through 30 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 Columns 31 through 49 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 z = Columns 1 through 30 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 Columns 31 through 50 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 z = Columns 1 through 30 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 Columns 31 through 51 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 z = Columns 1 through 30 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 Columns 31 through 52 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 z = Columns 1 through 30 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   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 ];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2 2 2 1 3 3 3 3 3 3 3 5 3 z = Columns 1 through 30 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 3 Column 31 1 z = Columns 1 through 30 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 3 Columns 31 through 32 1 3 z = Columns 1 through 30 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 3 Columns 31 through 33 1 3 2 z = Columns 1 through 30 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 3 Columns 31 through 34 1 3 2 3 z = Columns 1 through 30 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 3 Columns 31 through 35 1 3 2 3 1 z = Columns 1 through 30 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 3 Columns 31 through 36 1 3 2 3 1 4 z = Columns 1 through 30 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 3 Columns 31 through 37 1 3 2 3 1 4 4 z = Columns 1 through 30 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 3 Columns 31 through 38 1 3 2 3 1 4 4 6 z = Columns 1 through 30 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 3 Columns 31 through 39 1 3 2 3 1 4 4 6 2 z = Columns 1 through 30 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 3 Columns 31 through 40 1 3 2 3 1 4 4 6 2 4 z = Columns 1 through 30 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 3 Columns 31 through 41 1 3 2 3 1 4 4 6 2 4 4 z = Columns 1 through 30 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 3 Columns 31 through 42 1 3 2 3 1 4 4 6 2 4 4 1 z = Columns 1 through 30 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 3 Columns 31 through 43 1 3 2 3 1 4 4 6 2 4 4 1 3 z = Columns 1 through 30 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 3 Columns 31 through 44 1 3 2 3 1 4 4 6 2 4 4 1 3 2 z = Columns 1 through 30 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 3 Columns 31 through 45 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 z = Columns 1 through 30 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 3 Columns 31 through 46 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 z = Columns 1 through 30 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 3 Columns 31 through 47 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 z = Columns 1 through 30 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 3 Columns 31 through 48 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 z = Columns 1 through 30 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 3 Columns 31 through 49 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 4 z = Columns 1 through 30 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 3 Columns 31 through 50 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 4 3 z = Columns 1 through 30 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 3 Columns 31 through 51 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 4 3 3 z = Columns 1 through 30 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 3 Columns 31 through 52 1 3 2 3 1 4 4 6 2 4 4 1 3 2 2 4 5 3 4 3 3 3 z = Columns 1 through 30 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 ...

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!