Project Euler #10 (Python)

前端 未结 9 1310
情深已故
情深已故 2021-01-22 07:27

Why is my algorithm for finding the sum of all prime numbers below 2 million so slow? I\'m a fairly beginner programmer and this is what I came up with for finding the solution:

9条回答
  •  广开言路
    2021-01-22 08:06

    sum = 2
    
    def isPrime(n):
        if n % 2 == 0: return False
        for i in range(3, int(n**0.5)+1, 2):
            if n % i == 0: return False
        return True
    if __name__ == "__main__":
        n = 1
        while n < 2000000:
            n += 2
            if isPrime(n):sum += n
    print sum
    

提交回复
热议问题