Problem 808. Hamming Weight - Fast
48.33% Correct | 51.67% Incorrect
Last Solution submitted on Mar 06, 2019
Shlomo Geva on 29 Oct 2017
1. This solution is much faster on re-invocation than the one without the persistent num_ones variable. Unless of course it is performed on a much larger (than num_ones) array of 32-bit integer.
2. It is essential to have the statement
The reason for this is that the floor() function has a problem with precision. If can fail with 32-bit integer that are close to 2^32.
For instance, consider this Matlab code and system response:
Problem Recent Solvers10
How long is the longest prime diagonal?
More from this Author260
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!