Definition
A positive integer n is an arithmetic number if the average of its positive divisors is also an integer.

Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5.

Example
30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer.

Task
Calculate and show here:

1. The first 100 arithmetic numbers.

2. The xth arithmetic number where x = 1,000 and x = 10,000.

3. How many of the first x arithmetic numbers are composite.

Note that, technically, the arithmetic number 1 is neither prime nor composite.

Stretch
Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000.

References
 Wikipedia: Arithmetic number
 OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer




use strict;
use warnings;
use feature 'say';
 
use List::Util <max sum>;
use ntheory <is_prime divisors>;
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
 
sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r }
sub table { my $t = 10 * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
 
my @A = 0;
for my $n (1..2E6)  {
    my @div = divisors $n;
    push @A, $n if 0 == sum(@div) % @div;
}
 
say "The first @{[num2en 100]} arithmetic numbers:";
say table @A[1..100];
 
for my $x (1E3, 1E4, 1E5, 1E6) {
    say "\nThe @{[num2en_ordinal $x]}: " . comma($A[$x]) .
        "\nComposite arithmetic numbers ≤ @{[comma $A[$x]]}: " . comma -1 + grep { not is_prime($_) } @A[1..$x];
}