Anonymous user
Square-free integers: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, split some compound statements.
m (→{{header|REXX}}: added/changed comments and whitespace, split some compound statements.) |
|||
Line 1,125:
=={{header|REXX}}==
<lang rexx>/*REXX program displays square─free numbers (integers > 1) up to a specified limit. */
numeric digits 20 /*be able to handle larger numbers. */
parse arg LO HI . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO=
if HI=='' | HI=="," then HI=
sw= linesize() - 1 /*use one less than a full line. */
count = 0 /*count of square─free numbers found. */
$= /*variable that holds a line of numbers*/
do j=LO to abs(HI)
if \isSquareFree(j) then iterate
count= count + 1
if HI<0 then iterate
if length($ || j)<sw then $= strip($ j)
else do; say $; $=j;
end /*j*/
if $\=='' then say $ /*are there any residuals to display ? */▼
TheNum= 'The number of square─free numbers between '
if HI<0 then say TheNum LO " and " abs(HI) ' (inclusive) is: ' count
▲if $\=='' then say $ /*are there any residuals to display ? */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isSquareFree: procedure; parse arg #; if #<1 then return 0 /*is the number too small?*/
odd=#//2 /*ODD=1 if # is odd, ODD=0 if even.*/
do k=2+odd to
if # // k**2 == 0 then return 0 /*Is # divisible by a square? */
end /*k*/ /* [↑] Yes? Then ^ square─free*/
return 1 /* [↑] // is REXX's ÷ remainder.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt: procedure; parse arg x;
do while q
end /*while q<=x*/
r= 0
do while q>1; q= q % 4; _=x - r - q; r= r % 2
if _>=0 then do; x= _; r= r + q; end
end /*while q>1*/
return r /*R is the integer square root of X. */</lang>
This REXX program makes use of '''linesize''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console); not all REXXes have this BIF.
|