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. Continue dividing the number until you reach a decimal (no decimals allowed in final answer).
Example
25 => 25 + 12 + 6 + 3 + 1 = 47
Tests
halvingSum(25)
halvingSum(127)
This challenge comes from Belia on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (16)
:v
Haskell
Verbose C# solutions 😄
C
Looping
Recursion
Tail Recursion
COBOL
Run it at jdoodle.com/a/280D
Here is a Python solution,
Befunge-93
Edit:
Here is a version with comments
Learning Typescript