Halving Sum

Java

public class HalvingSum {
  int halvingSum(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i *= 2) {
      sum += n / i;
    }

    return sum;
  }
}

Python

def halving_sum(n):
    i, sum = 1, 0
    while i <= n:
        sum += int(n / i)
        i *= 2;
    return sum

JavaScript

function halvingSum(n) {
  let i = 1, sum = 0
  while (i <= n) {
    sum += parseInt(n / i)
    i *= 2
  }

  return sum
}

Tags

  1. java (Private)
  2. python (Private)
  3. javascript (Private)
  4. 7-kyu (Private)
  5. codewars (Private)
  6. answer (Private)