Home > OS >  How to let prime number print once in Python
How to let prime number print once in Python

Time:12-01

My prime number finding code is trying all numbers so it will print the prime number a few times. Eg: 5/2 , 5/3, 5/4. So it will print 5 for 3 times.

def prime_number_generator(num):
    for n in range (1,num):
        for prime in range (2,n):
            if (n%prime) ==0:
                break
            else:
                yield n

number = input('Please input the number:') 
if number.isnumeric():
    num = int(number)
    if num <= 2:
        print('Number must be greater than 2') 
    else: 
        for prime in prime_number_generator(num):
            print(prime, end=', ') 
else: 
    print('Must be a positive integer') 

Output with input as 27:

3, 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, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 
23, 23, 23, 23, 23, 23, 23, 23, 25, 25, 25,

CodePudding user response:

That's not your only problem. You are also (incorrectly) returning 15 as prime because you are immediately assuming that the number is prime if it can't be divided by 2 (the first iteration of your second for loop). Try this (note the indentation of the else):

def prime_number_generator(num):
    for n in range (2, num):
        for prime in range (2, n):
            if (n % prime) == 0:
                break
        else:
            yield n

See this question for an explanation of for..else construct in Python.

CodePudding user response:

The Problem

In this function:

def prime_number_generator(num):
    for n in range (1,num):
        for prime in range (2,n):
            if (n%prime) ==0:
                break
            else:
                yield n

You are yielding n if it is not exactly divisible by prime. Here is an example for what happens when n = 5:

  • First, prime = 2. n % prime != 0, so n, i.e, 5 is yielded
  • Then, prime = 3. n % prime != 0, so 5 is, once again, yielded
  • Then, prime = 4, n % prime != 0, so 5 is, once again, yielded
  • Finally, prime = 5. n % prime == 0, so the loop is broken.

This process is then repeated with the next n

How to fix

You can remove the else clause from the if, and add it to the for loop. This means that n will be yielded only if the for loop was not broken, that is, only if n is not divisible by any number between 2 and n - 1.

So you can rewrite your function like this:

def prime_number_generator(num):
    for n in range (1, num):
        for prime in range (2, n):
            if (n % prime) == 0:
                break
        else:
            yield n

CodePudding user response:

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return False
        else:
            return True
        
def get_prime_numbers(n):
    for i in range(2, n):
        if is_prime(i):
            yield i
            
prime_numers = [prime for prime in get_prime_numbers(20)]
print(prime_numers)
  • Related