Prime numbers from 2 to n in python

The program must accept an integer N as the input. The program must print all the prime numbers from 2 to N (inclusive of N) as output.

Boundary Condition(s):
2 <= N <= 999999

Input Format:
The first line contains the value of N.

Output Format:
The first line contains all the prime numbers from 2 to N.

Example Input/Output 1:
Input:
11

Output:
2 3 5 7 11 

Example Input/Output 2:
Input:
120

Output:
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 101 103 107 109 113

 def prime(n):
    pr=[True for i in range(n+1)]
    p=2
    while(p*p<=n):
        if pr[p]==True:
            for i in range(p*p,n+1,p):pr[i]=False
        p+=1
    for p in range(2,n+1):
        if pr[p]:print(p,end=' ')
n=int(input())
prime(n)

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.


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two positive integers start and end. The task is to write a Python program to print all Prime numbers in an Interval.

    Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.

    The idea to solve this problem is to iterate the val from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.

    Below is the Python implementation: 

    Python3

    def prime(x, y):

        prime_list = []

        for i in range(x, y):

            if i == 0 or i == 1:

                continue

            else:

                for j in range(2, int(i/2)+1):

                    if i % j == 0:

                        break

                else:

                    prime_list.append(i)

        return prime_list

    starting_range = 2

    ending_range = 7

    lst = prime(starting_range, ending_range)

    if len(lst) == 0:

        print("There are no prime numbers in this range")

    else:

        print("The prime numbers in this range are: ", lst)

    Output: 

    The prime numbers in this range are: [2,3,5]

    Time Complexity: O(N2), where N is the size of the range.

    Auxiliary Space: O(N), since N extra space has been taken.

    The above solution can be optimized using the Sieve of Eratosthenes. Please see print prime numbers in a range for details. 

    How do you find prime numbers up to n in python?

    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.

    How do you print prime numbers from 1 to 10 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=' ').

    Is 2 a prime number python?

    All lists of prime numbers begin with 2. Thus, the smallest prime number is 2 and not 1.