Amicable pairs: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add MAD |
m →{{header|REXX}}: added/changed whitespace and comments, used a template for the output sections. |
||
Line 3,777:
=={{header|REXX}}==
===version 1, with factoring===
<lang rexx>
/*REXX*/
Call time 'R'
Do x=1 To 20000
pd=proper_divisors(x)
Line 3,859 ⟶ 3,862:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sigma: procedure; parse arg x; od= x // 2
s= 1 /*set initial sigma sum to unity. ___*/
if
{{out|output|text= when using the default input:}}
<pre>
Line 3,887 ⟶ 3,892:
parse arg H .; if H=='' | H=="," then H=20000 /*get optional arguments (high limit).*/
w=length(H) ; low=220 /*W: used for columnar output alignment*/
x= 220 34765731 6232 87633 284 12285 10856 36939357 6368 5684679 /*S minimums.
do i=0 for 10; $.i= word(x, i + 1);
#=0 /*number of amicable pairs found so far*/▼
@.= /* [↑] LOW is lowest amicable number. */
do k=low for H-low /*generate sigma sums for a range of #s*/
parse var k '' -1 D /*obtain last decimal digit of K. */
if k<$.D then iterate /*if no need to compute, then skip it. */
_= sigma(k)
@.k=
if k==@._ then do /*is it a possible amicable number ? */
if _==k then iterate /*Is it a perfect number? Then skip it*/
#= # +
say right(_, w)
end
end /*k*/ /* [↑] process a range of integers. */
Line 3,906 ⟶ 3,911:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sigma: procedure; parse arg x; od= x //
s=
do j=2+od by 1+od while j*j<x /*divide by all integers up to the √ x */
if x//j==0 then s= s + j + x%j
end /*j*/ /* [↑] % is REXX integer division. */
if j*j==x then return s + j /*Was X a square? If so, add √ X */
'''output''' is the same as the 2<sup>nd</sup> REXX version.▼
▲
===version 4, SIGMA using integer SQRT===
Line 3,921 ⟶ 3,928:
<lang rexx>/*REXX program calculates and displays all amicable pairs up to a given number. */
parse arg H .; if H=='' | H=="," then H=20000 /*get optional arguments (high limit).*/
w= length(H) ;
x= 220 34765731 6232 87633 284 12285 10856 36939357 6368 5684679 /*S minimums.
do i=0 for 10; $.i= word(x, i + 1);
#=0 /*number of amicable pairs found so far*/▼
@.= /* [↑] LOW is lowest amicable number. */
do k=low for H-low /*generate sigma sums for a range of #s*/
parse var k '' -1 D /*obtain last decimal digit of K. */
if k<$.D then iterate /*if no need to compute, then skip it. */
_= sigma(k)
@.k=
if k==@._ then do /*is it a possible amicable number ? */
if _==k then iterate /*Is it a perfect number? Then skip it*/
#= # +
say right(_, w)
end
end /*k*/ /* [↑] process a range of integers. */
Line 3,941 ⟶ 3,948:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt: procedure; parse arg x; r= 0; q= 1;
do while q>1; q=q%4; _=x-r-q; r=r%2; if _>=0 then do;x=_;r=r+q; end; end
return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
sigma: procedure; parse arg x; od= x //
s=
do j=2+od by 1+od to iSqrt(x) /*divide by all integers up to the √ x */
if x//j==0 then s= s + j + x%j
end /*j*/ /* [↑] % is the REXX integer division.*/
if j*j==x then return s + j /*Was X a square? If so, add √ X */
'''output''' is the same as the 2<sup>nd</sup> REXX version.▼
▲
===version 5, SIGMA (in-line code)===
Line 3,962 ⟶ 3,971:
<lang rexx>/*REXX program calculates and displays all amicable pairs up to a given number. */
parse arg H .; if H=='' | H=="," then H=20000 /*get optional arguments (high limit).*/
w= length(H) ;
x= 220 34765731 6232 87633 284 12285 10856 36939357 6368 5684679
do i=0 for 10; $.i= word(x, i + 1);
#=0 /*number of amicable pairs found so far*/▼
@.= /* [↑] LOW is lowest amicable number. */
#= 0
do k=low for H-low /*generate sigma sums for a range of #s*/
parse var k '' -1 D /*obtain last decimal digit of K. */
if k<$.D
if k==@._ then do
r=0; do while q>1; q=q%4; _=z-r-q; r=r%2; if _>=0 then do; z=_; r=r+q; end; end▼
end /*j*/ /* [↑] % is REXX integer division. */▼
▲ @.k=s /*only keep the pertinent sigma sums. */
▲ if k==@.s then do /*is it a possible amicable number ? */
▲ if s==k then iterate /*Is it a perfect number? Then skip it*/
▲ #=#+1 /*bump the amicable pair counter. */
end
end /*k*/ /* [↑] process a range of integers. */
say
▲say /*stick a fork in it, we're all done. */
say # 'amicable pairs found up to' H /*display the count of amicable pairs. */
exit /*stick a fork in it, we're all done. */
'''output''' is the same as the 2<sup>nd</sup> REXX version. <br><br>▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt: procedure; parse arg x; r= 0; q= 1; do while q<=x; q= q * 4; end
return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
sigma: procedure; parse arg x; od= x // 2 /*use either EVEN or ODD integers. */
s= 1 /*set initial sigma sum to unity. ___*/
do j=2+od by 1+od to iSqrt(x) /*divide by all integers up to the √ x */
if x//j==0 then s= s + j + x%j /*add the two divisors to the sum. */
/* ___ */
if j*j==x then return s + j /*Was X a square? If so, add √ X */
return s /*return (sigma) sum of the divisors. */</lang>
▲
=={{header|Ring}}==
|