Cody

# Problem 1547. Relative ratio of "1" in binary number

Solution 1226151

Submitted on 6 Jul 2017 by Anik Paul
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(ones_ratio(x),y_correct))

r = 0

2   Pass
x = 1; y_correct = 1; assert(isequal(ones_ratio(x),y_correct))

r = 1

3   Pass
x = 2; y_correct = 0.5; assert(isequal(ones_ratio(x),y_correct))

ans = 1 m = 0 bit = 1 0 i = 2 bit = 1 0 r = 0.5000

4   Pass
x = 7; y_correct = 1; assert(isequal(ones_ratio(x),y_correct))

ans = 2 m = 3 bit = 1 0 0 ans = 1 m = 1 bit = 1 1 0 ans = 0 m = 0 bit = 1 1 1 i = 3 bit = 1 1 1 r = 1

5   Pass
x = 128; y_correct = 0.125; assert(isequal(ones_ratio(x),y_correct))

ans = 7 m = 0 bit = 1 0 0 0 0 0 0 0 i = 8 bit = 1 0 0 0 0 0 0 0 r = 0.1250

6   Pass
x = 1265476542; y_correct = 19/31; assert(isequal(ones_ratio(x),y_correct))

ans = 30 m = 191734718 bit = Columns 1 through 29 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 27 m = 57516990 bit = Columns 1 through 29 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 25 m = 23962558 bit = Columns 1 through 29 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 24 m = 7185342 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 22 m = 2991038 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 21 m = 893886 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 19 m = 369598 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 18 m = 107454 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 16 m = 41918 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 15 m = 9150 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 13 m = 958 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 9 m = 446 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 8 m = 190 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 0 0 0 0 0 0 Columns 30 through 31 0 0 ans = 7 m = 62 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 0 0 0 0 Columns 30 through 31 0 0 ans = 5 m = 30 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 0 Columns 30 through 31 0 0 ans = 4 m = 14 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 0 0 Columns 30 through 31 0 0 ans = 3 m = 6 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 0 Columns 30 through 31 0 0 ans = 2 m = 2 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 1 Columns 30 through 31 0 0 ans = 1 m = 0 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 1 Columns 30 through 31 1 0 i = 31 bit = Columns 1 through 29 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 1 Columns 30 through 31 1 0 r = 0.6129

7   Pass
x = 98917653181; y_correct = 23/37; assert(isequal(ones_ratio(x),y_correct))

ans = 36 m = 3.0198e+10 bit = Columns 1 through 29 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 34 m = 1.3018e+10 bit = Columns 1 through 29 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 33 m = 4.4284e+09 bit = Columns 1 through 29 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 32 m = 133405373 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 26 m = 66296509 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 25 m = 32742077 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 24 m = 15964861 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 23 m = 7576253 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 22 m = 3381949 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 21 m = 1284797 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 20 m = 236221 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 17 m = 105149 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 16 m = 39613 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 15 m = 6845 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 12 m = 2749 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 11 m = 701 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 0 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 9 m = 189 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 0 0 0 0 0 0 0 0 ans = 7 m = 61 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 1 0 0 0 0 0 0 0 ans = 5 m = 29 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 1 0 1 0 0 0 0 0 ans = 4 m = 13 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 1 0 1 1 0 0 0 0 ans = 3 m = 5 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 1 0 1 1 1 0 0 0 ans = 2 m = 1 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 1 0 1 1 1 1 0 0 ans = 0 m = 0 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 1 0 1 1 1 1 0 1 i = 37 bit = Columns 1 through 29 1 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 Columns 30 through 37 1 0 1 1 1 1 0 1 r = 0.6216

### Community Treasure Hunt

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

Start Hunting!