Created
July 30, 2016 06:16
Find sum of first N numbers which has only two set bits!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# give input in single line with spaces | |
noc = int(raw_input()) | |
for case in xrange(0,noc): | |
N = int(raw_input()) | |
i = 1 | |
totalFound = 0 | |
totalSum = 0 | |
while totalFound != N: | |
count = 0 | |
tmpN = i | |
while tmpN: | |
tmpN &= (tmpN-1) | |
count += 1 | |
if count > 2: | |
break | |
if count == 2: | |
totalSum += i | |
totalFound += 1 | |
i += 1 | |
print totalSum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment