0

I am trying to write some code for project euler problem 10, in which you are required to find the sum of all the prime numbers less than 2 million. Now the code is taking too long to execute. I would like to know how can I improve my code as it did work for prime numbers less than 100.

# =================== isPrime Algorithm ====================
# I created this program to check whether a number is prime or not

def isPrime(n):
    # Prime number algorithm
    if n == 2:
        return True
    elif n == 3:
        return True
    else:
        for x in range(3, n, 2):
            if n % x == 0:
                return False
            else:
                continue
    return True

def main():
    # Collects the prime numbers between 1 and 100
    prime_arr = [] # I used this array just to test I could have easily printed the num in the for loop below

    for num in range(1, 101, 2):
        if isPrime(num) == True:
            prime_arr.append(num)
        else:
            continue

    print(prime_arr)

# Call the main function
main()
4
Contributors
5
Replies
34
Views
3 Months
Discussion Span
Last Post by filtre
0
import re 
def is_prime(num): return not re.match(r"^1?$|^(11+?)\1+$", "1" * num)
def pri(num): return is_prime(num)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.