-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPE001.py
66 lines (55 loc) · 1.57 KB
/
PE001.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
import math
import time
lolim = 1
uplim = 1000
div1 = 3
div2 = 5
uplim -= 1
def method1(div1, div2, lolim, uplim):
def findLCM(arg1, arg2):
lower = min(arg1, arg2)
higher = max(arg1, arg2)
outp = higher
while outp % lower:
outp += higher
return outp
lcm = findLCM(div1, div2)
total = 0
while lolim % lcm and lolim < uplim:
if not lolim%div1 or not lolim%div2:
total += lolim
lolim += 1
while uplim % lcm and uplim > lolim:
if not uplim%div1 or not uplim%div2:
total += uplim
uplim -= 1
#print("lolim", lolim)
#print("uplim", uplim)
#print("total", total)
if uplim > lolim:
mult = 0
average = -1
for i in range(lolim, lolim+lcm):
if not i%div1 or not i%div2:
mult += 1
limSum = lolim + uplim
limDif = uplim - lolim
#Something is still not correct here!
total += math.ceil(limDif/lcm/2*mult) * limSum
# 33 * 1005 * 7 + 4*1005 -1005-1002
return total
#print("mult", mult, ". average", average, ". limSum", limSum, ". limDif", limDif)
#234168
# 33*1005*7 +4*1005
#1005/15 = 67
def method2(div1, div2, lolim, uplim):
gen = (i for i in range(lolim, uplim+1) if not i%3 or not i%5)
total = sum(gen)
return total
t1 = time.perf_counter_ns()
res1 = method1(div1, div2, lolim, uplim)
t2 = time.perf_counter_ns()
res2 = method2(div1, div2, lolim, uplim)
t3 = time.perf_counter_ns()
print("method1:", res1, "time:", t2-t1)
print("method2:", res2, "time:", t3-t1)