Binary Sum
InterviewTime limit1sMemory limit128 MB
Given k, print in binary the sum of all integers from 1 to the largest k-digit binary number.
- Level
Medium5 of 10
- Topics
- Math, Bit manipulation
- Solved
- No attempts yet
Problem
You want to add up every natural number whose binary representation has at most digits.
A natural number is included in the sum when, written in binary, it has or fewer digits. In other words, you add every natural number from up to the largest number that fits in binary digits.
Given an integer , write a program that prints this sum in binary.
Input
The first line contains an integer . ()
Output
On the first line, print in binary the sum of every natural number whose binary representation has at most digits.
Hint
For example, when the natural numbers with at most binary digits are through . Their sum is , which in binary is .