Skip to content

Instantly share code, notes, and snippets.

@Getmrahul
Created July 30, 2016 06:16
Show Gist options
  • Save Getmrahul/b4e95b9010db76351ff42f0c66ac43b9 to your computer and use it in GitHub Desktop.
Save Getmrahul/b4e95b9010db76351ff42f0c66ac43b9 to your computer and use it in GitHub Desktop.
Find sum of first N numbers which has only two set bits!
# 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