Cody

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

Solution 1988376

Submitted on 23 Oct 2019 by Asif Newaz
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))

b = '0' n = 1 1 m = 1 1 y = 0

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

b = '11010111' n = 1 1 m = 1 8 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))

b = 100×7 char array '0000001' '0000010' '0000011' '0000100' '0000101' '0000110' '0000111' '0001000' '0001001' '0001010' '0001011' '0001100' '0001101' '0001110' '0001111' '0010000' '0010001' '0010010' '0010011' '0010100' '0010101' '0010110' '0010111' '0011000' '0011001' '0011010' '0011011' '0011100' '0011101' '0011110' '0011111' '0100000' '0100001' '0100010' '0100011' '0100100' '0100101' '0100110' '0100111' '0101000' '0101001' '0101010' '0101011' '0101100' '0101101' '0101110' '0101111' '0110000' '0110001' '0110010' '0110011' '0110100' '0110101' '0110110' '0110111' '0111000' '0111001' '0111010' '0111011' '0111100' '0111101' '0111110' '0111111' '1000000' '1000001' '1000010' '1000011' '1000100' '1000101' '1000110' '1000111' '1001000' '1001001' '1001010' '1001011' '1001100' '1001101' '1001110' '1001111' '1010000' '1010001' '1010010' '1010011' '1010100' '1010101' '1010110' '1010111' '1011000' '1011001' '1011010' '1011011' '1011100' '1011101' '1011110' '1011111' '1100000' '1100001' '1100010' '1100011' '1100100' n = 1 100 m = 100 7 y = 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 60 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 Columns 61 through 90 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 Columns 91 through 100 5 4 5 5 6 2 3 3 4 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 ]; assert(isequal(your_fcn_name(x),y_correct))

b = 100×7 char array '1011100' '1100010' '0000100' '1010101' '1010110' '0010001' '0010111' '1001111' '0001010' '0001011' '1100011' '1010000' '1010001' '1010111' '1011101' '0011000' '0000101' '0000110' '0001100' '0010010' '0000001' '0000111' '1011000' '0010011' '0011001' '1001100' '1010010' '0001101' '1011110' '1100100' '0001000' '0001110' '0010100' '0010101' '0000010' '1010011' '1011001' '1011111' '1100000' '1001101' '0001111' '0010000' '0010110' '0000011' '0001001' '1011010' '1011011' '1100001' '1001110' '1010100' '1000011' '1001001' '0110110' '0111100' '0111101' '0101010' '0110000' '0011101' '0100011' '0100100' '1001010' '0110111' '0111000' '0111110' '1000100' '0110001' '0011110' '0011111' '0100101' '0101011' '0110011' '0111001' '0111111' '1000101' '1001011' '0011010' '0100000' '0100110' '0101100' '0110010' '0111010' '1000000' '1000110' '1000111' '0110100' '0100001' '0100111' '0101101' '0101110' '0011011' '0101000' '0101001' '0101111' '0011100' '0100010' '1000001' '1000010' '1001000' '0110101' '0111011' n = 10 10 m = 100 7 y = 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

Suggested Problems

More from this Author56

Community Treasure Hunt

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

Start Hunting!