Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import time, sleep
- #my fastest version up to now
- def proper_divisors_sum1(n):
- pd_list = []
- for x in range(1, int(n**.5)+1):
- if n % x == 0:
- pd_list.append(x)
- pd_list.append(n//x)
- pd_list = list(set(pd_list))
- pd_list.sort()
- pd_list = pd_list[:-1]
- return sum(pd_list)
- #This uses several of Martin's ideas
- #It's now my fastest version
- def proper_divisors_sum2(n):
- pd = set((1,))
- for x in range(2, int(n**.5)+1):
- if n % x == 0:
- pd.update((x, n//x))
- return sum(list(pd))
- # This incorporates all of Martin's ideas
- # Seems that divmod() is a speed killer
- def proper_divisors_sum3(n):
- pd = set((1,))
- for x in range(2, int(n**.5)+1):
- factor, mod = divmod(n,x)
- if mod == 0:
- pd.update((x,factor))
- return sum(list(pd))
- n = 100000
- m = 100000 #number of loops
- sleep_secs = 1
- t0 = time()
- for i in range(m):
- sum_ = proper_divisors_sum1(n)
- t1 = time()
- print("time1 =", round((t1-t0), 3))
- sleep(sleep_secs)
- t0 = time()
- for i in range(m):
- sum_ = proper_divisors_sum2(n)
- t1 = time()
- print("time2 =", round((t1-t0), 3))
- sleep(sleep_secs)
- t0 = time()
- for i in range(m):
- sum_ = proper_divisors_sum3(n)
- t1 = time()
- print("time3 =", round((t1-t0), 3))
- """
- time1 = 8.999
- time2 = 7.924
- time3 = 13.959
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement