I am very confused, I do not know what kind of equation I should make. I cannot think of anything other than 1 that satisfies this condition. Can you give me some guidance on how to write this code?
For example, let me enter 3 as the number n. From ((2 ^ n) -1) comes = 7. I need to find a 7-digit palindrome number like 1670761
This code should work:
def len_x_palimdrome(x,f,start = ""):
#f - a callable object taking one input and returning a bool
nums = "0123456789"
for i in nums:
if x == 1:
if f(int(start+i+start)):
return(i)
elif x == 2:
if f(int(start+i*2+start)):
return(i*2)
else:
a = len_x_palimdrome(x-2,f)
if a != None:
return(i+a+i)
return(None)
The function takes in the length and a function lambda or whatever that will be used as a filter. Note that the function will return the lowest palindrome that fulfills the requirement(s) defined in the function.
Example filter function:
def f(n):
if n == 0:
return False
return n%7 == 0
In conclusion:
print(len_x_palimdrome(2**3-1,f))