Print out a list of prime numbers in python - python

The question is: print out all numbers from 1 to 1000 that satisfy the two conditions:
Those are prime numbers
The numbers after being reversed are also prime numbers.
e.g., 13 satisfies (as 13 and 31 are prime numbers), but 19 does not satisfy (19 is a prime number, while 91 is not).
My codes:
def prime(n):
if n<2:
return False
for i in range(1, n):
if n%i == 0:
return False
else:
return True
def reverse(n):
List = []
while n>0:
List.append(n%10)
n = n//10
string = [str(integer) for integer in List]
a_string = "".join(string)
result = int(a_string)
print(result)
L = []
for i in range (1, 1000):
if prime(i) == prime(reverse(i)) == True:
L.append(i)
print(L)
Mine seems to contain some errors as the outcome is not as expected, either it shows none or still shows 19 in the list.
Thank you in advance!

First your prime method is wrong, because the loops starts at 1, and every numbre satisfies n%1 == 0 , it needs to starts at 2
def prime(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
Then your reverse method returns nothing so reverse(5) gives None, you have tried it manually.
def reverse(n):
values = []
while n > 0:
values.append(n % 10)
n = n // 10
return int("".join(map(str, values)))
Then simplify the condition to be
for i in range(1, 20):
if prime(i) and prime(reverse(i)):
L.append(i)
The reverse process can be done with string also, and so short that it can be inlined
L = []
for i in range(1, 20):
if prime(i) and prime(int(str(i)[::-1])):
L.append(i)

It would seem running prime twice is very inefficient. I would recommend the following:
def prime(n):
if n<2:
return False
for i in range(2, n):
if n%i == 0:
return False
return True
l = [i for i in range(1, 1001) if prime(i)]
print(*(x for x in l if int(str(x)[::-1]) in l))

Related

Printing the sum of prime divisors of each number in a given range in python

Here's my code for printing the sum of prime divisors of each number between 18 to 25. But it is only printing 5.
For example:
18 has prime factors 2, 3 hence sum = 5,
19 is prime, hence sum of factors = 0,
20 has prime factors 2 and 5, hence sum = 7,
21 has prime factors 3 and 7, hence sum = 10,
22 has prime factors 2 and 11, hence sum = 13,
23 is prime. hence sum = 0,
24 has prime factors 2 and 3, hence sum = 5,
25 has prime factor 5, hence sum = 5
Therefore, it should print [5,0,7,10,13,0,5,5]
I believe I should use break statement but i tried it didn't work. As I am beginner in python, any kind of help much appreciated.
def isPrime(n):
i = 2
while i * i <= n:
# n has a factor, hence not a prime
if (n % i == 0):
return False
i += 1
# we reach here if n has no factors
# and hence n is a prime number
return True
def summ(l, r):
summ = 0
arrayofdivisors = []
arrayofsum = []
# iterate from lower to upper
for i in range(l, r + 1) :
# if i is prime, it has no factors
if (isPrime(i)) :
continue
for j in range(2, i):
# check if j is a prime factor of i
if (i % j == 0 and isPrime(j)) :
arrayofdivisors.append(j)
if(len(arrayofdivisors)>1):
ans = sum(arrayofdivisors)
arrayofsum.append(ans)
return arrayofsum
# Driver code
if __name__ == "__main__":
l = 18
r = 25
print(summ(l, r))
Try this
def isPrime(n) :
if (n <= 1) :
return False
if (n <= 3) :
return True
if (n % 2 == 0 or n % 3 == 0) :
return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def Sum(N):
SumOfPrimeDivisors = [0] * (N + 1)
for i in range(2, N + 1) :
if (SumOfPrimeDivisors[i] == 0) :
for j in range(i, N + 1, i) :
SumOfPrimeDivisors[j] += i
return SumOfPrimeDivisors[N]
arr=[]
for i in range(18,26):
if isPrime(i):
arr.append(0)
else:
arr.append(Sum(i))
print(arr)
It's almost impossible to tell because of the way you've formatted things, but the problem here is that your "return" statement is indented one position too far. Thus, you return at the end of the first loop, before it has a chance to check anything else. Un-indent the return arrayofsum and it should work.
Then, you are initializing arrayofdivisors outside the main loop, so it just kept getting larger and larger. Then, you weren't adding 0 to the list for prime numbers, as your spec required.
Note that if statements in Python do not need parentheses, like they do in C.
def isPrime(n):
i = 2
while i * i <= n:
# n has a factor, hence not a prime
if n % i == 0:
print(n,'not prime')
return False
i += 1
# we reach here if n has no factors
# and hence n is a prime number
print(n,"prime")
return True
def summ(l, r):
# iterate from lower to upper
arrayofsum = []
for i in range(l, r + 1) :
arrayofdivisors = []
# if i is prime, it has no factors
if isPrime(i):
arrayofsum.append(0)
continue
for j in range(2, i):
# check if j is a prime factor of i
if i % j == 0 and isPrime(j):
arrayofdivisors.append(j)
arrayofsum.append( sum(arrayofdivisors) )
return arrayofsum
# Driver code
if __name__ == "__main__":
l = 18
r = 25
print(summ(l, r))

Finding the sum of prime numbers between m and n (m and n included in the sum)

def isPrime(n, i):
if i == n-1:
return ("True")
elif n%i == 0:
return ("False")
else:
return isPrime(n, i+1)
def sumOfPrime(m,n):
if m > 0 and n > 0 and m <= n:
if isPrime(m,2)==True:
temp = temp + m
return temp
else:
return (sumOfPrime(m+1,n))
else:
return temp
how can I fix the error "UnboundLocalError: local variable 'temp' referenced before assignment" without using a global variable
I reviewed your code, and this is my proposal:
def isPrime(n, i=None):
if i is None:
i = n - 1
while i >= 2:
if n % i == 0:
return False
else:
return isPrime(n, i-1)
else:
return True
def sumOfPrime(m, n):
sum = 0
for value in range(m, n+1):
if isPrime(value):
sum = sum + value
return sum
# --- test ---
result = sumOfPrime(1, 9)
print (result) # <-- prints 18
If the difference between m and n is quite high, then it is recommended that you use some type of sieve, to filter primes out in a given range. Otherwise, iterating over numbers from m to n and checking if the number is prime, it is going to be expensive for large m and n.
def is_prime(n):
if n <= 2:
return n > 1
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
def prime_range_sum(m, n):
return sum(i for i in range(m, n + 1) if is_prime(i))
print(prime_range_sum(1, 9))
# prints 17
Here's my version, which I kept as close as possible to the original, while fixing some errors and making some adjustments.
def isPrime(n, i=2): # since you always use 2, just make it default
if i == n-1:
return True # return a boolean True instead of a string
elif n%i == 0:
return False # return a boolean False instead of a string
else:
return isPrime(n, i+1)
def sumOfPrime(m,n,total=0): # we will need to carry the total around, make default to 0
if 0 < m <= n: # we can simplify this complex condition
if isPrime(m):
total += m # if it's prime, increase the total...
return sumOfPrime(m+1, n, total) # and pass it to the next recursion
return total # if this is the last recursion, return total
# Example run
total = sumOfPrime(10,45)
print(total) # prints 264

Finding prime_factors that returns the prime factors of an integer as a list

def prime_factors(n):
i = 2
lst = []
while i <= n:
if isprime(i):
lst.append(i)
i += 1
return lst
def isprime(n):
n = isdivisor(n)
i = 2
lst = []
for j in n:
while i <= j/2:
if j % i != 0:
return True
else:
return False
i += 1
def isdivisor(n):
i = 2
lst = []
while i <= n/2:
if n % i == 0:
lst.append(i)
i += 1
return lst
print prime_factors(15)
First, I tried to find divisors of integer n and checked the divisors whether they are prime or not.However the output is somehow [10, 14, 15] instead of [3,5]. Is it because I can't do the assignment n = isdivisor(n) or something else ?
I just wrote a pice of code to do It, without using libs. I hope It can help you.
my code:
def get_square_root(n):
"""Return square root of n."""
return n ** (1.0 / 2)
def is_divisor(n, d):
"""Return True if n divides d, False otherwise."""
return n % d == 0
def is_prime(n):
"""Return True if n is prime, False otherwise."""
limit = int(get_square_root(n))
for i in range(2, limit+1):
if is_divisor(n, i):
return False
return True
def get_prime_factors(n):
"""Return a list of the prime factors of n."""
prime_factors = []
for i in range(2, n):
if is_divisor(n, i) and is_prime(i):
prime_factors.append(i)
return prime_factors
if __name__ == '__main__':
n = 15 # Change n to test
prime_factors = get_prime_factors(n)
print(prime_factors)
output:
[3, 5]
There are some logical errors in your code. You are overcomplicating things somewhat by returning lists of divisors and primes. You already have a loop in the prime_factors() to go through all the numbers from 2 to n. So you can simplify your isdivisor() and isprime() functions:
def isdivisor(i, n):
return n % i == 0
def isprime(n):
i = 2
while i < n:
if n % i == 0:
return False
i += 1
return True
Then change your main loop in prime_factors() like this:
def prime_factors(n):
i = 2
lst = []
while i < n:
if isdivisor(i, n) and isprime(i):
lst.append(i)
i += 1
return lst
Output:
>>>prime_factors(15)
[3,5]

Check if list contains alternating primes and perfect squares

I am just getting started in Python Programming. I had a problem on checking if a given list contains alternating sequence of primes and perfect squares. The list can start with either a prime or a perfect square. I came up with a solution but it's not efficient as it generates unwanted lists. Is this possible with more efficient Python code?
First I'm creating functions to generate a list of primes as well as perfect squares up to the max value of testing list. Functions squaretest() and primecheck():
def squaretest(num):
sqlist=[]
i=1
while i**2 <= num:
sqlist.append(i**2)
i+=1
return sqlist
def primecheck(num):
primelist=[]
for i in range(2,num + 1):
for p in range(2,i):
if (i % p) == 0:
break
else:
primelist.append(i)
return primelist
Then I am dividing the given list into lists of even index and odd index elements and checking all elements of them against the primelist and the squarelist:
def primesquare(l):
if len(l)==1:
primelist = primecheck(l[0])
sqlist = squaretest(l[0])
return (l[0] in primelist) or (l[0] in sqlist)
else:
ol=[]
el=[]
for i in range(0,len(l),2):
ol.append(l[i])
for p in range (1, len(l),2):
el.append(l[p])
primelist = primecheck(max(l))
sqlist = squaretest (max(l))
return((all(x in primelist for x in el)) == True and (all(y in sqlist for y in ol)) == True) or ((all(x in primelist for x in ol)) == True and (all(y in sqlist for y in el)) == True)
It works.
Any suggestions will be really helpful.
You can use sets to check if all members of a list are in another list.
def primesquare(l):
if len(l) == 0:
return True
primelist = set(primecheck(max(l)))
sqlist = set(squaretest(max(l)))
ol = set(l[::2])
el = set(l[1::2])
odds_are_primes = ol.issubset(primelist)
odds_are_squares = ol.issubset(sqlist)
evens_are_primes = el.issubset(primelist)
evens_are_squares = el.issubset(sqlist)
return (odds_are_primes and evens_are_squares) or (odds_are_squares and evens_are_primes)
I came up with a solution but it's not efficient as it generates
unwanted lists.
Assuming the unwanted lists are the two lists representing the even and odd elements, then we can fix that. (Eliminating the primes and squares list is a whole 'nother problem.) Below is my rework of your code -- we don't create addtional lists but rather with a couple of reusable ranges which are objects that produce integer sequences as needed, but not stored in memory.
Your any() design is efficient in that the arguments are generator expressions, not lists, which are computed as needed. As soon as a flaw is found in the array, the whole thing stops and returns False--it doesn't need to process the rest:
def squares(number):
return {x * x for x in range(int(number ** 0.5) + 1)}
def primes(number):
prime_set = set()
for i in range(2, number + 1):
for p in range(2, int(i ** 0.5) + 1):
if (i % p) == 0:
break
else: # no break
prime_set.add(i)
return prime_set
def primesquare(array):
if not array:
return True # define as the problem demands
length, maximum = len(array), max(array)
odd, even = range(0, length, 2), range(1, length, 2)
prime_set, square_set = primes(maximum), squares(maximum)
return all(array[i] in prime_set for i in even) and all(array[i] in square_set for i in odd) or all(array[i] in prime_set for i in odd) and all(array[i] in square_set for i in even)
I admire #AndreySemakin's set-based solution (+1), and use sets above, but his solution generates the same lists you want to eliminate (just in the form of sets).
I came up with this solution:
def primesquare(lst):
# checking if the first element is either perfect square or a prime
if not lst or (not checksquare(lst[0]) and not checkprime(lst[0])):
return False
length = len(lst)
if length == 1:
return True
if checksquare(lst[0]):
# if first element is square then make s(quare)=2 and p(rime)=1
s, p = 2, 1
else:
# if first element is prime then make s=1 and p=2
s, p = 1, 2
# running perfect square loop from s to len-1 with gap of 2 and checking condition
for i in range(s, length, 2):
if not checksquare(lst[i]):
return False
# running prime loop from p to len-1 with gap of 2
for i in range(p, length, 2):
if not checkprime(lst[i]):
return False
return True
def checksquare(n): # function to check perfect square
if n < 0:
return False
if 0 <= n <= 1:
return True
for i in range(int(n ** 0.5) + 1):
if i * i == n:
return True
return False
def checkprime(n): # function to check prime
if n < 2:
return False
if n % 2 == 0:
return n == 2
for i in range(3, int(n ** 0.5) + 1, 2):
if n % i == 0:
return False
return True

Generating large prime numbers in python

I can't seem to make random prime numbers using this code, please can someone help me?
def RandomPrime():
prime = False
while prime == False:
n = random.randint(10000, 100000)
if n % 2 != 0:
for x in range(3, int(n**0.5), 2):
if n % x ==0:
prime = False
else:
prime = True
return n
Imagine what happens if the last number in range(3, int(n**0.5), 2) is not an integer divisor of n:
if n % x ==0:
prime = False # not this
else:
prime = True # this
So even if all previous checks evaluated False, you call n a prime. The minimal change to your code to fix this is:
prime = prime and True # or 'prime &= True'
So if prime is already False, it remains False.
However, bear in mind that, for primality, if any of those checks is False n is not prime. You can use this and Python's and and all (which are evaluated lazily, i.e. don't keep checking once finding a False) to implement much more efficiently:
def rand_prime():
while True:
p = randint(10000, 100000)
if (r % 2 != 0 and
all(p % n != 0 for n in range(3, int(((p ** 0.5) + 1), 2))):
return p
For even better performance, note that randrange incorporates a step argument, just like range, so you can skip all of the even numbers (which definitely aren't prime!):
def rand_prime():
while True:
p = randrange(10001, 100000, 2)
if all(p % n != 0 for n in range(3, int((p ** 0.5) + 1), 2)):
return p
Note: sqrt(n) (from math) is, in my opinion, a little clearer to other, less-technical readers than n ** 0.5 (although it may or may not be more efficient).
Take a look to the tabs: The else should refer to the whole for loop, not to the iF
def RandomPrime():
prime = False
while prime == False:
n = random.randint(10000, 100000)
if n % 2 != 0:
for x in range(3, int(n**0.5), 2):
if n % x ==0:
break
else:
prime = True
return n
There're errors in your code:
Incorrect "else:"; you can't declare number being prime if a remainder is not 0;
All the remaiders should be non-zeros
int(n*0.5) should be int(n*0.5 + 1) to prevent round-up errors
The possible solution is
def RandomPrime():
while True:
n = random.randint(10000, 100000)
if n % 2 == 0:
continue;
prime = True;
for x in range(3, int(n**0.5 + 1), 2):
if n % x == 0:
prime = False;
break;
if prime:
return n
Correct logic, you are setting True when n % x ! = 0 for first time:
for x in range(3, int(n**0.5), 2):
if n % x ==0:
prime = False
else:
prime = True
should be:
prime = False
for x in range(3, int(n**0.5), 2):
if n % x ==0:
break
else:
prime = True
Read break and continue Statements, and else Clauses on Loops.
The shorter way of writing equivalent code will be (from #Steve Jesso):
prime = all(n % x != 0 for x in range(3, int(n**0.5), 2)
Generating big primes over and over can cost a lot of time. For this reason I used increments.
import random
def generate_big_prime(size):
p = random.randrange(2 ** (size - 1), 2 ** size - 1)
if p % 2 == 0:
p += 1
while not is_prime(p):
p += 2
return p
Where is_prime() is a primality test. I would recommend the Miller-Rabin primality test for its efficiency.

Categories