Factor-perfect numbers: Difference between revisions

m
Python example
m (spacing)
m (Python example)
Line 104:
OEIS A163272:
0, 1, 48, 1280, 2496, 28672, 29808, 454656, 2342912,
</pre>
 
 
=={{header|Python}}==
<syntaxheader lang=python>''' Rosetta Code task Factor-perfect_numbers '''
 
 
from sympy import divisors
 
 
def more_multiples(to_seq, from_seq):
''' Uses the first definition and recursion to generate the sequences '''
onemores = [to_seq + [i]
for i in from_seq if i > to_seq[-1] and i % to_seq[-1] == 0]
if len(onemores) == 0:
return []
for i in range(len(onemores)):
for arr in more_multiples(onemores[i], from_seq):
onemores.append(arr)
return onemores
 
 
listing = sorted(more_multiples([1], divisors(48)[1:-1]) + [[1, 48]])
print('48 sequences using first definition:')
for j, seq in enumerate(listing):
print(f'{str(seq):20}', end='\n' if (j + 1) % 4 == 0 else '')
 
 
# Derive second definition's sequences
print('\n48 sequences using second definition:')
for k, seq in enumerate(listing):
if seq[-1] != 48:
seq.append(48)
seq2 = [seq[i] // seq[i - 1] for i in range(1, len(seq))]
print(f'{str(seq2):20}', end='\n' if (k + 1) % 4 == 0 else '')
 
 
def count_multiple_sequences(number):
''' Counts using the first definition, plus one extra for [1, n] '''
return len(more_multiples([1], divisors(number)[1:-1])) + 1
 
 
print("\nOEIS A163272: ", end='')
for num in range(500_000):
if num == 0 or count_multiple_sequences(num) == num:
print(num, end=', ')
</syntaxhighlight>{{out}}
<pre>
48 sequences using first definition:
[1, 2] [1, 2, 4] [1, 2, 4, 8] [1, 2, 4, 8, 16]
[1, 2, 4, 8, 24] [1, 2, 4, 12] [1, 2, 4, 12, 24] [1, 2, 4, 16]
[1, 2, 4, 24] [1, 2, 6] [1, 2, 6, 12] [1, 2, 6, 12, 24]
[1, 2, 6, 24] [1, 2, 8] [1, 2, 8, 16] [1, 2, 8, 24]
[1, 2, 12] [1, 2, 12, 24] [1, 2, 16] [1, 2, 24]
[1, 3] [1, 3, 6] [1, 3, 6, 12] [1, 3, 6, 12, 24]
[1, 3, 6, 24] [1, 3, 12] [1, 3, 12, 24] [1, 3, 24]
[1, 4] [1, 4, 8] [1, 4, 8, 16] [1, 4, 8, 24]
[1, 4, 12] [1, 4, 12, 24] [1, 4, 16] [1, 4, 24]
[1, 6] [1, 6, 12] [1, 6, 12, 24] [1, 6, 24]
[1, 8] [1, 8, 16] [1, 8, 24] [1, 12]
[1, 12, 24] [1, 16] [1, 24] [1, 48]
 
48 sequences using second definition:
[2, 24] [2, 2, 12] [2, 2, 2, 6] [2, 2, 2, 2, 3]
[2, 2, 2, 3, 2] [2, 2, 3, 4] [2, 2, 3, 2, 2] [2, 2, 4, 3]
[2, 2, 6, 2] [2, 3, 8] [2, 3, 2, 4] [2, 3, 2, 2, 2]
[2, 3, 4, 2] [2, 4, 6] [2, 4, 2, 3] [2, 4, 3, 2]
[2, 6, 4] [2, 6, 2, 2] [2, 8, 3] [2, 12, 2]
[3, 16] [3, 2, 8] [3, 2, 2, 4] [3, 2, 2, 2, 2]
[3, 2, 4, 2] [3, 4, 4] [3, 4, 2, 2] [3, 8, 2]
[4, 12] [4, 2, 6] [4, 2, 2, 3] [4, 2, 3, 2]
[4, 3, 4] [4, 3, 2, 2] [4, 4, 3] [4, 6, 2]
[6, 8] [6, 2, 4] [6, 2, 2, 2] [6, 4, 2]
[8, 6] [8, 2, 3] [8, 3, 2] [12, 4]
[12, 2, 2] [16, 3] [24, 2] [48]
 
OEIS A163272: 0, 1, 48, 1280, 2496, 28672, 29808, 454656,
</pre>
4,102

edits