You could try something like this, based on this function divisors(n)
that seems to be the fastest one to calculate factors of a number, I proved it with n=600851475143
and it took 0.2790100000020175 seconds
to run the script 1000000
times, while others took more than 16 seconds
with the same number:
def divisors(n):
# get factors and their counts
factors = {}
nn = n
i = 2
while i*i <= nn:
while nn % i == 0:
factors[i] = factors.get(i, 0) + 1
nn //= i
i += 1
if nn > 1:
factors[nn] = factors.get(nn, 0) + 1
primes = list(factors.keys())
# generates factors from primes[k:] subset
def generate(k):
if k == len(primes):
yield 1
else:
rest = generate(k+1)
prime = primes[k]
for factor in rest:
prime_to_i = 1
# prime_to_i iterates prime**i values, i being all possible exponents
for _ in range(factors[prime] + 1):
yield factor * prime_to_i
prime_to_i *= prime
# in python3, `yield from generate(0)` would also work
for factor in generate(0):
yield factor
lis=[15,9,220,284]
for x, y in zip(lis, lis[1:]):
print('with ',x,' and ', y,': ' )
if (sum(list(divisors(x))[:-1])==y) and (sum(list(divisors(y))[:-1])==x):
print('1\n')
else:
print('0\n')
Output:
with 15 and 9 :
0
with 9 and 220 :
0
with 220 and 284 :
1
Or you can save it in a dict maybe to be more practical:
lis=[15,9,220,284]
dct={}
for x, y in zip(lis, lis[1:]):
key=str(x)+'-'+str(y)
if (sum(list(divisors(x))[:-1])==y) and (sum(list(divisors(y))[:-1])==x):
dct.update({key:1})
else:
dct.update({key:0})
print(dct)
Output:
{'15-9': 0, '9-220': 0, '220-284': 1}