how to check among big prime numbers for condition

115 Views Asked by At

Out of prime numbers less than 201920190 how to find those that dont have digit '7' in their str representation? I need to count the number(count) of such prime numbers that have no '7' in them.

Std sieve implementation is too slow, it just stuck computing..

def SieveOfEratosthenes(num):
    prime = [True for i in range(num+1)]
# boolean array
    p = 2
    while (p * p <= num):
 
        # If prime[p] is not
        # changed, then it is a prime
        if (prime[p] == True):
 
            # Updating all multiples of p
            for i in range(p * p, num+1, p):
                prime[i] = False
        p += 1
 
    # Print all prime numbers
    for p in range(2, num+1):
        if prime[p]:
                  #here added additional check to only print those that have no digit 7
                  if '7' not in str(p):
                     print(p)
 
 
# Driver code
if __name__ == '__main__':
    num = 201920190
    print("Following are the prime numbers smaller"),
    print("than or equal to", num)
    SieveOfEratosthenes(num)

How to do it better?

1

There are 1 best solutions below

6
GB supports the mod strike On

Perhaps cheating, but if your priority is simply to get a number you could just download a list of all the primes up to 201920190, e.g. from https://primes.utm.edu/lists/small/millions/ (there are about 12 million of them), and then count through that list.

It's not nearly as satisfying as getting your own sieve code to work, but it's pretty efficient!