-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathlargest-prime-from-consecutive-prime-sum.py
More file actions
56 lines (47 loc) · 1.33 KB
/
largest-prime-from-consecutive-prime-sum.py
File metadata and controls
56 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Time: precompute: O(sqrt(r) * sqrt(r)) = O(r)
# runtime: O(logp), p = len(PRIMES)
# Space: O(sqrt(r))
import bisect
# precompute, number theory, binary search
def is_prime(n):
if (n <= 1) or (n != 2 and n%2 == 0):
return False
for i in xrange(3, n+1, 2):
if i*i > n:
break
if n%i == 0:
return False
return True
def linear_sieve_of_eratosthenes(n): # Time: O(n), Space: O(n)
primes = []
spf = [-1]*(n+1) # the smallest prime factor
for i in xrange(2, n+1):
if spf[i] == -1:
spf[i] = i
primes.append(i)
for p in primes:
if i*p > n or p > spf[i]:
break
spf[i*p] = p
return primes, spf
def precompute(n, sqrt_n):
result = [0]
primes, spf = linear_sieve_of_eratosthenes(sqrt_n)
total = 0
for p in primes:
total += p
if total > n:
break
if (total < len(spf) and spf[total] == total) or is_prime(total):
result.append(total)
return result
MAX_NUM = 5*10**5
SQRT_MAX_NUM = 2729 # by precomputation
PRIMES = precompute(MAX_NUM, SQRT_MAX_NUM)
class Solution(object):
def largestPrime(self, n):
"""
:type n: int
:rtype: int
"""
return PRIMES[bisect.bisect_right(PRIMES, n)-1]