Function to generate a palindrome - python

I have this following that I can't seem to solve correctly.
I have to write a function that takes a positive integer n as an input and checks if it is a palindrome (a number that's the same forward and backward). If it is, I have to return this number. If it isn't, I have to calculate the sum of the original number with the reversed number. This step is repeated until the result is a palindrome. This is the code I have so far:
x = input("Enter a positive integer: ")
def generate_palindrome(n):
if n == n[::-1]:
return n
else:
while n != n[::-1]:
r = n[::-1]
int(n) += int(r)
return n
generate_palindrome(x)
Of course this returns an error, since int(n) += int(r) is not possible. However, when not using int, it adds it as a string and that's also incorrect.
I'm not sure how far I'm off with the solution. How can I implement this correctly?

You are close. You just need another variable for the integer version of your string.Then just be clear which holds a string and which holds an integer:
def generate_palindrome(n):
if n == n[::-1]:
return n
else:
while n != n[::-1]: # n needs to be a string for this to work
r = int(n[::-1]) # r and s need to be numbers for correct addition
s = int(n)
n = str(r + s) # n needs to still be a string for the next loop iteration
return n
generate_palindrome("989")
# '989'
generate_palindrome("98")
# '8813200023188'
FWIW, I don't know that this is always guaranteed to return a reasonably sized number for every input. For example, I gave up on generate_palindrome("5798").

Related

Nth number of fibonacci

I am learning python. I am working on printing the Nth number of the Fibonacci sequence. I am reading a solution but I don't understand it. Could someone please explain how this solution works. I don't understand why it is recursively returning the next term and the current term? Shouldn't it recursively add the previous two terms? Where is terms defined as a list? Thanks for any help.
def fib(N):
"""
Recursively return the next term and the current term in the Fibonacci
sequence.
"""
if N == 1:
return [1, 0]
else:
terms = fib(N - 1)
terms = [terms[0] + terms[1], terms[0]]
return terms
def validate_positive_integer():
"""
Ask the user for input and only return when a positive integer under
500 is given.
"""
while True:
s = raw_input("Which term in the Fibonacci sequence you want to see? ")
try:
N = int(s)
if N >= 500:
print "Enter a number smaller than 500."
elif N > 0:
return N
else:
print "Enter a positive integer."
except ValueError:
print "Enter a positive integer."
def main():
N = validate_positive_integer()
print fib(N)[1]
if __name__ == "__main__":
main()
If you let F(n) denote the nth Fibonacci number, you'll see that fib(n) returns both F(n+1) and F(n). That gives you the desired Fibonacci number as well as precomputing the value that fib(n+1) will need to return F(n+2) and F(n+1).
terms is being defined as a list here, terms = [terms[0] + terms[1], terms[0], using the values from terms = fib(N - 1). Since the sequence is defined as the sum of the previous two terms, the two previously returned terms are added. Comment any more questions here

Belphegor prime number problem with Python

I have seen a video about Belphegor prime numbers. The definition of a Belphegor prime is one such that in the expression (10^(n+3)+666)*(10^(n+1)+1) is prime for n being a positive integer.
I tried making a Python program that would determine whether an input number n will produce a prime number in the expression mentioned before but my code said that for all n in the expression would be prime which is not true and it said it indefinitely.
My code:
n = int(input("Enter a positive number n: "))
x =(10**(n+3)+666)*10**(n+1)+1
for i in range(2,x - 1):
if x % i == 0:
print("Composite")
else:
print("Belphegor prime")
Please help me fix this error!
Your problem is that your code prints out either Composite or Prime on every iteration through the loop. Instead, you need to assume the number is prime and if at any time the test fails, set a flag to false; then use the state of the flag at the end of the loop to determine if the number is actually prime:
from math import sqrt
n = int(input("Enter a positive number n: "))
x =(10**(n+3)+666)*(10**(n+1)+1)
p = True
for i in range(2,int(sqrt(x))+1):
if x % i == 0:
p = False
break
if p:
print("Belphegor prime")
else:
print("Composite")
Note you can shorten your loop by only iterating as far as sqrt(x) as any number greater than sqrt(x) which is a divisor of x will have a matching divisor less than sqrt(x).
Note also that as #LeoE pointed out in the comments to your question, you are missing some () in your computation of x.

How can I display all numbers in range 0-N that are "super numbers"

The program asks the user for a number N.
The program is supposed to displays all numbers in range 0-N that are "super numbers".
Super number: is a number such that the sum of the factorials of its
digits equals the number.
Examples:
12 != 1! + 2! = 1 + 2 = 3 (it's not super)
145 = 1! + 4! + 5! = 1 + 24 + 120 (is super)
The part I seem to be stuck at is when the program displays all numbers in range 0-N that are "super numbers". I have concluded I need a loop in order to solve this, but I do not know how to go about it. So, for example, the program is supposed to read all the numbers from 0-50 and whenever the number is super it displays it. So it only displays 1 and 2 since they are considered super
enter integer: 50
2 is super
1 is super
I have written two functions; the first is a regular factorial program, and the second is a program that sums the factorials of the digits:
number = int(input ("enter integer: "))
def factorial (n):
result = 1
i = n * (n-1)
while n >= 1:
result = result * n
n = n-1
return result
#print(factorial(number))
def breakdown (n):
breakdown_num = 0
remainder = 0
if n < 10:
breakdown_num += factorial(n)
return breakdown_num
else:
while n > 10:
digit = n % 10
remainder = n // 10
breakdown_num += factorial(digit)
#print (str(digit))
#print(str(breakdown_num))
n = remainder
if n < 10 :
#print (str(remainder))
breakdown_num += factorial(remainder)
#print (str(breakdown_num))
return breakdown_num
#print(breakdown(number))
if (breakdown(number)) == number:
print(str(number)+ " is super")
Existing answers already show how to do the final loop to tie your functions together. Alternatively, you can also make use of more builtin functions and libraries, like sum, or math.factorial, and for getting the digits, you can just iterate the characters in the number's string representation.
This way, the problem can be solved in a single line of code (though it might be better to move the is-super check to a separate function).
def issuper(n):
return sum(math.factorial(int(d)) for d in str(n)) == n
N = 1000
res = [n for n in range(1, N+1) if issuper(n)]
# [1, 2, 145]
First I would slightly change how main code is executed, by moving main parts to if __name__ == '__main__', which will execute after running this .py as main file:
if __name__ == '__main__':
number = int(input ("enter integer: "))
if (breakdown(number)) == number:
print(str(number)+ " is super")
After that it seems much clearer what you should do to loop over numbers, so instead of above it would be:
if __name__ == '__main__':
number = int(input ("enter integer: "))
for i in range(number+1):
if (breakdown(i)) == i:
print(str(i)+ " is super")
Example input and output:
enter integer: 500
1 is super
2 is super
145 is super
Small advice - you don't need to call str() in print() - int will be shown the same way anyway.
I haven't done much Python in a long time but I tried my own attempt at solving this problem which I think is more readable. For what it's worth, I'm assuming when you say "displays all numbers in range 0-N" it's an exclusive upper-bound, but it's easy to make it an inclusive upper-bound if I'm wrong.
import math
def digits(n):
return (int(d) for d in str(n))
def is_super(n):
return sum(math.factorial(d) for d in digits(n)) == n
def supers_in_range(n):
return (x for x in range(n) if is_super(x))
print(list(supers_in_range(150))) # [1, 2, 145]
I would create a lookup function that tells you the factorial of a single digit number. Reason being - for 888888 you would recompute the factorial of 8 6 times - looking them up in a dict is much faster.
Add a second function that checks if a number isSuper() and then print all that are super:
# Lookup table for single digit "strings" as well as digit - no need to use a recursing
# computation for every single digit all the time - just precompute them:
faks = {0:1}
for i in range(10):
faks.setdefault(i,faks.get(i-1,1)*i) # add the "integer" digit as key
faks.setdefault(str(i), faks [i]) # add the "string" key as well
def fakN(n):
"""Returns the faktorial of a single digit number"""
if n in faks:
return faks[n]
raise ValueError("Not a single digit number")
def isSuper(number):
"Checks if the sum of each digits faktorial is the same as the whole number"
return sum(fakN(n) for n in str(number)) == number
for k in range(1000):
if isSuper(k):
print(k)
Output:
1
2
145
Use range.
for i in range(number): # This iterates over [0, N)
if (breakdown(number)) == number:
print(str(number)+ " is super")
If you want to include number N as well, write as range(number + 1).
Not quite sure about what you are asking for. From the two functions you write, it seems you have solid knowledge about Python programming. But from your question, you don't even know how to write a simple loop.
By only answering your question, what you need in your main function is:
for i in range(0,number+1):
if (breakdown(i)) == i:
print(str(i)+ " is super")
import math
def get(n):
for i in range(n):
l1 = list(str(i))
v = 0
for j in l1:
v += math.factorial(int(j))
if v == i:
print(i)
This will print all the super numbers under n.
>>> get(400000)
1
2
145
40585
I dont know how efficient the code is but it does produce the desired result :
def facto():
minr=int(input('enter the minimum range :')) #asking minimum range
maxr=int(input('enter the range maximum range :')) #asking maximum range
i=minr
while i <= maxr :
l2=[]
k=str(i)
k=list(k) #if i=[1,4,5]
for n in k: #taking each element
fact=1
while int(n) > 0: #finding factorial of each element
n=int(n)
fact=fact*n
n=n-1
l2.append(fact) #keeping factorial of each element eg : [1,24,120]
total=sum(l2) # taking the sum of l2 list eg 1+24+120 = 145
if total==i: #checking if sum is equal to the present value of i.145=145
print(total) # if sum = present value of i than print the number
i=int(i)
i=i+1
facto()
input : minr =0 , maxr=99999
output :
1
2
145
40585

Factorial calculation using Python

I am new to Python and currently reading Python 3 for absolute beginner and face following problem.
I would like to calculate factorial with procedure.
request user to input an non negative number n
then use for loop to calculate factorial
and the code is like that:
N = input("Please input factorial you would like to calculate: ")
ans = 1
for i in range(1,N+1,1):
ans = ans*i
print(ans)
while i would like to add a feature to check whether input number N is non-negative number. like:
if N != int(N) and N < 0:
I want the user to input N again if it is NOT non-negative number.
Thanks for your gentle help.
The construct could look like this:
while True:
N = input("Please input factorial you would like to calculate: ")
try: # try to ...
N = int(N) # convert it to an integer.
except ValueError: # If that didn't succeed...
print("Invalid input: not an integer.")
continue # retry by restarting the while loop.
if N > 0: # valid input
break # then leave the while loop.
# If we are here, we are about to re-enter the while loop.
print("Invalid input: not positive.")
In Python 3, input() returns a string. You have to convert it to a number in all cases. Your N != int(N) thus makes no sense, as you cannot compare a string with an int.
Instead, try to convert it to an int directly, and if that doesn't work, let the user enter again. That rejects floating point numbers as well as everything else which is not valid as an integer.
In Python's math library, there is a factorial function. You can use it like so:
import math
...
ans = math.factorial(N)
Since you want to calculate using a loop however, have you considered the following?
ans = -1
while ans < 0:
N = input("Please enter a positive integer: ")
if N.isdigit() == True:
n = int(N)
if n >= 0:
ans = n
for x in range (n-1, 1, -1):
ans *= x
print (ans)
Note, the second solution doesn't work for N = 0, where ans = 1 is correct by definition of factorial.
Number = int(input("Enter the number to calculate the factorial: "))
factorial = 1
for i in range(1,Number+1):
factorial = i*factorial
print("Factorial of ",Number," is : ", factorial)
def factorial(a):
if a == 1:
return 1
else:
return a * factorial(a - 1)
print('factorial of number', factorial(5))
Start
Declare Integer n,i,n!
Display “Enter a nonnegative integer.”
Input n
For i=1 to n-1 Step1,
Display “n!=i*n”
End for
Stop
You can check math module for python.
# math.factorial(x)
Return x factorial.
Raises ValueError if x is not integral or is negative.

Next higher prime and palindrome number

Is there any suggestion on solving next higher prime and palindrome number from a given int.
Here is the snippet I am trying but its a kind of slow, please suggest if you ve any good algorithm that i can test.
#!/usr/bin/python
def next_higher(n):
while True:
s = str(n)
if not any([n % i == 0 \
for i in range(2, int(n**0.5))]) and s == s[::-1]:
return n
n = n + 1
print next_higher(2004)
print next_higher(20)
Output:
10201
101
Updated code testing for palindrome before prime. much faster than my previous code.
I am implementing the suggestion from user2357112.
#!/usr/bin/python
def next_higher(n):
while True:
s = str(n)
if s == s[::-1]:
if not any([n % i == 0 \
for i in range(2, int(n**0.5))]):
return n
n = n + 1
print next_higher(2004111)
print next_higher(2004)
print next_higher(2004)
print next_higher(20)
There are quite a few optimizations you could do:
Like user2357.. suggested in the comments, test palindromeness first, and then check if the number is prime, since prime check is more expensive.
You don't need to check even number divisibility once you check is the number is divisible by 2. So you can change it to [2] + range(3, int(n**0.5) + 1, 2) to check only odd numbers after 2. (Also you need to do sqrt + 1 like I mentioned in the comments)
You should use () instead of []. [] generates the entire list of factors first and only then checks for any. If you use (), it creates a generator, so it stops as soon as a True value is found without calculating the entire list.
You should also use xrange instead of range for the same reason (xrange gives a generator, range gives a list)
You can use the Sieve of Eratosthenes algorithm to significantly reduce the time taken for prime number check.
You can also see if the palindrome check can be made faster. You can actually skip a whole lot of numbers instead of doing just + 1 each time.
Here is a version with most of these optimizations except the last two:
def next_higher(n):
if n % 2 == 0:
n = n - 1
while True:
n = n + 2
s = str(n)
if s == s[::-1]:
if not any((n % i == 0 for i in xrange(3, int(n**0.5) + 1, 2))):
return n
This should be pretty fast for your needs I believe. But you can do the last 2 optimizations to make it much more faster if you want.
Other than what has already been suggested,
What I suggest is that you first get the first palindrome number that is just higher than the given integer.
You can do this by trying to match the centre digits outwards.
Also, you should only check for numbers with odd number of digits, since if a number has even number of digits and it is a palindrome, then it will always be divisible by 11 and cannot be prime.
Once you get the first palindrome number that has odd number of digits and that is just higher than the current number, test it for primality and find the next palindrome number higher than this one.
You can do this by incrementing the centre digit.
Keep doing this till it rolls over to zero. In that case start incrementing the two neighbouring digits.
Continue, till you reach a prime number.
I tried optimizing the palindrome check i.e to find odd palindrome's.
Since the first digit should be odd number, i focused on that part.
Here's the code below with the assumptions its greater than 1 digit.
def next_odd_palindrome(n):
"""to check the next odd palindrome number"""
if n%2==0:
n=n-1
while True:
n=n+2
s = str(n)
if int(s[0])%2==0:
n = int(str(int(s[0])+1)+ s[1:])
s = str(n)
if s==s[::-1]:
return n
let me know if anything wrong.
Just for the fun of it, I implemented all optimizations by Hari Shankar and Abhishek Bansal.
It first finds the higher odd length palindrome, then increment the palindrome in a way that keeps its palindromity. Then checks each number using prime numbers calculated by Sieve method in the beginning.
This can process up to n=10^14 (can be higher if you increase the CACHE size) under 1 second in my computer =D
primes = []
CACHE = int(10**7) # Cache size for Sieve
# Custom class for immediate printing of output
import sys
class Unbuf:
def __init__(self,stream):
self.stream = stream
def write(self,data):
self.stream.write(data)
self.stream.flush()
sys.stdout = Unbuf(sys.stdout)
def sieve():
global primes
is_prime = [False,False]+([True]*(CACHE-1))
for i in xrange(2,int(CACHE**0.5)):
if is_prime[i]:
is_prime[i*i::i] = [False]*((CACHE-i*i+i)/i)
primes = [num for num, bool_prime in enumerate(is_prime) if bool_prime]
def is_prime(n):
"""Checks whether n is prime"""
global primes
if n<2:
return False
if n==2:
return True
for prime in primes:
if prime>n**0.5+1:
return True
if n%prime==0:
return False
# For the case that the number is bigger than the square of our largest prime
for num in xrange(primes[-1]+2,n**0.5+1,2):
if n%num==0:
return False
return True
def next_higher_odd_length_palindrome(n):
n = str(n)
if len(n)%2==0: # Even length, take the smallest odd length (10(00)*1)
n = '1'+('0'*(len(n)-1))+'1'
else:
middle_idx = len(n)/2
left = int(n[:middle_idx+1])
left_cmp = n[middle_idx::-1]
right_cmp = n[middle_idx:]
# If mirroring left part to right part
# makes the number smaller or equal, then
if right_cmp>=left_cmp:
# Increase the left half number
left = left+1
# Mirror left part to the right part
n = str(left)+str(left)[-2::-1]
return n
def next_higher(n):
if n<=1:
return 2
# Ensure the number is a palindrome of odd length
n = next_higher_odd_length_palindrome(n)
while True:
if is_prime(int(n)):
return int(n)
n = next_higher_odd_length_palindrome(n)
if int(n[0])%2==0:
new_lead = str(int(n[0])+1)
n = new_lead+n[1:-1]+new_lead
import time
print 'Sieving...',
start_time = time.time()
sieve()
print 'Done in %.3fs' % (time.time() - start_time)
print next_higher(2004111)
print next_higher(2004)
print next_higher(20)
while True:
n = int(raw_input('Enter n: '))
start_time = time.time()
result = next_higher(n)
print 'Next higher prime palindrome: %d (calculated in %.3fs)' % (result, time.time() - start_time)
Which in my computer gives this output:
Sieving... Done in 1.444s
3007003
10301
101
Enter n: 1999999999
Next higher prime palindrome: 10000500001 (calculated in 0.004s)
Enter n: 1999999999999
Next higher prime palindrome: 3000002000003 (calculated in 0.051s)
Enter n: 1000000000000
Next higher prime palindrome: 1000008000001 (calculated in 0.030s)
Enter n:

Categories