How to print list of prime numbers in python

A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number.

2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime [it is composite] since, 2 x 3 = 6.

Source Code

# Python program to display all the prime numbers within an interval

lower = 900
upper = 1000

print["Prime numbers between", lower, "and", upper, "are:"]

for num in range[lower, upper + 1]:
   # all prime numbers are greater than 1
   if num > 1:
       for i in range[2, num]:
           if [num % i] == 0:
               break
       else:
           print[num]

Output

Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997

Here, we store the interval as lower for lower interval and upper for upper interval, and find prime numbers in that range. Visit this page to learn how to check whether a number is prime or not.

Here's a simple and intuitive version of checking whether it's a prime in a RECURSIVE function! :] [I did it as a homework assignment for an MIT class] In python it runs very fast until 1900. IF you try more than 1900, you'll get an interesting error :] [Would u like to check how many numbers your computer can manage?]

def is_prime[n, div=2]:

    if div> n/2.0: return True

    if n% div == 0:
        return False
    else:
        div+=1
        return is_prime[n,div]

#The program:
until = 1000
for i in range[until]:
    if is_prime[i]:
        print i

Of course... if you like recursive functions, this small code can be upgraded with a dictionary to seriously increase its performance, and avoid that funny error. Here's a simple Level 1 upgrade with a MEMORY integration:

import datetime
def is_prime[n, div=2]:
    global primelist
    if div> n/2.0: return True
    if div < primelist[0]:
        div = primelist[0]
        for x in primelist:
            if x ==0 or x==1: continue
            if n % x == 0:
                return False
    if n% div == 0:
        return False
    else:
        div+=1
        return is_prime[n,div]


now = datetime.datetime.now[]
print 'time and date:',now
until = 100000
primelist=[]
for i in range[until]:
    if is_prime[i]:
        primelist.insert[0,i]
print "There are", len[primelist],"prime numbers, until", until
print primelist[0:100], "..."

finish = datetime.datetime.now[]
print "It took your computer", finish - now , " to calculate it"

Here are the resuls, where I printed the last 100 prime numbers found.

time and date: 2013-10-15 13:32:11.674448

There are 9594 prime numbers, until 100000

[99991, 99989, 99971, 99961, 99929, 99923, 99907, 99901, 99881, 99877, 99871, 99859, 99839, 99833, 99829, 99823, 99817, 99809, 99793, 99787, 99767, 99761, 99733, 99721, 99719, 99713, 99709, 99707, 99689, 99679, 99667, 99661, 99643, 99623, 99611, 99607, 99581, 99577, 99571, 99563, 99559, 99551, 99529, 99527, 99523, 99497, 99487, 99469, 99439, 99431, 99409, 99401, 99397, 99391, 99377, 99371, 99367, 99349, 99347, 99317, 99289, 99277, 99259, 99257, 99251, 99241, 99233, 99223, 99191, 99181, 99173, 99149, 99139, 99137, 99133, 99131, 99119, 99109, 99103, 99089, 99083, 99079, 99053, 99041, 99023, 99017, 99013, 98999, 98993, 98981, 98963, 98953, 98947, 98939, 98929, 98927, 98911, 98909, 98899, 98897] ...

It took your computer 0:00:40.871083 to calculate it

So It took 40 seconds for my i7 laptop to calculate it. :]

A guide on how to print a list of prime numbers using Python.

As I am continuously striving to expand my knowledge of programming in Python, I have decided to try my hand at creating algorithms. As a result, I have found some questions that one can be asked at a coding interview. One such question is to print out the prime numbers from a range of numbers.

Prime numbers are special numbers, greater than 1, that have exactly two factors, themselves and 1.The prime numbers below 20 are: 2, 3, 5, 7, 11, 13, 17, 19. Don’t forget: the number 1 is not a prime number as it only has one factor.

Since the general rule of thumb is that one is not a prime number, it cannot be included in the printout of prime numbers that is generated by the function.

The pseudocode that I have written, with the help of the StackOverflow website, can be found below:

  1. The variables, lower and upper, which give the values of the upper and lower bands of the prime numbers to be printed are defined.
  2. The method, print_prime, is defined. This method accepts the variables, upper and lower, as input.
  3. The empty list, prime_list, is defined.
  4. A for loop is created to iterate through the lower value and the upper value plus 1.
  5. The variable, prime, is set to True.
  6. If num is 1 then prime is sent to False [because 1 is not considered a prime number].
  7. Another for loop is created where i iterates through the numbers 2 and num.
  8. If num modulo i is 0 then prime is False.
  9. Once the second for loop is iterated through, the first for loop will ask if num is prime. If num is prime then it will be appended to the list, prime_list.
  10. The method will be complete at this point and will return prime_list.
  11. The method, print_prime is then called and prime_list is printed out.

The code for this function can be seen in the screenshot below:

To conclude, many of the algorithms for the interview questions are readily available on the internet, but they need to be checked to ensure they are correct. For instance, the code that I found on StackOverflow was not correct because it included 1 as a prime number whilst 1 is not generally considered a prime number. I, therefore, had to modify the code to ensure that if the number is 1 then prime becomes False.

Another disadvantage of the algorithms being readily available on the internet, the programmer is not encouraged to think through the algorithm for himself. It is important, therefore, that a person goes through the code with a fine-tooth comb to ensure the understands it and can replicate it if asked the question during a coding interview.

That’s it for this topic. Thank you for reading.

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

How do you find a list of prime numbers?

The prime numbers from 1 to 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97. Why is 1 not a prime number? 1 is not a prime number because it has only one factor, namely 1.

How do I print all prime numbers?

First, take the number N as input. Then use a for loop to iterate the numbers from 1 to N. Then check for each number to be a prime number. If it is a prime number, print it.

How do you print the first 50 prime numbers in Python?

Program Code.
numr=int[input["Enter range:"]].
print["Prime numbers:",end=' '].
for n in range[1,numr]:.
for i in range[2,n]:.
if[n%i==0]:.
break..
print[n,end=' '].

How do you print prime numbers from 1 to 100 in Python?

num1 = input["Input a number: "] num2 = input["Input another number: "] for x in range[num1,num2]: prime = True for i in range[2,x]: if [x%i==0]: prime = False if prime == True: print x print "Done......"

Chủ Đề