I'm going to make a list of prime numbers

Asked 2 years ago, Updated 2 years ago, 76 views

prime = [1,2,3,5,7]
factors = []

for i in range(11,10000000,2):
    for j in range(2,i,2):
        if i % j ==0:
            factors.append(j)
            if factors == []:
                prime.append(i)
                print(i)
            else:
                break
        else:
            continue

I'm trying to put the prime numbers in ascending order, but it's executed The print(i) doesn't come out, so which part is the problem?

python primes

2022-09-20 20:51

1 Answers

prime = [1,2,3,5,7]
factors = []

for i in range(11,100):
    for j in range(2,i):
        if i % j == 0:
            factors.append(j)
            break

    if factors == []:
        print(i)
        prime.append(i)
    else:
        factors = []

print(prime)

It's a number that's not completely divided except for 1 and yourself. The author seems to want to put the value in factors when the remaining value is 0, and then make a code to determine the decimal number by judging whether it is an empty space.

I think it's right to check it after the loop is completed (I think you have a question here) And the factors, for a moment. It's like a temporary storage, so you have to empty it after the operation so there won't be any problem with the conditional statement.


2022-09-20 20:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.