Gwalior Fort is a popular tourist place in Gwalior, Madhya Pradesh. Everyday peoples registered to visit Fort as token provided based on first come basis. Everyday limited numbers of peoples are allowed to visit. The management selects a number A1 randomly every day and generates another number A2 as count of the total number of set bits (i.e., total no. of 1s) in binary representation of all numbers from 1 to A1. For example, A1=3 then A2=4 [1(01) +2(10) +3(11)]. So, write a python module CountBit.py to find A2 using A1. You need to import this module in main program. In main program define a function
G-T(n) which takes a number and return value as shown in example by calling the appropriate function implemented in CountBit.py module.
Example-1 : Input : 3 , Output : 4
Example-2 : Input : 7 , Output : 12
CountBit.py:
# CountBit.py
def CountBit(a):
cnt = 0
while a > 0:
if a%2 == 1:
cnt += 1
a //= 2
return cnt
main.py:
# main.py
from CountBit import CountBit
def G_T(n):
cnt = 0
for i in range(1, n+1):
cnt += CountBit(i)
return cnt
def main():
A1 = int(input())
A2 = G_T(A1)
print(A2)
if __name__ == '__main__':
main()
Comments
Leave a comment