const halvingSum = (n: number): number => { let sum = 0; while (n !== 0) { sum += n; n = Math.floor(n / 2); } return sum; }
const halvingSum = (n: number): number => { if (n === 0) { return n; } return n + halvingSum(Math.floor(n / 2)) }
const halvingSum = (n: number, sum: number = 0): number => { if (n === 0) { return sum; } return halvingSum(Math.floor(n / 2), sum + n); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Looping
Recursion
Tail Recursion