Answer to Question #309030 in Python for XYZ

Question #309030

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


1
Expert's answer
2022-03-10T07:17:04-0500

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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS