Arithmetic numbers: Difference between revisions

Added Python example
m (→‎{{header|Pascal}}: added free pascal version)
(Added Python example)
Line 539:
The 1,000,000th arithmetic number is 1,228,663 up to which 905,043 are composite.
</pre>
 
=={{header|Python}}==
<lang python3>def factors(n: int):
f = set([1, n])
i = 2
while True:
j = n // i
if j < i:
break
if i * j == n:
f.add(i)
f.add(j)
i += 1
return f
 
arithmetic_count = 0
composite_count = 0
n = 1
while arithmetic_count <= 1000000:
f = factors(n)
if (sum(f)/len(f)).is_integer():
arithmetic_count += 1
if len(f) > 2:
composite_count += 1
if arithmetic_count <= 100:
print(f'{n:3d} ', end='')
if arithmetic_count % 10 == 0:
print()
if arithmetic_count in (1000, 10000, 100000, 1000000):
print(f'\n{arithmetic_count}th arithmetic number is {n}')
print(f'Number of composite numbers <= {n}: {composite_count}')
n += 1</lang>
Output:
<pre> 1 3 5 6 7 11 13 14 15 17
19 20 21 22 23 27 29 30 31 33
35 37 38 39 41 42 43 44 45 46
47 49 51 53 54 55 56 57 59 60
61 62 65 66 67 68 69 70 71 73
77 78 79 83 85 86 87 89 91 92
93 94 95 96 97 99 101 102 103 105
107 109 110 111 113 114 115 116 118 119
123 125 126 127 129 131 132 133 134 135
137 138 139 140 141 142 143 145 147 149
 
1000th arithmetic number is 1361
Number of composite numbers <= 1361: 782
 
10000th arithmetic number is 12953
Number of composite numbers <= 12953: 8458
 
100000th arithmetic number is 125587
Number of composite numbers <= 125587: 88219
 
1000000th arithmetic number is 1228663
Number of composite numbers <= 1228663: 905043</pre>
 
=={{header|Raku}}==
Anonymous user