-3

While writing a program to find the prime numbers in a certain range, the code prints every prime number multiple times instead of just listing it once.

This is the code:

first_interval = int(input('enter the beginning of the interval'))
second_interval=int(input('enter the end of the interval'))

for digit in range((first_interval),(second_interval+1)):
    if digit>1:
        for i in range(2,digit):
            if (digit %i)==0:
                break
            else: print(digit)

I was expecting every prime number in the interval (4,21) to be listed just once. What I was expecting:

7
11
13
17
19

What I got:

5
5
5
7
7
7
7
7
9
11
11
11
11
11
11
11
11
11
13
13
13
13
13
13
13
13
13
13
13
15
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
21
khelwood
  • 55,782
  • 14
  • 81
  • 108
akar
  • 1
  • 1
    check your indentation, it matters. – Julien Jan 25 '23 at 09:17
  • You print `digit` every time you find a number that is not a factor of `digit`. You need to wait until you know that there are *no* factors (i.e. after your for-loop finishes without finding a factor). – khelwood Jan 25 '23 at 09:17
  • Does this answer your question? [Prime number check acts strange](https://stackoverflow.com/questions/18833759/prime-number-check-acts-strange) – Remi Cuingnet Jan 25 '23 at 09:18

4 Answers4

0

Instead of printing out digit on every iteration, use a variable is_prime and set it to False whenever you find a divisor of digit, then print out digit after the for loop if is_prime turns out to be True.

first_interval = int(input('enter the beginning of the interval: '))
second_interval = int(input('enter the end of the interval: '))

for digit in range(first_interval, second_interval+1):
    if digit > 1:
        is_prime = True
        for i in range(2, digit):
            if (digit % i) == 0:
                is_prime = False
                break
        if is_prime:
            print(digit)

Sample input/output:

enter the beginning of the interval: 1
enter the end of the interval: 100
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
Fractalism
  • 1,231
  • 3
  • 12
0

When generating a list of prime numbers in a range you first need to ensure that the low value is at least 2. 2 is the lowest and only even prime number and therefore for greatest efficiency should be treated as a special case.

Once 2 has been dealt with (if relevant) then your loop can increment the test value by 2 because from then on you only need to work with odd numbers.

Here's an efficient function that tests for prime numbers followed by a suggestion for how you could implement your code:

from math import sqrt, floor

def isprime(n):
    if n < 2:
        return False
    if n == 2 or n == 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    for i in range(5, floor(sqrt(n))+1, 6):
        if n % i == 0 or n % (i + 2) == 0:
            return False
    return True

start = int(input('enter the beginning of the interval: '))
stop = int(input('enter the end of the interval: '))

# ensure that the start value is at least 2
# if it's exactly 2 just print it
if (start := min(max(2, start), stop)) == 2
    print(2)

# ensure that the start value is an odd number
start |= 1

# now work in increments of 2
for n in range(start, stop+1, 2):
    if isprime(n):
        print(n)

Output:

enter the beginning of the interval: 4
enter the end of the interval: 21
5
7
11
13
17
19
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

You could use all to check whether digit is prime or false and print it:

first_interval = int(input('enter the beginning of the interval : '))
second_interval=int(input('enter the end of the interval : '))

for digit in range(first_interval,second_interval):
    if digit > 1 and all(digit % i for i in range(2,digit)):
        print(digit) 
enter the beginning of the interval : 4
enter the end of the interval : 21
5
7
11
13
17
19
Arifa Chan
  • 947
  • 2
  • 6
  • 23
-1

It is a good idea to first write a function that determines if a number is prime or not and then call this function for each number in the desired range:

def is_prime(number):
    if number < 2:
        return False
    for i in range(2,number):
        if number % i == 0: 
            return False
    return True

first_interval = int(input('enter the beginning of the interval: '))
second_interval = int(input('enter the end of the interval: '))

for digit in range(first_interval,second_interval+1):
    if is_prime(digit):
        print(digit)
Clarai
  • 21
  • 4