Square-free integers: Difference between revisions
Content added Content deleted
Catskill549 (talk | contribs) |
m (→{{header|REXX}}: changed whitespace and comments, changed some variable names, used the correct glyph for a logical not.) |
||
Line 1,618: | Line 1,618: | ||
if HI=='' | HI=="," then HI= 145 /* " " " " " " */ |
if HI=='' | HI=="," then HI= 145 /* " " " " " " */ |
||
sw= linesize() - 1 /*use one less than a full line. */ |
sw= linesize() - 1 /*use one less than a full line. */ |
||
# = 0 /*count of square─free numbers found. */ |
|||
$= /*variable that holds a line of numbers*/ |
$= /*variable that holds a line of numbers*/ |
||
do j=LO to abs(HI) /*process all integers between LO & HI.*/ |
do j=LO to abs(HI) /*process all integers between LO & HI.*/ |
||
if \isSquareFree(j) then iterate /*Not square─free? Then skip this #. */ |
if \isSquareFree(j) then iterate /*Not square─free? Then skip this #. */ |
||
#= # + 1 /*bump the count of square─free numbers*/ |
|||
if HI<0 then iterate /*Only counting 'em? Then look for more*/ |
if HI<0 then iterate /*Only counting 'em? Then look for more*/ |
||
if length($ || j)<sw then $= strip($ j) /*append the number to the output list.*/ |
if length($ || j)<sw then $= strip($ j) /*append the number to the output list.*/ |
||
Line 1,629: | Line 1,629: | ||
if $\=='' then say $ /*are there any residuals to display ? */ |
if $\=='' then say $ /*are there any residuals to display ? */ |
||
@theNum= 'The number of square─free numbers between ' |
|||
if HI<0 then say |
if HI<0 then say @theNum LO " and " abs(HI) ' (inclusive) is: ' # |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
isSquareFree: procedure; parse arg |
isSquareFree: procedure; parse arg x; if x<1 then return 0 /*is the number too small?*/ |
||
odd= |
odd= x//2 /*ODD=1 if X is odd, ODD=0 if even.*/ |
||
do k=2+odd to iSqrt( |
do k=2+odd to iSqrt(x) by 1+odd /*use all numbers, or just odds*/ |
||
if |
if x // k**2 == 0 then return 0 /*Is X divisible by a square?*/ |
||
end /*k*/ /* [↑] Yes? Then |
end /*k*/ /* [↑] Yes? Then ¬ square─free*/ |
||
return 1 /* [↑] // is REXX's ÷ remainder.*/ |
return 1 /* [↑] // is REXX's ÷ remainder.*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
iSqrt: procedure; parse arg x; q= 1 |
iSqrt: procedure; parse arg x; q= 1; do while q<=x; q= q * 4 |
||
end /*while q<=x*/ |
|||
end /*while q<=x*/ |
|||
r= 0 |
r= 0 |
||
do while q>1; q= q % 4; _=x - r - q; r= r % 2 |
do while q>1; q= q % 4; _= x - r - q; r= r % 2 |
||
if _>=0 then do; x= _; r= r + q; end |
if _>=0 then do; x= _; r= r + q; end |
||
end /*while q>1*/ |
end /*while q>1*/ |
||
return r /*R is the integer square root of X. */</lang> |
return r /*R is the integer square root of X. */</lang> |