Untouchable numbers: Difference between revisions

→‎{{header|REXX}}: changed the code to be more idiomatic, optimized the sigmaP function (aliquot function) for about a 5% improvement in speed, updated (correct) count of untouchable numbers for up to and including a million.
(→‎{{header|REXX}}: changed wording in the post-output section notes.)
(→‎{{header|REXX}}: changed the code to be more idiomatic, optimized the sigmaP function (aliquot function) for about a 5% improvement in speed, updated (correct) count of untouchable numbers for up to and including a million.)
Line 366:
<br>that consumed the most CPU time.
<lang rexx>/*REXX pgm finds N untouchable numbers (numbers that can't be equal to any aliquot sum).*/
parse arg n cols tens over . /*obtain optional arguments from the CL*/
if n='' | n=="," then n=2000 /*Not specified? Then use the default.*/
if cols='' | cols=="," | cols==0 then cols= 10 /* " " " " " " */
if tens='' | tens=="," then tens= 0 /* " " " " " " */
if over='' | over=="," if y<=n then u.yover= 1 20 /* " " /*mark Y" as a" touchable if in" " range. */
tell= n>0; n= abs(n) /*N>0? Then display the untouchable #s*/
call genP n * 20 over /*call routine to generate some primes.*/
u.= 0 /*define all possible aliquot sums ≡ 0.*/
do p=1 for #; _= @.p + 1; u._= 1 /*any prime+1 is not an untouchable.*/
Line 378 ⟶ 379:
u.5= 0 /*special case as prime 2 + 3 sum to 5.*/
do j=2 for lim; if !.j then iterate /*Is J a prime? Yes, then skip it. */
oddy= j // 2sigmaP() /*usecompute: either aliquot EVENsum (sigma orP) of ODD integers. */ /* ◄■■■■■■ aliquot sumJ.*/
if y<= 1 n then u.y= 1 /*setmark initial sigma sum (Y) to 1.as a touchable ___*/if in range. /* ◄■■■■■■ aliquot sum.*/
do m=2+odd by 1+odd while q.m<j /*divide by all integers up to the √ J */ /* ◄■■■■■■ aliquot sum.*/
if j//m==0 then y= y + m + j%m /*add the two divisors to the sum. */ /* ◄■■■■■■ aliquot sum.*/
end /*m*/ /* [↑] % is REXX integer division. */ /* ◄■■■■■■ aliquot sum.*/
/* ___ */ /* ◄■■■■■■ aliquot sum.*/
if q.m==j then y= y + m /*Was J a square? If so, add √ J */ /* ◄■■■■■■ aliquot sum.*/
if y<=n then u.y= 1 /*mark Y as a touchable if in range. */
end /*j*/
call show /*maybe show untouchable #s and a count*/
Line 398 ⟶ 393:
genP: #= 9; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; @.8=19; @.9=23 /*a list*/
!.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1; !.19=1 !.23=1 /*primes*/
parse arg lim; call genSq; qq.10= 100 /*define the (high) limit for searching*/
qq.10= 100 /*define square of the 10th prime index*/
do j=@.#+6 by 2 to lim /*find odd primes from here on forward.*/
parse var j '' -1 _; if _==5 then iterate; if j// 3==0 then iterate
if j// 7==0 then iterate; if j//11==0 then iterate; if j//13==0 then iterate
if j//17==0 then iterate; if j//19==0 then iterate; if j//23==0 then iterate
do k=10 while qq.k<=j /* [↓] divide Y /*start dividing by knownthe tenth oddprime: primes.29*/
if j//@. do k==010 thenwhile iterateqq.k <= j /*J ÷[↓] by adivide prime? J Then ¬prime.by known odd ___ primes.*/
end if j//*@.k*/ ==0 then iterate j /*J [↑]÷ by onlya processprime? numbers Then ¬prime. √ J ___ */
#= #+1 end /*k*/ /* [↑] /*bumponly process numbers the number (count) J of primes. */
!.j#= #+1; @.#= j; qq @.#= j*j /*assignbump theprime #th primecount; assign flaga asnew prime.*/
end!.j= 1; /*j*/; return qq.#= j*j /*#:mark prime; is thecompute numbersquare of primes generatedprime.*/
end /*j*/; return /*#: is the number of primes generated*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: w=7; $= right(2, w+1) right(5, w) /*start the list of an even prime and 5*/
Line 416 ⟶ 413:
end /*t*/
if tell & $\=='' then say $ /*display a residual grid line, if any.*/
if tell then say /*addshow a spacing blank line for output. */
if n>0 then say right( commas(cnt), 20) , /*indent the output a bit.*/
' untouchable numbers were found ≤ ' commas(n); return</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
sigmaP: s= 1 /*set initial sigma sum (S) to 1. ___*/
if j//2 then do m=3 by 2 while q.m<j /*divide by odd integers up to the √ J */
if j//m==0 then ys= y s+ m + j%m /*add the two divisors to the sum. */ /* ◄■■■■■■ aliquot sum.*/
end /*m*/ end /*m*/ /* [↑] %process an is REXXodd integer division. */ /* ◄■■■■■■ aliquot sum.___*/
else do m=2+odd by 1+odd while q.m<j /*divide by all integers up to the √ J */ /* ◄■■■■■■ aliquot sum.*/
if j//m==0 then s=s+m+j%m /*add the two divisors to the sum. */
end /*m*/ /* [↑] process an even integer. ___*/
if q.m==j then return y= ys + m /*Was J a square? If so, add √ J */ /* ◄■■■■■■ aliquot sum.*/
return s /* No, just ___return. */ /* ◄■■■■■■ aliquot sum.*</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 452 ⟶ 459:
1,212 untouchable numbers were found ≤ 10,000
13,863 untouchable numbers were found ≤ 100,000
150,253252 untouchable numbers were found ≤ 1,000,000
</pre>
Due to complications of choosing an overreach limit of eighteen, &nbsp; I cannot be sure to any certainty that the count is correct for one million &nbsp; (using an overreach factor of eighteen).
 
I am currently re-running the REXX program using an overreach limit of twenty.
 
=={{header|Wren}}==