여기서 우리는 다른 파이썬 내장 함수를 사용합니다. 먼저 bin()을 사용하여 숫자를 이진수로 변환한 다음 문자열의 이진 형식을 역전시켜 원본과 비교하고 일치하면 회문을 그렇지 않으면 회문과 비교합니다.
예시
Input: 5 Output: palindrome
설명
5의 이진 표현은 101입니다.
그것을 반대로 하면 결과가 101이 되고, 원본과 비교하여 일치시킵니다.
그래서 회문
알고리즘
Palindromenumber(n) /* n is the number */ Step 1: input n Step 2: convert n into binary form. Step 3: skip the first two characters of a string. Step 4: them reverse the binary string and compare with originals. Step 5: if its match with originals then print Palindrome, otherwise not a palindrome.
예시 코드
# To check if binary representation of a number is pallindrome or not
defpalindromenumber(n):
# convert number into binary bn_number = bin(n)
# skip first two characters of string
# Because bin function appends '0b' as
# prefix in binary
#representation of a number bn_number = bn_number[2:]
# now reverse binary string and compare it with original
if(bn_number == bn_number[-1::-1]):
print(n," IS A PALINDROME NUMBER")
else:
print(n, "IS NOT A PALINDROME NUMBER")
# Driver program
if __name__ == "__main__":
n=int(input("Enter Number ::>"))
palindromenumber(n)
출력
Enter Number ::>10 10 IS NOT A PALINDROME NUMBER Enter Number ::>9 9 IS A PALINDROME NUMBER