-4

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
Warrior
  • 5
  • 4
  • 2
    Tell us what happens when you run your code, is there an error? There's no error, but the result is not the expected? – Ignatius Reilly Sep 10 '22 at 14:08
  • b is not included in your code; should range(a, b+1) – maggle Sep 10 '22 at 14:08
  • 1
    BTW, `range(a,b)` does not include `b`. Maybe you want to try `range(a,b+1)`. – Ignatius Reilly Sep 10 '22 at 14:09
  • 2
    Questions seeking debugging help ("why isn't this code working?") should include the desired behavior, a specific problem or error and the shortest code necessary. in the question itself. Questions without a clear problem statement are not useful to other readers. – Daniel Hao Sep 10 '22 at 14:09
  • 1
    @DanielHao actually there was no result so did not included that, thanks anyways I will specify more going forward. – Warrior Sep 10 '22 at 17:01
  • @IgnatiusReillygot the fault.thanks for looking. – Warrior Sep 10 '22 at 17:02
  • @IgnatiusReilly can you please tell why after having +1 in range only letting program to run because if range is (a,b) then also at least program should run , answer will be wrong but that is fine. – Warrior Sep 11 '22 at 04:22
  • @Warrior the same is valid for the second nested range. Your code is instructed to print a number If and only if `k==i`, but because in the nested loop `i` goes from 2 to ´k-1` (because range is not inclusive of the last value), it's impossible to fulfill that condition. So, change both ranges to `(a,b+1)` and `(2,k+1)` and see what happens :) – Ignatius Reilly Sep 11 '22 at 16:43
  • BTW, if it picks your curiosity and you are wandering _isn't python supposed to be simple and intuitive? why does it have to include one value but exclude the other one?_, you may want to give a look to [this](https://stackoverflow.com/questions/4504662/why-does-rangestart-end-not-include-end) and [this](https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF). – Ignatius Reilly Sep 11 '22 at 16:47
  • And to make it clearer, your code _is running_. The problem is that is not printing anything because the condition to print is not fulfilled. So, it runs, but doesn't print anything. One basic debugging technique would be to include a print statement after each condition, so you can "see" the flow of your program, which one is being executed step by step. You eliminate those prints for the final version. – Ignatius Reilly Sep 11 '22 at 16:54

5 Answers5

0

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)
0

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)

Nicholas Obert
  • 1,235
  • 1
  • 13
  • 29
0

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.

Warkaz
  • 845
  • 6
  • 18
  • thanks for optimizing , but can you tell why after having +1 in range only letting program to run because if range is (a,b) then also at least program should run , answer will be wrong but that is fine. – Warrior Sep 11 '22 at 04:20
  • You want both values to be inclusive (per your request), by default your gonna get a, a+1,..., b -1 with range, so to include it you need the +1. In the inner for loop you put first check if it is equal and then break, but in reality can just put range(2, k - 1) and even a theory to square root of the number. – Warkaz Sep 11 '22 at 06:36
0

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.

Module_art
  • 999
  • 2
  • 9
  • 26
0

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.

NoDakker
  • 3,390
  • 1
  • 10
  • 11