-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe12.py
113 lines (97 loc) · 2.6 KB
/
e12.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from pdb import set_trace as st
def dirty_prime_gen(seed1, seed2):
x = (seed1 + seed2) // 6
if x % 2 == 0:
k = x // 2
else:
k = x // 2 + 1
yield 6*k + 1
k += 1
while True:
yield 6*k - 1
yield 6*k + 1
k += 1
def trngl_num_gen():
n = 1
while True:
yield (n*(n+1))//2
n += 1
def num_is_trngl(num):
d = (8*num + 1)**0.5
if int(d) == d:
return (d - 1)//2
return False
def is_prime(test, known):
for x in known:
if x > test:
return False
if test % x == 0:
return False
return True
def prime_list(lim, known=None):
if known == None:
known = [2, 3, 5, 7]
dp = dirty_prime_gen(known[-2], known[-1])
while len(known) < lim:
sp = next(dp)
if is_prime(sp, known):
known.append(sp)
return known
def prime_factorize(num, known_primes):
pf = dict()
for p in known_primes:
if p > num:
break
n = num
times = int()
while n % p == 0:
n /= p
times += 1
pf[p] = times
return {k:v for k, v in pf.items() if v != 0}
def the_func(dct):
# (12, 504, {2: 3, 3: 2, 7: 1}),
# 504 = 2*2*2 * 3*3 * 7
# 504 = product of 6 distinct prime numbers - a, b, c, d, e, f
# ans = a * b * c * d**2 * e**2 * f**6
chars = "abcdefghijklmnopqrstuvwxyz"
grp_size = sum(dct.values())
pph = list(chars[:grp_size]) # primeNum place holder
i = int()
raw_eqn = list()
for pwr, reps in dct.items():
for _ in range(reps):
cpph = pph[i]
frag = "%s**%d" % (cpph, pwr-1)
raw_eqn.append(frag)
i += 1
code = "def x(%s): return %s" % (", ".join(pph), "*".join(raw_eqn))
st()
return grp_size, eval(code)
def find_least_f_num(min, max):
ans = list()
knp = prime_list(max) # misuse but okay
for num in range(min, max+1):
fzn = prime_factorize(num, knp)
nf = sum(fzn.values())
ans.append((nf, num, fzn))
return sorted(ans, key=lambda x: x[0])
return ans
from pprint import pprint as pp
research = find_least_f_num(501, 600)
pp(research)
exit()
# (512, 576, 540, 567, 600)
from itertools import product
def get_trngl_num_with_n_factors(n):
knp = prime_list(n) # misuse but ok
pf = prime_factorize(n, knp)
grp_size, functor = the_func(pf)
knp = knp[:grp_size*2]
for group in product(knp, grp_size):
x = functor(group)
if num_is_trngl(x):
print(x)
break
return
#get_trngl_num_with_n_factors(504)