All co-binary numbers in a range in Python
In this problem, we need to find all the numbers of a co-binary palindrome that exist in a given range (start, end) in Python.
Now you must have thought about what is a co-binary palindrome? A co-binary palindrome is a number which is a palindrome in both ways, when it is a decimal number and when it has been binary converted.
In: start=000 , end=800 Out: Co-Binary numbers are : [0, 1, 3, 5, 7, 9, 33, 99, 313, 585, 717]
Now let’s understand its implementation through the help of code.
Code(Python): find all the numbers of a co-binary palindrome that exist in a given range
- Here first we convert the coming number to Binary number
- After conversion, we reverse the number and check for if it is palindrome or not.
- We have declared the highest and the lowest value. Between which the program or the function will run.
- You may set your own limits and check for other results.
def Binary_conversion(args): return bin(args).replace("0b","") def reverse(n): n = str(n) return n[::-1] def check_palindrome(num): if num == int(reverse(num)) : return 1 else: return 0 # starting number start = 000 # ending number end = 800 bin_all=  for j in range(start,end+1): if check_palindrome(j)== 1 and check_palindrome( int(Binary_conversion(j))): bin_all.append(j) print("Co-Binary numbers are:",bin_all)
Co-Binary numbers are: [0, 1, 3, 5, 7, 9, 33, 99, 313, 585, 717] [Program finished]
I hope you understand the concept clearly. Try running the code, if you have any doubt you may drop a comment. Your feedback will be appreciated.