简体   繁体   中英

Finding an interval of 100 consecutive composite numbers with Python

I'm trying to write a program that can determine an integer A as such as there are no prime numbers between A and A+100 ... Unfortunately, with my mediocre Python skills, this is all I managed to write:

for A in range (1,1000000):
    if is_prime(n)==False in range (A,A+3):
        print(A)

As you can see, I first tried to get it working with an interval of only 2 consecutive composite numbers. I also used a (working) function "is_prime" that determines if an integer is prime or not.

You can start yelling at me for my incompetence !

I recommend a sieve-style operation for performance reasons. Create a list of X numbers, mark all primes, then look for an unbroken sequence of composite numbers.

You're in the right ballpark. Just gotta finish the rest of the list comprehension.

for A in range (1,1000000):
    if all(is_prime(n)==False for n in range (A,A+3)):
        print(A)

style nitpick: not is_prime(n) would be preferable to is_prime(n) == False .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM