PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#From 1 to N, N being user given input, prints all numbers that satisfy the following: If the amount of 1s in each individual digit's binary representation, concatanated, from right to left up to the length of the binary representation of the current processed number, in the given current processed number equals the amount of 1s in the processed number's binary representation.
#i.e. 179: BCD = 0001 0111 1001 (5 ones)
# 179 = 10110011 (5 ones)
#So 179 would be printed.
import sys
def bcdConversion(cur_dig):
bcd = binaryConversion(cur_dig)
while len(bcd) < 4:
bcd = "0" + bcd
return bcd
def binaryConversion(cur_num):
if int(cur_num) // 2 == 0:
return str(int(int(cur_num)%2))
else:
return binaryConversion(int(cur_num) // 2) + str(int(int(cur_num)%2))
def oneCount(cur_num):
count = 0
for d in cur_num:
if d == "1":
count += 1
return count
try:
N = int(input("Enter number to end on: "))
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run