How to resolve the algorithm Attractive numbers step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Attractive numbers step by step in the PL/I 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 PL/I programming language

Source code in the pl/i programming language

attractive: procedure options(main);
    %replace MAX by 120;
    declare prime(1:MAX) bit(1);
    
    sieve: procedure;
        declare (i, j, sqm) fixed;
        prime(1) = 0;
        do i=2 to MAX; prime(i) = '1'b; end;
        
        sqm = sqrt(MAX);
        do i=2 to sqm;
            if prime(i) then do j=i*2 to MAX by i;
                prime(j) = '0'b;
            end;
        end;
    end sieve;
    
    factors: procedure(nn) returns(fixed);
        declare (f, i, n, nn) fixed;
        n = nn;
        f = 0;
        do i=2 to n;
            if prime(i) then do while(mod(n,i) = 0);
                f = f+1;
                n = n/i;
            end;
        end;
        return(f);
    end factors;
    
    attractive: procedure(n) returns(bit(1));
        declare n fixed;
        return(prime(factors(n)));
    end attractive;
    
    declare (i, col) fixed;
    i = 0;
    col = 0;
    call sieve();
    do i=2 to MAX;
        if attractive(i) then do;
            put edit(i) (F(4));
            col = col + 1;
            if mod(col,18) = 0 then put skip;
        end;
    end;
end attractive;

  

You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Python programming language
You may also check:How to resolve the algorithm Rep-string step by step in the BaCon programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the TXR programming language
You may also check:How to resolve the algorithm Successive prime differences step by step in the Factor programming language
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the Euphoria programming language