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




with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
 
procedure Main is
   procedure divisor_count_and_sum
     (n : Positive; divisor_count : out Natural; divisor_sum : out Natural)
   is
      I : Positive := 1;
      J : Natural;
   begin
      divisor_count := 0;
      divisor_sum   := 0;
      loop
         J := n / I;
         exit when J < I;
         if I * J = n then
            divisor_sum   := divisor_sum + I;
            divisor_count := divisor_count + 1;
            if I /= J then
               divisor_sum   := divisor_sum + J;
               divisor_count := divisor_count + 1;
            end if;
         end if;
         I := I + 1;
      end loop;
   end divisor_count_and_sum;
 
   arithmetic_count : Natural  := 0;
   composite_count  : Natural  := 0;
   div_count        : Natural;
   div_sum          : Natural;
   mean             : Natural;
   n                : Positive := 1;
begin
 
   while arithmetic_count <= 1_000_000 loop
      divisor_count_and_sum (n, div_count, div_sum);
      mean := div_sum / div_count;
      if mean * div_count = div_sum then
         arithmetic_count := arithmetic_count + 1;
         if div_count > 2 then
            composite_count := composite_count + 1;
         end if;
         if arithmetic_count <= 100 then
            Put (Item => n, Width => 4);
            if arithmetic_count mod 10 = 0 then
               New_Line;
            end if;
         end if;
         if arithmetic_count = 1_000 or else arithmetic_count = 10_000
           or else arithmetic_count = 100_000
           or else arithmetic_count = 1_000_000
         then
            New_Line;
            Put (Item => arithmetic_count, Width => 1);
            Put_Line ("th arithmetic number is" & n'Image);
            Put_Line
              ("Number of composite arithmetic numbers <=" & n'Image & ":" &
               composite_count'Image);
         end if;
      end if;
      n := n + 1;
   end loop;
end Main;