Anonymous user
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 .
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==","
tell= n>0; n= abs(n) /*N>0? Then display the untouchable #s*/
call genP n *
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. */
if y<=
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 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
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 /*
if n>0 then say right( commas(cnt), 20) , /*indent the output a bit.*/
' untouchable numbers were found ≤ ' commas(n); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 */
▲
▲ else do m=2
if j//m==0 then s=s+m+j%m /*add the two divisors to the sum. */
end /*m*/ /* [↑] process an even integer. ___*/
{{out|output|text= 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,
</pre>
=={{header|Wren}}==
|