mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@ -4,22 +4,11 @@ The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
Find the sum of all the primes below two million.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from math import sqrt
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range # Python 3
|
||||
|
||||
|
||||
def is_prime(n):
|
||||
for i in xrange(2, int(sqrt(n)) + 1):
|
||||
for i in range(2, int(sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
return False
|
||||
|
||||
@ -32,7 +21,7 @@ def sum_of_primes(n):
|
||||
else:
|
||||
return 0
|
||||
|
||||
for i in xrange(3, n, 2):
|
||||
for i in range(3, n, 2):
|
||||
if is_prime(i):
|
||||
sumOfPrimes += i
|
||||
|
||||
@ -41,7 +30,7 @@ def sum_of_primes(n):
|
||||
|
||||
def solution(n):
|
||||
"""Returns the sum of all the primes below n.
|
||||
|
||||
|
||||
# The code below has been commented due to slow execution affecting Travis.
|
||||
# >>> solution(2000000)
|
||||
# 142913828922
|
||||
@ -58,4 +47,4 @@ def solution(n):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(raw_input().strip())))
|
||||
print(solution(int(input().strip())))
|
||||
|
@ -4,15 +4,9 @@ The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
Find the sum of all the primes below two million.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
import math
|
||||
from itertools import takewhile
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
|
||||
def primeCheck(number):
|
||||
if number % 2 == 0 and number > 2:
|
||||
@ -30,7 +24,7 @@ def prime_generator():
|
||||
|
||||
def solution(n):
|
||||
"""Returns the sum of all the primes below n.
|
||||
|
||||
|
||||
# The code below has been commented due to slow execution affecting Travis.
|
||||
# >>> solution(2000000)
|
||||
# 142913828922
|
||||
@ -47,4 +41,4 @@ def solution(n):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(raw_input().strip())))
|
||||
print(solution(int(input().strip())))
|
||||
|
Reference in New Issue
Block a user