Cody

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

Solution 1208399

Submitted on 8 Jun 2017 by Augusto Mazzei
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))

x = 0

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

x = 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))

x = Columns 1 through 29 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 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 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 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 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 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 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 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 1 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 1 2 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 1 2 2 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 1 2 2 3 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 1 2 2 3 2 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 1 2 2 3 2 3 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = Columns 1 through 29 1 1 2 1 2 2 3 1 2 2 3 2 3 3 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Columns 30 through 58 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 Columns 59 through 87 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 Columns 88 through 100 88 89 90 91 92 93 94 95 96 97 98 99 100 x = ...

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))

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

### Community Treasure Hunt

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

Start Hunting!