How to resolve the algorithm Arithmetic numbers step by step in the SETL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic numbers step by step in the SETL programming language

Table of Contents

Problem Statement

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. 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. 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. Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic numbers step by step in the SETL programming language

Source code in the setl programming language

program arithmetic_numbers;
    [divsum, divcount] := calcdivsums(130000);

    print("First 100 arithmetic numbers:");

    loop for nth in [1..100000] do
        loop until divsum(num) mod divcount(num) = 0 do num +:= 1; end loop;
        comp +:= if num>1 and divsum(num) /= num+1 then 1 else 0 end if;

        if nth <= 100 then
            putchar(rpad(str num, 5));
            if nth mod 10 = 0 then print(); end if;
        end if;

        if nth in [1000, 10000, 100000] then
            print("The " + nth + "th arithmetic number is " + num + ".");
            print("Of the first " + nth + " arithmetic numbers, " +
                  comp + " are composite.");
        end if;
    end loop;

    proc calcdivsums(m);
        sums := [];
        counts := [];
        loop for d in [1..m] do
            loop for n in [d, d*2..m] do
                sums(n) +:= d;
                counts(n) +:= 1;
            end loop;
        end loop;
        return [sums, counts];
    end proc;
end program;

  

You may also check:How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the Ada programming language
You may also check:How to resolve the algorithm Dot product step by step in the Picat programming language
You may also check:How to resolve the algorithm Prime triangle step by step in the jq programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Ada programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the Icon and Unicon programming language