Task: Arithmetic numbers

C: #include <stdio.h>

void divisor_count_and_sum(unsigned int n, unsigned int* pcount,
                           unsigned int* psum) {
    unsigned int divisor_count = 1;
    unsigned int divisor_sum = 1;
    unsigned int power = 2;
    for (; (n & 1) == 0; power <<= 1, n >>= 1) {
        ++divisor_count;
        divisor_sum += power;
    }
    for (unsigned int p = 3; p * p <= n; p += 2) {
        unsigned int count = 1, sum = 1;
        for (power = p; n % p == 0; power *= p, n /= p) {
            ++count;
            sum += power;
        }
        divisor_count *= count;
        divisor_sum *= sum;
    }
    if (n > 1) {
        divisor_count *= 2;
        divisor_sum *= n + 1;
    }
    *pcount = divisor_count;
    *psum = divisor_sum;
}

int main() {
    unsigned int arithmetic_count = 0;
    unsigned int composite_count = 0;

    for (unsigned int n = 1; arithmetic_count <= 1000000; ++n) {
        unsigned int divisor_count;
        unsigned int divisor_sum;
        divisor_count_and_sum(n, &divisor_count, &divisor_sum);
        if (divisor_sum % divisor_count != 0)
            continue;
        ++arithmetic_count;
        if (divisor_count > 2)
            ++composite_count;
        if (arithmetic_count <= 100) {
            printf("%3u ", n);
            if (arithmetic_count % 10 == 0)
                printf("\n");
        }
        if (arithmetic_count == 1000 || arithmetic_count == 10000 ||
            arithmetic_count == 100000 || arithmetic_count == 1000000) {
            printf("\n%uth arithmetic number is %u\n", arithmetic_count, n);
            printf("Number of composite arithmetic numbers <= %u: %u\n", n,
                   composite_count);
        }
    }
    return 0;
}











Ada: 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;