(n)
return 1 + count_ones(n & (n - 1))
Using Brian Kernighan’s Algorithm. (Recursive Approach)
Using Brian Kernighan’s Algorithm. (Recursive Approach)
Using Brian Kernighan’s Algorithm . ( Recursive Approach )
def count_ones(n):
    """Using Brian Kernighan’s Algorithm. (Recursive Approach)"""
    if not n: return 0
    return 1 + count_ones(n & (n - 1))
def count_ones ( n ) : if not n : return 0 return 1 + count_ones ( n & ( n - 1 ) )