From 959d29728a3a9144cc0522fa6aed4def2d2983bd Mon Sep 17 00:00:00 2001 From: Satyamkumarai Date: Sat, 10 Oct 2020 15:54:17 +0530 Subject: [PATCH 1/2] Added soln to problem 50 --- project_euler/problem_50/sol1.py | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 project_euler/problem_50/sol1.py diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py new file mode 100644 index 000000000000..02161da500ea --- /dev/null +++ b/project_euler/problem_50/sol1.py @@ -0,0 +1,83 @@ + + + +"""fills the primes dict with prime numbers <=u \n + primes: Global Dictionary To fill \n + l: (optional) Starting value \n +""" +def prma(u:int,primes:dict, l:int=2)->None: + """ + >>> primes = {} + >>> prma(10,primes) + >>> primes + {2: 1, 3: 3, 5: 3, 7: 4} + """ + + if len(primes) <= 1: + primes[2] = 1 + primes[3] = 2 + + psrt = 2 + while l <= u: # while the number (l) is less than(or equal to ) the given + + if l > psrt**2: # if l exceeds the previous val of psrt + + psrt = (l**0.5) # psrt = sqrt(l) + + for i in primes: #for the primes currently in primes + + if i > psrt: # You need to only check till the sqrt(l) to see if it is a prime.. + primes[l] = len(primes)+1 #if it exceeds the value then it must be prime.. + break + + if l % i == 0: # if it is divisible.. it jumps to the next number for l i.e: l++ + break + l += 1 + + + + +def solution(MX = 1000000)->int: + """ + >>> solution() + 997651 + """ + primes= {} # using a dict To maintain order and o(1) searching.. + prma(MX,primes) #fill dict with primes < MX + primes_tuple = tuple(primes) + running_sum = 0 + count=0 + while running_sum=0: + running_sum1-=primes_tuple[ei] + ei-=1 + countL = ei-si+1 + # print(countL,running_sum1,primes_tuple[0:ei]) #debug + + + + # counting from right + ei = count-1 + running_sum2 = running_sum + while not running_sum2 in primes and si<=ei: + running_sum2-=primes_tuple[si] + si+=1 + countR = ei-si+1 + # print(countR,running_sum2,primes_tuple[0:count]) #debug + + return running_sum1 if countL>countR else running_sum2 + +if __name__ == "__main__": + print(solution()) + From 60dfac737d6bbcd9a38efd0699e0c1f913400796 Mon Sep 17 00:00:00 2001 From: Satyamkumarai Date: Sat, 10 Oct 2020 16:03:00 +0530 Subject: [PATCH 2/2] minor fixes --- project_euler/problem_50/sol1.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_50/sol1.py b/project_euler/problem_50/sol1.py index 02161da500ea..353661ca0267 100644 --- a/project_euler/problem_50/sol1.py +++ b/project_euler/problem_50/sol1.py @@ -1,10 +1,9 @@ -"""fills the primes dict with prime numbers <=u \n - primes: Global Dictionary To fill \n - l: (optional) Starting value \n -""" +#fills the primes dict with prime numbers <=u +# primes: Global Dictionary To fill +# l: (optional) Starting value def prma(u:int,primes:dict, l:int=2)->None: """ >>> primes = {} @@ -37,10 +36,12 @@ def prma(u:int,primes:dict, l:int=2)->None: -def solution(MX = 1000000)->int: +def solution(MX:int = 1000000)->int: """ >>> solution() 997651 + >>> solution(1000) #lessthan 1000 + 953 """ primes= {} # using a dict To maintain order and o(1) searching.. prma(MX,primes) #fill dict with primes < MX