Anonymous user
Untouchable numbers: Difference between revisions
m
→{{header|REXX}}: moved more code into subroutines for more idiomatic code, moved subroutines in alphabetical order.
m (→{{header|REXX}}: added the fifth power of ten to the output.) |
m (→{{header|REXX}}: moved more code into subroutines for more idiomatic code, moved subroutines in alphabetical order.) |
||
Line 290:
=={{header|REXX}}==
Some optimization was done to the code to produce prime numbers, since a simple test could be made to exclude
<br>the calculation of touchability for primes as the aliquot sum of a prime is unity.
<br>This saved around '''15%''' of the running time.
A fair amount of code was put into the generation of primes, but the computation of the aliquot sum was the area
<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 . /*obtain optional arguments from the CL*/
Line 296 ⟶ 302:
if tens='' | tens=="," then tens= 0 /* " " " " " " */
tell= n>0; n= abs(n) /*N>0? Then display the untouchable #s*/
call genP
do p=1 for #; _= @.p + 1;
_= @.p + 3;
end /*p*/ /* [↑] this will also rule out 5. */
lim= n*9;
y= sigma(j); if y<=n then u.y= 1 /*mark Y as a touchable if in range. */
end /*j*/
exit cnt
do t=6 by 2 to n /*traipse through the even numbers. */▼
if !.t==1 then iterate /*Is it touchable? Then skip this #. */▼
cnt= cnt + 1 /*bump the count of untouchable numbers*/▼
if \tell then iterate /*Not telling? Then skip grid build. */▼
if cnt//cols\==0 then iterate /*allow X untouchable numbers per line.*/▼
end /*t*/▼
if n>0 then say right( commas(cnt), 20) ' untouchable numbers were found ≤ ' commas(n)▼
▲if tens==0 then exit cnt /*Any "tens" specified? No, then exit.*/
▲exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
grid: $= $ right( commas(t), w); if cnt//cols==0 then do; say $; $=; end; return
powers: do pr=1 for tens; call 'UNTOUCHA' -(10**p); end /*recurse*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; @.8=19 /*a list of low primes*/
!.2=1; @.3=1; @.5=1; !.7=1; !.11=1; !.13=1; !.17=1; !.19=1 /*define some primes. */
sq.9= 9*9; #= 8; !.= 0; sd= 9 /*square; #primes; is_prime; start of ÷*/
do j=@.#+2 by 2 to n*18 /*find odd primes from here on forward.*/
parse var j '' -1 _ /*obtain the last decimal digit of J. */
▲
if j//13==0 then iterate /*" " " 13 ? " " " */
if j//17==0 then iterate /*" " " 17 ? " " " */
if j//19==0 then iterate /*" " " 19 ? " " " */
do k=sd while sq.k<=j /* [↓] divide Y by known odd primes.*/
!.j= 1; @.#= j; sq.#= j*j /*assign the #th prime; flag as prime.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
do
▲ if j // @.k == 0 then iterate j /*J ÷ by a prime? Then ¬prime. ___ */
if n>0 then say right( commas(cnt), 20) ,
▲ end /*j*/; return /*#: is the number of primes generated*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sigma:
/* ___ */
if
return
{{out|output|text= when using the default inputs:}}
<pre>
2 5 52 88 96 120 124 146 162 188
206 210 216 238 246 248 262 268 276 288
|