Print Series of Prime Numbers in Python

Print series of prime numbers in python

You need to check all numbers from 2 to n-1 (to sqrt(n) actually, but ok, let it be n).
If n is divisible by any of the numbers, it is not prime. If a number is prime, print it.

for num in range(2,101):
prime = True
for i in range(2,num):
if (num%i==0):
prime = False
if prime:
print (num)

You can write the same much shorter and more pythonic:

for num in range(2,101):
if all(num%i!=0 for i in range(2,num)):
print (num)

As I've said already, it would be better to check divisors not from 2 to n-1, but from 2 to sqrt(n):

import math
for num in range(2,101):
if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
print (num)

For small numbers like 101 it doesn't matter, but for 10**8 the difference will be really big.

You can improve it a little more by incrementing the range you check by 2, and thereby only checking odd numbers. Like so:

import math
print 2
for num in range(3,101,2):
if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
print (num)

Edited:

As in the first loop odd numbers are selected, in the second loop no
need to check with even numbers, so 'i' value can be start with 3 and
skipped by 2.

import math
print 2
for num in range(3,101,2):
if all(num%i!=0 for i in range(3,int(math.sqrt(num))+1, 2)):
print (num)

Print prime numbers within an interval

for x in range (2,end):
if (i == 0 or i==1):
break
elif (i % x != 0):
print(i)
break
else:
break

This part of code should

print a number if it's prime, otherwise break

but it doesn't.


Do you notice something strange in your code? I do, and it's the fact that in every case the inner for loop breaks after the first iteration, rethink this aspect of your code, and it will work fine.

How can I display the first 25 prime numbers?

You can try this.

from sympy import isprime
i=1
count = 0
while count < 25:
if isprime(i):
print(i)
count += 1
i += 1

Print out a list of prime numbers in python

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)

Printing prime number in python

change range(3,i) to range(2,i) because if we use range(3,i), 4 is not checked with division by 2. As a result 4 is returned as prime.

for i in range(3,51):
flag=0
for j in range(2,i):
if(i%j==0):
flag=1
if(flag==0):
print(i)

However, a more structural and efficient way is to use the following:-

def isPrime(num):
for i in range(2,num/2+1):
if (num%i==0):
return False
return True

for i in range(3,51):
if isPrime(i):
print i

We don't need to check the division by all numbers till the number itself for prime. Because if we can check till the half of the given number only for increased efficiency.

my python program is to find prime numbers between two intervals but i am getting 9 in the output can you please tell what mistake i am doing

In case of 9, you're checking if the number is divisible by 2, it's not, and then you print the number and don't check anything else.

You're also looping till i+1, meaning you check if the number is divisible by itself, which it is...

Try changing those:

start = int(input('enter starting point of interval:'))
end = int(input('enter ending point of interval:'))
for i in range(start,end+1):
if i>1:
for j in range(2,i):
if (i % j == 0):
break
else:
print(i, end = " ")

Also, instead of checking if i>1 in every loop, change the loop conditions, and you can loop until the square root.

End result:

import math
start = int(input('enter starting point of interval:'))
end = int(input('enter ending point of interval:'))
for i in range(max(start, 2),end+1):
if i % 2 == 0 and i != 2: continue
if all((i%j!=0) for j in range(3,int(math.sqrt(i))+1, 2)):
print(i, end = " ")


Related Topics



Leave a reply



Submit