Home > Mobile >  Can anyone help me find out where I made a mistake?
Can anyone help me find out where I made a mistake?

Time:11-24

I am trying to figure out how to print a requested amount of prime numbers but I am having problems.

I can't describe what I did so I'll just paste my code so far:

requested_primes = 3 #just for simplicity, i am going to request an input for how many prime integers they  #want printed
found_primes = 0
examined_number = 2 #starting point for counting

while found_primes != requested_primes: #self-evident i hope
    for i in range(2, examined_number): #trying to check if it's a prime number or not with this one.
        if examined_number % i == 0: #if it can be neatly divided, it's not a prime and the search has to go    #on
            examined_number  = 1
        else: #if no neat divisions can occur then i got a prime number and i can print it before going back #and searching for another one, until found_primes == requested_primes
            print(examined_number, end=' ')
            examined_number  = 1
            found_primes  = 1

Nothing comes up in the terminal.

CodePudding user response:

Your for loop never runs due to i having the same starting value as examined_number (both are 2).

Try using for in in range(1, examined_number) and that should get your loop to execute - you can verify it is running by adding a few print statements

CodePudding user response:

There are 2 problems, both of which can potentially cause you to have infinite loop. First of all, you are starting with range(2, 2) - it has 0 elements, meaning your for loop will not do anything and you will stay in the while loop infintiely. Start from 3 and just assume 2 is prime.

Fixing this will still not make the code work, because you are increasing examined_number and found_primes every time number does not divide evenly. Which means your found_primes very quickly goes past requested_primes and you will stay in the while loop forever. In order to fix that, you can use that fact that for loop supports else clause that only happens if the loop finished normally - so we break if a number has divided evenly. If we didn't break, we run the else clause.

requested_primes = 10 #just for simplicity, i am going to request an input for how many prime integers they  #want printed
found_primes = 1
print(2, end=' ')
examined_number = 3 #starting point for counting

while found_primes != requested_primes: #self-evident i hope
    for i in range(2, examined_number): #trying to check if it's a prime number or not with this one.
        if examined_number % i == 0: #if it can be neatly divided, it's not a prime and the search has to go    #on
            examined_number  = 1
            break
    else: #if no neat divisions can occur then i got a prime number and i can print it before going back #and searching for another one, until found_primes == requested_primes
        print(examined_number, end=' ')
        examined_number  = 1
        found_primes  = 1
  • Related