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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

A number is an   attractive number   if the number of its prime factors (whether distinct or not) is also prime.

The number   20,   whose prime decomposition is   2 × 2 × 5,   is an   attractive number   because the number of its prime factors   (3)   is also prime.

Show sequence items up to   120.

Let's start with the solution:

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

Source code in the setl programming language

program attractive_numbers;
    numbers := [n in [2..120] | attractive(n)];
    printtab(numbers, 20, 3);

    proc printtab(list, cols, width);
        lines := [list(k..cols+k-1) : k in [1, cols+1..#list]];
        loop for line in lines do
            print(+/[lpad(str item, width+1) : item in line]);
        end loop;
    end proc;

    proc attractive(n);
        return #factorize(#factorize(n)) = 1;
    end proc;

    proc factorize(n);
        factors := [];
        d := 2;
        loop until d > n do
            loop while n mod d = 0 do
                factors with:= d;
                n div:= d;
            end loop;
            d +:= 1;
        end loop;
        return factors;
    end proc;
end program;

  

You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the Prolog programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Inverted index step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Two bullet roulette step by step in the 11l programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Objeck programming language