How do you print prime numbers from 1 to 10 in python?

A prime number is a natural number which is greater than 1 and has no positive divisor other than 1 and itself, such as 2, 3, 5, 7, 11, 13, and so on.

The user is given two integer numbers, lower value, and upper value. The task is to write the Python program for printing all the prime numbers between the given interval (or range).

To print all the prime numbers between the given interval, the user has to follow the following steps:

  • Step 1: Loop through all the elements in the given range.
  • Step 2: Check for each number if it has any factor between 1 and itself.
  • Step 3: If yes, then the number is not prime, and it will move to the next number.
  • Step 4: If no, it is the prime number, and the program will print it and check for the next number.
  • Step 5: The loop will break when it is reached to the upper value.

Example: The Python Code to Print the Prime Number between the given Interval.

Output:

Please, Enter the Lowest Range Value:  14
Please, Enter the Upper Range Value:  97
The Prime Numbers in the range are: 
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Conclusion

In this tutorial, we have shown how to write the code to print the prime numbers between the given interval of numbers.


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. :)

How do you print prime numbers in Python?

Example: The Python Code to Print the Prime Number between the given Interval..
# First, we will take the input:.
lower_value = int(input ("Please, Enter the Lowest Range Value: ")).
upper_value = int(input ("Please, Enter the Upper Range Value: ")).
print ("The Prime Numbers in the range are: ").

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

To print all the prime numbers up to N, we start one loop from 2 to N and then inside the loop we check current number or “num” is prime or not. To check if it is prime or not we again need one nested loop. It is not an efficient way to check prime number but it is simpler to understand the basic of looping in Python.

How do you print the first 10 prime numbers?

Algorithm.
STEP 1: START..
STEP 2: SET ct =0, n =0, i= 1, j=1..
STEP 3: REPEAT STEP 4 to 12 UNTIL n<10..
STEP 4: j =1..
STEP 5: ct =0..
STEP 6: REPEAT STEP 7 to 9 UNTIL j<=i..
STEP 7: if i%j==0 then..
STEP 8: ct = ct+1..

How do you find the prime numbers from 1 to 50 in Python?

lower = int(input("Enter lower range: ")).
upper = int(input("Enter upper range: ")).
for num in range(lower,upper + 1):.
if num > 1:.
for i in range(2,num):.
if (num % i) == 0:.
break..