-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.py
More file actions
62 lines (51 loc) · 1.27 KB
/
primes.py
File metadata and controls
62 lines (51 loc) · 1.27 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
57
58
59
60
61
#!/usr/bin/env python3.2
from math import sqrt, ceil
# factor product of two primes
def factor_2p_recursive(product, a=None, b=None):
if a==None:
s = sqrt(product)
return factor_2p_recursive(product, int(s), ceil(s))
delta = product - a*b
if int(delta)==0:
return (int(a), int(b))
if delta > 0:
b = ceil(b + delta/a)
else:
a = int(a + delta/b)
return factor_2p_recursive(product, a, b)
def ceil_int_divide(num, den):
idiv = num//den
if (idiv * den) == num:
return idiv
else:
return idiv+1
def factor_2p(product, a=None, b=None):
if a==None:
s = sqrt(product)
a = int(s)
b = ceil(s)
delta = product - a*b
while int(delta) != 0:
print("%d %d %d" % (a,b,delta))
if delta > 0:
b = b + ceil_int_divide(delta,a)
else:
a = a + delta//b
delta = product - a*b
return (int(a), int(b))
def primes_under(N):
S = set(range(2,N+1))
i = 2
while i <= N:
if not i in S:
i += 1
else:
j = i*2
while j <= N:
if j in S:
S.remove(j)
j += i
i += 1
L = list(S)
L.sort()
return L