Cody

Solution 1477471

Submitted on 3 Apr 2018 by Gareth Lee
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))

ans = 1

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

ans = 1

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

ans = 2

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

ans = 3 ans = 4 ans = 5 ans = 6 ans = 7 ans = 8 ans = 9 ans = 10

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

ans = 1 ans = 2 ans = 3 ans = 4 ans = 5 ans = 6 ans = 7 ans = 8 ans = 9 ans = 10 ans = 11 ans = 12 ans = 13 ans = 14 ans = 15 ans = 16 ans = 17 ans = 18 ans = 19 ans = 20 ans = 21 ans = 22 ans = 23 ans = 24 ans = 25 ans = 26 ans = 27 ans = 28 ans = 29 ans = 30 ans = 31 ans = 32 ans = 33 ans = 34 ans = 35 ans = 36 ans = 37 ans = 38 ans = 39 ans = 40 ans = 41 ans = 42 ans = 43 ans = 44 ans = 45 ans = 46 ans = 47 ans = 48 ans = 49 ans = 50

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

ans = 4

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

ans = 6

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

ans = 6

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

ans = 11

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

ans = 45