How to resolve the algorithm Anti-primes step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Anti-primes step by step in the PL/I programming language

Table of Contents

Problem Statement

The anti-primes (or highly composite numbers, sequence A002182 in the OEIS) are the natural numbers with more factors than any smaller than itself.

Generate and show here, the first twenty anti-primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Anti-primes step by step in the PL/I programming language

Source code in the pl/i programming language

antiprimes: procedure options(main);

    /* count the factors of a number */
    countFactors: procedure(n) returns(fixed);
        declare (n, i, count) fixed;
        if n<2 then return(1);
        count = 1;
        do i=1 to n/2;
            if mod(n,i) = 0 then count = count + 1;
        end;
        return(count);
    end countFactors;
    
    declare maxFactors fixed static init (0);
    declare seen fixed static init (0);
    declare n fixed;
    declare factors fixed;
    
    do n=1 repeat(n+1) while(seen < 20);
        factors = countFactors(n);
        if factors > maxFactors then do;
            put edit(n) (F(5));
            maxFactors = factors;
            seen = seen + 1;
            if mod(seen,15) = 0 then put skip;
        end;
    end;
end antiprimes;

  

You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Nim programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the C programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm S-expressions step by step in the Ceylon programming language