The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
def isPrime(n): if n == 2 or n == 3: return True if n < 2 or n % 2 == 0: return False if n < 9: return True if n % 3 == 0: return False r = int(n**0.5) f = 5 while f <= r: if n % f == 0: return False if n % (f + 2) == 0: return False f += 6 return True def problem10(n): ''' The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below n. ''' if n < 2: return 0 if n < 3: return 2 result = 2 for i in range(3, n, 2): if isPrime(i): result += i return result

0 nhận xét:
Đăng nhận xét