Home > Back-end >  Write a python script to print all Prime numbers between two given numbers (both values inclusive)
Write a python script to print all Prime numbers between two given numbers (both values inclusive)

Time:09-11

Write a python script to print all Prime numbers between two given numbers (both values inclusive) can anyone please tell what am I doing wrong here ?

a = int(input("Enter the value of a : "))  
b = int(input("Enter the value of b : "))  

for k in range(a,b):
    for i in range(2,k):
        if k%i!=0:
            if k!=i:
                continue
        elif k==i:
            print(k)
            break
        elif k!=i:
            break

CodePudding user response:

you are checking if a number is prime the wrong way there are many unhandled cases in your code for example if a is larger than b you will start looping from "a" anyway and even if the values are sat up correctly the algorithm is not right here is my optimal solution hope it will help

a = int(input("Enter the value of a : "))  
b = int(input("Enter the value of b : "))  

def is_prime(n):
    # negative numbers cannot be primes 1 and 0 are also not primes
    if n <= 1:
        return False
    # since 2 is the first prime we will start looping from it
    # until n since you mentioned that n is included
    for i in range(2, n   1):
        # if n is cleanly divisible by any number less than n
        # that means that n is not prime
        if n % i == 0 and n != i:
            return False
    return True
for k in range(a,b):
    if a > b:
        print ("a cannot be bigger than b")
    if is_prime(k):
        print(k)

CodePudding user response:

Here's the solution. I added some comments to explain what the code does.

a = int(input("Enter the value of a : "))  
b = int(input("Enter the value of b : "))  

# Include b in the range by adding 1
for num in range(a, b   1):

    # Prime numbers are greater than 1
    if num > 1:

        for i in range(2, num):

            # If the number is divisible, it is not prime
            if num % i == 0:
                # Check if the number is equal to the number itself
                if num != i:
                    break
        
        else:
            # If the loop was not broken, the number isn't divisible
            # by other numbers except itself and 1, so it's prime
            print(num)

CodePudding user response:

Well, a prime is "a number that is divisible only by itself and 1", so to actually first I would go only to range(2, k-1). This approach you have is one of the most straightforward ways of doing it, and is not computationally friendly. There are algorithms that specialize in this kind of prime number finding.

I have fixed the code, simplifying the expressions and adding like already mentioned 1 for inclusivity.

a = int(input("Enter the value of a : "))  
b = int(input("Enter the value of b : "))  

for k in range(a,b 1):
    for i in range(2,k 1):
        if k==i:
            print(k) 
        elif k%i!=0:
            continue
        else: # is divisible and isn't 1 or the number
            break

I encourage you to see this post, how they do it.

CodePudding user response:

An other way of doing it would be:

#Check only 1 number at time:
def check_prime(check):
    if check > 1:
        for i in range(2, check):
            if check % i == 0:
                return False
        return True
    return False

#then check your numbers in a loop
list_prime = []
for i in range(50, 250):
    if check_prime(i):
        list_prime.append(i)

print(list_prime)

That way you can check 1 possible prime number at time. If you need numbers in between just put it in loop.

CodePudding user response:

Usually, when generating prime numbers utilizing some form of the "Sieve of Erotosthenes" algorithm, it is only necessary to check for denominator values up to the square root of the number being evaluated. With that in mind, here is one more possible take on your prime number loop test.

import math

a = int(input("Enter the value of a : "))  
b = int(input("Enter the value of b : "))  

for k in range(a,b):
    if k == 2 or k == 3:        # Pick up the prime numbers whose square root value is less than 2
        print(k)
    x = int(math.sqrt(k)   1)   # Need only check up to the square root of your test number
    for i in range(2,x):
        if k%i!=0:
            if x-1 > i:
                continue
            else:
                print(k)
        else:                   # If the remainder is zero, this is not a prime number
            break

Another version to try.

  • Related