Weird numbers: Difference between revisions

m
no edit summary
No edit summary
mNo edit summary
Line 2,416:
fastest-way-to-produce-a-list-of-all-divisors-of-a-number """
fctrs = [] # Empty list
if n % 6 == 0: # 6 is primitive semiperfect, equals 2 * 3
return "Semiperfect"
while n % 2 == 0: # Divides by 2 (adds 2, 2...) to prime fctrs
fctrs.append(2) # Append 2
n //= 2
t = 2 ** (len(fctrs) + 1) - 1 # Test
while n % 3 == 0: # Divides by 3 (adds 3, 3...) to prime fctrs
fctrs.append(3) # Append 3
Line 2,429 ⟶ 2,426:
for k in (i, i+2):
while n % k == 0:
while k <= t:
""" 2^k * p is never weird """
return "Semiperfect"
fctrs.append(k) # Append k
n //= k
Line 2,441 ⟶ 2,435:
def isweird(n): # Checks if n is weird
global primitivesp_nos # Retrieves list of primitive semiperfect nos
if n % 6 == 0: # 6 is primitive semiperfect, equals 2 * 3
prime_fctrs = get_prime_fctrs(n)
if prime_fctrs == "Semiperfect":
return 0
prime_fctrsx = get_prime_fctrs(n)
sum_fctrs = 1 # Sum of all factors based on formula
fctrs = set(prime_fctrsx) # Set of all fctrs
for i in fctrs:
sum_fctrs = sum_fctrs * (i ** (prime_fctrsx.count(i) + 1) - 1)//(i - 1)
difference = sum_fctrs - n - n # Difference between sum of fctrs and target n
if difference <= 0: # If difference < 0, n is deficient
returnif difference == 0:
primitivesp_nos.add(n) # n is primitive semiperfect
if difference == 0: # If difference = 0, n is perfect
return "Semiperfect"0
primitivesp_nos.add(n) # n is primitive semiperfect
for i in range(2, returnlen(x)): 0
for ij in rangecombinations(2x, len(prime_fctrs)i): # All combinations of prime fctrs
for j in combinations(prime_fctrs, i): # All combinations of prime fctrs
product = prod(j) # Product
if product not in primitivesp_nos: # Factor product added to set
Line 2,461 ⟶ 2,454:
else: # If factor is semiperfect, n cannot be weird
return 0
prime_fctrsx = 1 # Overwrites list, saves space
fctrs.add(1) # All numbers have 1 as a factor
fctrs = sorted(fctrs) # Sorts fctrs in order
Line 2,470 ⟶ 2,464:
https://discuss.python.org/t/a-python-program-for-
finding-weird-numbers/48654/6 """
prime_fctrs = 1 # Overwrites list, saves space
for d in fctrs:
prime_fctrsx |= prime_fctrsx << d
if not prime_fctrsx >> ns & 1: # Checks if combos set contains ns
returnisweird = 1
else:
primitivesp_nos.add(n)
returnisweird = 0
return isweird
main() # Start program