Amicable pairs: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments, used a template for the output sections.
(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>Call time 'R'
/*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 /*use either EVEN or ODD integers. */
s= 1 /*set initial sigma sum to unity. ___*/
do j=2+od by 1+od while j*j<x /*divide by all integers up to the √ xX */
if x//j==0 then s= s + j + x%j /*add the two divisors to the sum. */
end /*j*/ /* [↑] % is REXX integer division. */
return s /*return the sum of the divisors. ___ */</lang>
if kj*j==@.sx then do return s + j /*Was X a square? If /*isso, itadd a possible amicable number ? X */
@.k=s return s /*onlyreturn keep(sigma) thesum pertinentof sigmathe sumsdivisors. */</lang>
{{out|output|text=&nbsp; 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); end /*i*/end /*minimum amicable #s for last dec dig.*/
#=0 /*number of amicable pairs found so far*/
@.= /* [↑] LOW is lowest amicable number. */
#=0 0 /*number of amicable pairs found so far*/
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) /*generate sigma sum for the number K.*/
@.k=_ _ /*only keep the pertinent sigma sums. */
if k==@._ then do /*is it a possible amicable number ? */
if _==k then iterate /*Is it a perfect number? Then skip it*/
#= # +1 1 /*bump the amicable pair counter. */
say right(_, w) ' and ' right(k, w) " are an amicable pair."
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 //2 2 /*use either EVEN or ODD integers. */
s=1 1 /*set initial sigma sum to unity. ___*/
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 /*add the two divisors to the sum. */
end /*j*/ /* [↑] % is REXX integer division. */
return s /*return the sum of the divisors. ___ */</lang>
if j*j==x then return s + j /*Was X a square? If so, add √ X */
'''output''' &nbsp; is the same as the 2<sup>nd</sup> REXX version.
#=#+1 return s /*bumpreturn the(sigma) amicablesum pairof counterthe divisors. */</lang>
'''{{out|output''' |text=&nbsp; is the sameidentical asto the 2<sup>nd</sup> REXX version.}} <br><br>
 
===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) ; low=220 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); end /*i*/end /*minimum amicable #s for last dec dig.*/
#=0 /*number of amicable pairs found so far*/
@.= /* [↑] LOW is lowest amicable number. */
#=0 0 /*number of amicable pairs found so far*/
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) /*generate sigma sum for the number K.*/
@.k=_ _ /*only keep the pertinent sigma sums. */
if k==@._ then do /*is it a possible amicable number ? */
if _==k then iterate /*Is it a perfect number? Then skip it*/
#= # +1 1 /*bump the amicable pair counter. */
say right(_, w) ' and ' right(k, w) " are an amicable pair."
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<=x; q= q * 4; end
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 //2 2 /*use either EVEN or ODD integers. */
s=1 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. */
end /*j*/ /* [↑] % is the REXX integer division.*/
return s /*return the sum of the divisors. ___ */</lang>
if j*j==x then return s + j /*Was X a square? If so, add √ X */
'''output''' &nbsp; is the same as the 2<sup>nd</sup> REXX version.
say return s /*stickreturn a(sigma) forksum inof it,the divisors. we're all done. */</lang>
'''{{out|output''' |text=&nbsp; is the sameidentical asto the 2<sup>nd</sup> REXX version.}} <br><br>
 
===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) ; low=220 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); end /*i*/end /*minimum amicable #s for last dec dig.*/
f.=0; do p=0 until f.p>10**digits(); f.p=4**p; end /*p*/ /*calc. pows of 4*/
#=0 /*number of amicable pairs found so far*/
@.= /* [↑] LOW is lowest amicable number. */
#= 0 do k=low for H-low+1 /*generate sigma sums for a range /*number of #samicable pairs found so far*/
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. */
od=k//2 _= sigma(k) /*OD:generate sigma sum setfor to unity ifthe number K is odd.*/
#=0 @.k= _ /*numberonly keep ofthe amicablepertinent pairssigma foundsums. so far*/
z=k; q=1; do p=0 while f.p<=z; q=f.p; end /*R will end up being the iSqrt of Z.*/
if k==@._ then do if s==k then iterate /*Isis it a perfectpossible amicable number ? Then skip it*/
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
s=1 if _==k then iterate /*setIs initialit sigmaa sumperfect tonumber? unity. Then skip ___it*/
do j #=2+od # by+ 1+od to r /*dividebump bythe allamicable integerspair counter. up to the K */
if k//j==0 then s=s+ j + k%j say right(_, w) /*add' theand two' divisors to the sum.right(k, w) */" are an amicable pair."
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. */
say right(s,w) ' and ' right(k,w) " are an amicable pair."
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. */</lang>
exit /*stick a fork in it, we're all done. */
'''output''' &nbsp; 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
r=0; do while q>1; q=q%4; _=zx-r-q; r=r%2; if _>=0 then do; zx=_; r=r+q; end; 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. */
end /*j*/ /* [↑] % is the REXX integer division. */
/* ___ */
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>
'''{{out|output''' |text=&nbsp; is the sameidentical asto the 2<sup>nd</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==