## Task

Given a positive integer `n`, calculate the following sum: 

```
n + n/2 + n/4 + n/8 + ...
``` 

All elements of the sum are the results of integer division.

## Example

```
25  =>  25 + 12 + 6 + 3 + 1 = 47
```

Difficulty: introductory

Starter code:

def halving_sum(n):
	

Solutions:
def halving_sum(n): 
    s=0
    while n: 
        s+=n ; n>>=1
    return s

def halving_sum(n): 
    if n == 1:
        return 1
    else:
        return n + halving_sum(n//2)

def halving_sum(n): 
    return n and n + halving_sum(n>>1)

def halving_sum(n):
    result = n
    
    while n >= 1:
        n //= 2
        result += n
        
    return result

import math
def halving_sum(n): 
    return sum(n//(2**x) for x in range(int(math.log2(n))+1))

def halving_sum(n):
    return n + halving_sum(n // 2) if n else 0

def halving_sum(n): 
    L=[n]
    for i in range(1,n+1):
        a = n//(2**i)
        if a not in L:
            L.append(a)
    return sum(L)

def halving_sum(n):
    r = 0
    while n:
        r += n
        n >>= 1
    return r

def halving_sum(n):
  if n == 1:
    return 1
  else:
    return n + halving_sum(int(n/2))

def halving_sum(n): 
    divisor = 1
    addends = []
    addend = n // divisor
    while addend > 0:
        addends.append(addend)
        divisor *= 2
        addend = n // divisor
    return sum(addends)

def halving_sum(n): 
    # your code here
    re = n
    while n > 1:
        n = n//2
        re += n
    return re

def halving_sum(n): 
    return n + halving_sum(n >> 1) if n > 1 else n

def halving_sum(n):

    b = n // 2
    c = []
    c.append(n)
    c.append(b)


    for i in range(0,n):

        b = b // 2
        c.append(b)

    return sum(c)

def halving_sum(n):
    total = 0
    
    while True:
        if n == 0:
            break
        else:
            total += (n)
            n = n // 2
    
    return total

def halving_sum(n):
    if n < 1:
        return 0
    else:
        return n + halving_sum (n // 2)


def halving_sum(n): 
    return 1 if n<2 else n + halving_sum((int)(n/2))
    


def halving_sum(n): 
    sum = 0
    while n != 1:
        sum = sum + n
        n = n // 2
    return sum+1

def halving_sum(n):
    result = n

    if n < 1:
        return n
    else:
        while n > 1:
            result += n // 2
            n = n // 2
        return result

print((halving_sum(25)))


def halving_sum(n): 
    if n > 1 :
        n = n + halving_sum(n//2)
    return n

def halving_sum(n): 
    result = 0

    while n:
        result += n
        n //= 2

    return result

def halving_sum(n): 
    return 1 if n<=1 else n + halving_sum(n//2)

def halving_sum(n): 
    return sum(n//2**i for i in range(14))

def halving_sum(n): 
    return n if n <= 1 else n + halving_sum(n >> 1)

def halving_sum(n): 
    total = n
    while n > 0:
        n //= 2
        total += n
    return total

def halving_sum(n): 
    return n if n == 1 else n + halving_sum(n // 2)

def square_gen(n):
    x = 1
    while x <= n:
        yield x
        x *= 2
        
def halving_sum(n):
    return sum([int(n/i) for i in square_gen(n)])

import math
def halving_sum(n):
    counter = 0
    div = n
    result = 0
    while div >= 1:
        div = math.floor(n/2**counter)
        counter += 1
        result += div
    return result
        


def halving_sum(n): 
    return sum(n >> i for i in range(15))

def halving_sum(n): 
    cnt = 2
    s = n
    
    while n//cnt > 0:
        s += n//cnt
        cnt *= 2

    return s

def halving_sum(n): 
    # your code here
    total = n
    while n > 0:
        n = n//2
        total += n
    return total
    pass

def halving_sum(n): 
    return 1 if n//1 == 1 else n//1 + halving_sum((n//1)/2)


def halving_sum(n): 
    sum = 1
    while (n != 1):
        sum+=n
        n = n//2
    return (sum)

def halving_sum(n): 
    my_list = []
    while n:
        my_list.append(n)
        n = n//2
    return sum(my_list)

import math
def halving_sum(n): 
    return sum([ n // 2 ** i for i in range(math.ceil(math.log(n, 2)))]) if n > 1 else 1

def halving_sum(n): 
    if n//2 < 1:
        return n
    if n//2 == 1:
        return n + 1
    return n + halving_sum(n//2)

def halving_sum(n):
    add = n
    while n>1:
        n=n//2
        add += n
        
    return add

def halving_sum(n): 
    return n if n <= 1 else halving_sum(n//2) + n

def halving_sum(n):
    summa = 0
    while n != 1:
        summa += n
        n = n // 2 
    return summa + 1

def halving_sum(n): 
    sum =  0
    exp = 0
    while(n // (2 ** exp)):
        sum += (n // (2 ** exp))
        exp += 1
    return sum

def halving_sum(n):
    result = n
    denom = 2
    while n // denom > 0:
        result += n // denom
        denom *= 2
    return result

def halving_sum(n): 
    tot = 0
    while n != 1:
        tot += n
        n = n // 2
    return tot + 1


def halving_sum(n): 
    m = n
    summ = 0
    count = 1
    while n // count >= 1:
        summ += n // count
        count *= 2
    return summ


import math

def halving_sum(n):
    half = n/2
    if half <= 1:
        return math.ceil(half)
    else:
        return math.floor(n + halving_sum(half))

def halving_sum(n): 
    sum = n
    while n > 1:
        n //= 2 # n=n//2
        sum += n
    return sum

def halving_sum(n): 
    hold = 0
    x = 1
    while n//x > 0:
        hold += n//x
        x *= 2
    return hold

import math
def halving_sum(n):
    if n < 1:
        return 0
    elif n == 1:
        return 1
    return math.floor(n) + halving_sum(n/2)

def halving_sum(n):
    res=n
    while n != 1:
        n = int(n/2)
        res+=n
        
    return res

def halving_sum(n): 
    # your code here
    print(n)
    i = 1
    suma = 0
    resultado = 0
    while i < n:
        #print(i)
        suma = n/i
        print((int(suma)))
        resultado = resultado + int(suma)
        #print(int(resultado))
        #print(i)
        i *= 2
        #print(i)
    if(int(resultado) == 0):
        resultado = 1
        return(int(resultado))
    else:
        print((int(resultado)))
        return(int(resultado))


def halving_sum(n): 
    a = 1
    b = 0
    while n//a != 1:
        b = b + n//a
        a = a*2
    return b + 1

def halving_sum(n): 
    sum = 0
    while n>=1:
        sum += n
        n >>= 1
    return sum

def halving_sum(n): 
    sum = n
    while True:
        if n//2==0: break
        else      : n = n//2
        sum += n
    return sum

def halving_sum(n): 
    a = [n]
    b = n
    while n > 1:
        c = n//2
        a.append(c)
        b += c
        n = c
    return b

from itertools import takewhile, count

def halving_sum(n):
    return sum(takewhile(lambda x: x >= 1, map(lambda y: n//2**y, count())))

def halving_sum(n): 
    sm=0
    while n>0 :
        sm+=n ; n//=2
    return sm

def halving_sum(n):
    start = n
    total = 0
    while start >= 1:
        total += start
        start = int(start / 2)
    return total

def halving_sum(n): 
    if n<=1: return n
    return n + halving_sum(n//2)

import math
def halving_sum(n):
    ne = 1
    while n > 1:
        ne += n
        n = n/2
        n = math.floor(n)
    return ne

def halving_sum(n): 
    k = n
    t = n
    while k > 0:
        k = int(k /2)
        t+=k
    return t

def halving_sum(n): 
    suma = 0
    sumando = n
    while sumando >= 1:
        suma = suma+ sumando
        sumando = sumando //2
    return suma

def halving_sum(n):
    i, sum = 2, n
    while(i <= n):
        sum += n//i
        i += i
    return sum

def halving_sum(n):
    summed = 0
    x = 1
    while n // x > 0:
        summed += n // x
        x *= 2

    return summed


def halving_sum(n): 
    sum = 0
    if n >= 1:
        sum += n + halving_sum(n//2)
    return sum
    


def halving_sum(n): 
    a=[]
    for i in range(n):
        a.append(n)
        n=int(n/2)
        if int(n) >= 1:
            pass
        else:
            break
    return sum(a)

def halving_sum(n): 
    result = 0
    while n > 0:
        result += n
        n = int(n / 2)
    return result

def halving_sum(n):  
    if n > 1:
        n += halving_sum(n//2)
    return n


def halving_sum(n): 
    r=n
    while(n>0):
        n//=2
        r+=n
    return r

def halving_sum(n): 
    running_total = 0
    while (n > 0):
        running_total += n
        n = n//2
    return running_total


def halving_sum(n): 
    div = 2
    total = n
    while n//div:
        total += n//div
        div *= 2
    return total

def halving_sum(n):
    sum = n
    divide = n
    while divide // 2:
        divide = divide // 2
        sum += divide
    return sum


def halving_sum(n): 
    sm = 0
    i = 0
    while n // 2**i > 0:
        sm += n // 2**i
        i += 1
    return sm


def halving_sum(number): 

    n = number

    while number != 1:
        number = number//2
        n += number

    return n

def halving_sum(n): 
    sum_list = []
    sum_int = 0
    sum_list.append(int(n))
    while(n != 1):
        sum_list.append(int(n)//2)
        n = int(n)//2
    for q in range(len(sum_list)):
        sum_int = sum_int + sum_list[q]
    return sum_int

def halving_sum(n): 
    l=0
    while n!=1:
        l+=n
        n=n//2
    return l+1

def halving_sum(n): 
    output = 0
    while n > 0:
        output += n
        n //= 2
    return output

def halving_sum(n): 
    half = 0
    while n > 1:
        half += n
        n = n//2
    return half +1

def halving_sum(n): 
    i = 0
    x = 0
    while True:
        if n // 2 ** i > 0:
            x = x + n // 2 ** i
            i = i + 1
        else:
            break
    return x

def halving_sum(n): 
    if n == 1:
        return n
    else:
        print(n)
        return n + halving_sum(n//2)

def halving_sum(n):
    return 1 if n == 1 else sum([n//(2**i) for i in range(0, n//2) if n//(2**i) >=1])

import math
def halving_sum(n):
    return 1 if n == 1 else sum([math.floor(n/2**i) for i in range(0,math.floor(n/2)) if math.floor(n/2**i) >= 1])

def halving_sum(n): 
    m = 0
    while n >= 1:
        m += n
        n //= 2
    return m

def halving_sum(n):
    sums=[]
    nums=n
    while nums>0:
        sums.append(nums)
        nums=int(nums/2)
        if nums<=0:
            break
    return sum(sums)

def halving_sum(n): 
    # your code here
    b=1
    top=0
    while b<=n:
        top+=int(n/b)
        b*=2
    return top

def halving_sum(n): 
    res = n
    while n != 1:
        n = n//2
        res += n
    return res

def halving_sum(n):
    total = n
    divisor = 2
    while n // divisor > 0:
        if n // divisor == 1:
            total += 1
            break
        else:
            total += (n // divisor)
            divisor *= 2
    return(total)

def halving_sum(n): 
    # your code here
    res=0
    while n>1:
        res=res+n
        n=n//2
    return res+1

def halving_sum(n):
    c=0
    while n>=1:
        c=c+n
        n=n//2
    return c

from itertools import count, takewhile

def halving_sum(n): 
    g = (2**j for j in count(0))
    g = takewhile(lambda x: n >= x, g)
    return sum(n // i for i in g)


import unittest


def halving_sum(n):
    temp = []
    while True:
        if n == 1:
            break
        temp.append(n)
        n = n // 2
    return sum(temp) + 1
    
    
class TestHalvingSum(unittest.TestCase):
    def test_halving_sum_when_given_n_is_25(self):
        n = 25
        actual = halving_sum(n)
        self.assertEqual(actual, 47)

    def test_halving_sum_when_given_n_is_127(self):
        n = 127
        actual = halving_sum(n)
        self.assertEqual(actual, 247)


import math

def halving_sum(n): 
    a = 0
    d = 1
    res = 0
    while a != 1:
        a = math.floor(n / d)
        d *= 2
        res += a
    return res

def halving_sum(n): 
    delit = 2
    sum = n
    while n > 0:
        n = n//delit
        delit*2
        sum+=n
    return sum

import math
def halving_sum(n):
    accum = 0
    while n > 0:
        accum += n
        n = math.floor(n/2)
    return accum


from math import trunc
def halving_sum(n): 
    result = divided = n
    divisor = 1
    while divided > 1:
        divisor *= 2
        divided = n // divisor
        result += divided
    return result

def halving_sum(n): 
    
    x = n  
    
    while n != 1:
        x += n//2
        n //= 2
        
    return x

def halving_sum(n): 
    a = 0
    while n >= 1:
        a += n
        n *= 1/2
        n  = int(n)
    return a

def halving_sum(n): 

    number = n
    while number > 1:
        n += int(number/2)
        number /= 2
    return n

import math
def halving_sum(n): 
    # your code here
    number = n
    
    while number >= 1:
        n += (math.floor(number/2))
        number /= 2
        
    return n

import math

def halving_sum(n): 
    score = 0

    while (n >= 1):
        score += n

        n = math.floor((n * 1) / 2)
    
    return score

def halving_sum(n):
    total_sum = n

    while n != 1:
        n = n // 2
        total_sum += n
    
    return total_sum


def halving_sum(n): 
    answer = 0
    while n > 0:
        answer += n
        n = n // 2
    return answer

def halving_sum(n):
    
    x = 2
    sol = n
    
    while (n//x)>0:
        sol = sol + n//x
        x*=2
        
    return sol