Cody

# Problem 2738. Sideways sum

Solution 2105505

Submitted on 25 Jan 2020 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
N = 1; num_ones = 1; assert(isequal(popcount(N),num_ones))

b = '1' population = 1

2   Pass
N = 2; num_ones = 1; assert(isequal(popcount(N),num_ones))

b = '10' population = 1

3   Pass
N = 3; num_ones = 2; assert(isequal(popcount(N),num_ones))

b = '11' population = 2

4   Pass
for num_ones = 3:10 N = 2^num_ones-1; assert(isequal(popcount(N),num_ones)) end

b = '111' population = 3 b = '1111' population = 4 b = '11111' population = 5 b = '111111' population = 6 b = '1111111' population = 7 b = '11111111' population = 8 b = '111111111' population = 9 b = '1111111111' population = 10

5   Pass
for num_ones = 1:50 N = sum(2.^(find(randperm(50)<=num_ones)-1)); assert(isequal(popcount(N),num_ones)) end

b = '100000000000000000000000000000000000000000000000' population = 1 b = '1000000000000001000000000000000000000000000' population = 2 b = '10100001000000000000000000000000000' population = 3 b = '100000000001000000000000100010' population = 4 b = '1010000000000000000000000000000001100100' population = 5 b = '10000001100000010000000000010000010000000' population = 6 b = '110000001000000000100000000100000010000000010000' population = 7 b = '11000000000100000000000100100100011000' population = 8 b = '10000000010000000010100000001000010101000000000001' population = 9 b = '1100000000010100010000000010010000100000100010' population = 10 b = '1001010000110001010100000010001000001000' population = 11 b = '1100001000001001000001100101001000001000000100000' population = 12 b = '1001100000000001010001000110001100001000000100010' population = 13 b = '10000100101010000010000100000100110010000010000101' population = 14 b = '10001000010100001100100101110100001010000000000010' population = 15 b = '11000000110100101011101000000000100111000000100' population = 16 b = '100000010011001101010010100100010011000100011' population = 17 b = '1110100010101001010100000001001011010001100010000' population = 18 b = '100011010001110000010101100101011100100000010100' population = 19 b = '11000010001100001100000010111010100110000100101011' population = 20 b = '101111101011001010110000000010000111010000101001' population = 21 b = '11011000000000111010011011011100001010011010011' population = 22 b = '110011011111111000010111111000001000000010101000' population = 23 b = '10010110110101101010100100100010011101110100010010' population = 24 b = '100011001100100011101110000111011010100100011111' population = 25 b = '1100110111101001010100110101111001011100000110001' population = 26 b = '11111001111011010011000100100101011000101011100110' population = 27 b = '1101011001110011101011101001010110011001101110010' population = 28 b = '1101000011011010111100001010010100111101111110111' population = 29 b = '1100100100111101001000111111000111111001111111100' population = 30 b = '10001111101111110100010111010111001101110011010110' population = 31 b = '10101011101101101010110011101101001011111111010110' population = 32 b = '11001100100111001110111111101101111011100111001110' population = 33 b = '11110010100111111000100110110111111111111011110001' population = 34 b = '110110111111101101111000111111110001101111111100' population = 35 b = '11101011000101100101111011111111011101011111011111' population = 36 b = '11101011101111110011010111011001011011111111101111' population = 37 b = '111111101101110111111111100110100111110111110111' population = 38 b = '10111111011111110111011111100111101111101100110111' population = 39 b = '111111111010111111101111011010101011111111111111' population = 40 b = '10111111111101111010111111011111011111100111111110' population = 41 b = '11010111101101111111111111111111111111110101110011' population = 42 b = '11111111100011111111111111101011101110111111111111' population = 43 b = '11111101110101111111111111011111110111110111111111' population = 44 b = '10111111111111110111111111110011111111111011111111' population = 45 b = '11110011111111111011111011111111111111111111111111' population = 46 b = '11111111111111111011111111111110111111110111111111' population = 47 b = '11111111111111111111101111111111111101111111111111' population = 48 b = '11111111110111111111111111111111111111111111111111' population = 49 b = '11111111111111111111111111111111111111111111111111' population = 50

6   Pass
N = 23; num_ones = 4; assert(isequal(popcount(N),num_ones))

b = '10111' population = 4

7   Pass
N = 123; num_ones = 6; assert(isequal(popcount(N),num_ones))

b = '1111011' population = 6

8   Pass
N = 123456; num_ones = 6; assert(isequal(popcount(N),num_ones))

b = '11110001001000000' population = 6

9   Pass
N = 1234567; num_ones = 11; assert(isequal(popcount(N),num_ones))

b = '100101101011010000111' population = 11

10   Pass
N = 1125899906842501; num_ones = 45; assert(isequal(popcount(N),num_ones))

b = '11111111111111111111111111111111111111111110000101' population = 45

### Community Treasure Hunt

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

Start Hunting!