fastworld
fastworld

Reputation: 13

can I get the for loop to run the whole range?

This is my code. I am trying to find the prime numbers before or equal to the integer inputted. However, it seems that the loop stops when it sees an integer in the range that fits the requirements. Unfortunately, this is not I wanted it to do. I would like to make it run through all the tests in the range before making the judgement. Is this possible? If so, how do I do this? Thank you.

def getNumber(main):
    n = int(input())
return n


def isPrime(n):
    list=[2]
    if n > 1:
        for i in range(2, n+1):
            for a in range (2, n):
                if i*a != i and i%a != 0 and i%2 != 0:
                    list.append(i)
                    break
        return "\n".join(map(str, list))`

def main():
n = getNumber(main)
print(isPrime(n))

main()

Upvotes: 0

Views: 52

Answers (2)

vash_the_stampede
vash_the_stampede

Reputation: 4606

You can use a if/else block if your break is never executed by any item in the iterable the else statement will triggered. https://docs.python.org/3/tutorial/controlflow.html 4.4 demonstrates this accomplishing this almost exact task.

n = int(input('Enter number: '))
if n <= 1:
    print('No primes')
else:
    primes = []
    for i in range(2, n +1):
        for k in range(2, i):
            if not i % k:
                break
        else:
            primes.append(i)
    print(*primes)
    # Enter number: 50
    # 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Upvotes: 0

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

You've got your logic a bit wrong. Here's what your code is doing:

  1. Examine numbers in increasing order from 2 to the inputted n.
  2. For each number i, check if any number a between 2 and n divides i
  3. If a divides i, add i to the list, and then move to the next i

This isn't going to get you a prime number. In fact, I'm having trouble figuring out what it will give you, but a prime number probably isn't it. Look at this function instead, which will return all the prime numbers less than or equal to the given number - you can compare it to your code to figure out where you went wrong:

def getPrimesLessThanOrEqualTo(n):
    if n <= 1:                # Anything 1 or less has no primes less than it.
        return ""             #    So, return nothing.
    list = [2]                # 2 is the lowest prime number <= n
    for i in range(3, n+1):   # We start at 3 because there's no need to re-check 2
        for a in list:        # Instead of iterating through everything less than
                              #   i, we can just see if i is divisible by any of 
                              #   the primes we've already found
            if i % a == 0:    # If one of the primes we've found divides i evenly...
                break         # then go ahead and try the next i
            list.append(i)    # Now, if we got through that last bit without 
                              #   hitting the break statement, we add i to our list
    return "\n".join(list)    # Finally, return our list of primes <= i

If you wanted to be more efficient, you could even use range(3, n+1, 2) to count by twos - thus avoiding looking at even numbers at all.

Upvotes: 1

Related Questions