Kalinjar Fort is a popular tourist place in Bundelkhand, Utter 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 N1 randomly every day and generates another number N2 as count of the total number of set bits (i.e., total no. of 1s) in binary representation of all numbers from 1 to N1. For example, N1=3 then N2=4 [1(01) +2(10) +3(11)]. So, write a python module CountBit.py to find N2 using N1. You need to import this module , define a function Get_Token(n) which takes a number and return value as shown in example by calling the appropriate function implemented in CountBit.py module.
Â
Â
Example-1
Example-2
Example-3
Input:
3
Â
Output:
4
Â
Input:
7
Â
Output:
12
Input:
8
Â
Output:
13
Â
import random
N1 = random.randint(0,10)
N2=0
for r in range(0,N1+1):
  binary = bin(r)
  bits_1 = [ones for ones in binary[2:] if ones=='1']Â
  N2 = N2 + len(bits_1)
print("N1 = ",N1)
print("N2 = ",N2)
Comments
Leave a comment