Belphegor数是回文数,因此该数的形式为
1(0...)666(0...)1
第N个Belphegor编号由下式给出:
检查N是否是Belphegor号码
给定数字N ,任务是检查N是否是Belphegor数字。如果N是Belphegor数字,则打印“是”,否则打印“否” 。
例子:
Input: N = 16661
Output: Yes
Explanation:
This number is palindrome and is in the form 1(0…)666(0…)1.
Input: N = 1661
Output: No
方法:想法是检查数字是否为回文,如果为回文,则检查数字应为1(0…)666(0…)1格式,如果数字为此格式,则打印“是”。 ” ,否则打印“否” 。
下面是上述方法的实现:
Python3
# Python3 implementation
# of the above approach
# Function to check if the number
# N is in the form
# 1(0...)666(0...)1
def isinform(n):
temp = str(n)
Len = len(temp)
# basic case
if "666" not in temp or\
Len<5 or Len % 2 == 0:
return False
mid = Len//2
if not (temp[mid-1] == temp[mid]\
and temp[mid-1] == '6' and\
temp[0] == '1'):
return False
for i in range(mid + 2, Len-1):
if temp[i] != '0':
return False
return True
# Function to check if the number
# N is palindrome
def ispalindrome(n):
temp = str(n)
if temp == temp[::-1]:
return True
return False
# Function to check if a number
# N is Belphegor
def isBelphegor(n):
if ispalindrome(n):
if isinform(n):
return True
return False
# Driver Code
n = 100666001;
if isBelphegor(n):
print("Yes")
else:
print("No")
输出:
Yes