How to resolve the algorithm Primality by trial division step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Primality by trial division step by step in the PL/I programming language

Table of Contents

Problem Statement

Write a boolean function that tells whether a given integer is prime.

Remember that   1   and all non-positive numbers are not prime. Use trial division. Even numbers greater than   2   may be eliminated right away. A loop from   3   to   √ n    will suffice,   but other loops are allowed.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Primality by trial division step by step in the PL/I programming language

Source code in the pl/i programming language

is_prime: procedure (n) returns (bit(1));
   declare n fixed (15);
   declare i fixed (10);

   if n < 2 then return ('0'b);
   if n = 2 then return ('1'b);
   if mod(n, 2) = 0 then return ('0'b);

   do i = 3 to sqrt(n) by 2;
      if mod(n, i) = 0 then return ('0'b);
   end;
   return ('1'b);
end is_prime;

  

You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the Tcl programming language
You may also check:How to resolve the algorithm Penney's game step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the E programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Perl programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the PowerShell programming language