-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem005.py
More file actions
72 lines (55 loc) · 1.52 KB
/
Copy pathProblem005.py
File metadata and controls
72 lines (55 loc) · 1.52 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
62
63
64
65
66
67
68
69
70
71
72
#Project Euler, Problem 5
#The goal is to find the value of the smallest number that is evenly divisible by the positive integers 1 through 20.
def primefactors(n):
'''find the prime factors of a number, not including 1 and itself'''
pdivs = []
i = 3
if n == 1 and i <= n**(1/2):
pdivs = 1
return pdivs
if n == 2:
pdivs = [1,2]
return pdivs
while n != 1:
while n%2 == 0:
pdivs += [2]
n //= 2
while n%i == 0:
pdivs += [i]
n //= i
i += 2
if len(pdivs)==0:
pdivs = [n]
return pdivs
return pdivs
def sortncount(n):
'''pass an array through to be sorted and counted as a dictionary'''
dictr = {}
for element in n:
if element in dictr:
dictr[element] += 1
else:
dictr[element] = 1
return dictr
def highestfreq(n):
'''returns a dictionary consisting of keys that are in each dictionary, and their highest frequency'''
dictr = {}
for x in n:
for p,b in x.items():
#print(p,b)
if p not in dictr:
dictr[p] = 1
if dictr[p]<b:
dictr[p] = b
return dictr
def powerdict(n):
'''This raises each dict key to a power of its value'''
y = 1
for x in k:
y *= x**k[x]
return y
l = []
for x in range(1,21,1):
l += [sortncount(primefactors(x))]
k = highestfreq(l)
print(powerdict(highestfreq(l)))