Weird numbers: Difference between revisions
Content deleted Content added
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
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:
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)▼
return 0
sum_fctrs = 1 # Sum of all factors based on formula
fctrs = set(
for i in fctrs:
sum_fctrs = sum_fctrs * (i ** (
difference = sum_fctrs - n - n # Difference between sum of fctrs and target n
if difference <= 0: # If difference < 0, n is deficient
▲ primitivesp_nos.add(n) # n is primitive semiperfect
for i in range(2,
for
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
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:
if not
else:
primitivesp_nos.add(n)
return isweird
main() # Start program
|