Self-describing numbers: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 1,729:
/*────────────────────────────────────────────────────── self─descriptive, */
/*────────────────────────────────────────────────────── autobiographical, or a */
/*────────────────────────────────────────────────────── curious number. */
parse arg x y . /*obtain optional arguments from the CL*/
if x=='' | x=="," then exit /*Not specified? Then get out of Dodge*/
Line 1,736:
numeric digits max(9, w) /*ensure we can handle larger numbers. */
if x==y then do /*handle the case of a single number. */
noYes=test_sdntest_SDN(y) /*is it or ain't it? */
say y word("is isn't", noYes+1) 'a self-describing number.'
exit
Line 1,742:
 
do n=x to y
if test_sdntest_SDN(n) then iterate /*if not self─describing, try again. */
say right(n,w) 'is a self-describing number.' /*is it? */
end /*n*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
test_sdntest_SDN: procedure; parse arg ?; L=length(?) /*obtain the argument and its length.*/
do j=L to 1 by -1 /*parsing backwards is slightly faster.*/
if substr(?,j,1)\==L-length(space(translate(?,,j-1),0)) then return 1
Line 1,758:
║ and then compare the new number's length to the original length. ║
║ ║
║ The difference in length is the number of digits translated.
╚══════════════════════════════════════════════════════════════════╝
</pre>
Line 1,774:
===faster method===
(Uses table lookup.)
<lang rexx>/*REXX program determines if a number (in base 10) is a self-describing. number.*/
parse arg x y . /*obtain optional arguments from the CL*/
if x=='' | x=="," then exit /*Not specified? Then get out of Dodge*/
Line 1,782:
$= '1210 2020 21200 3211000 42101000 521001000 6210001000' /*the list of numbers.*/
/*test for a single integer. */
if x==y then do /*handle the case of a single number. */
say word("isn't is", wordpos(x, $) + 1) 'a self-describing number.'
exit
end
/* [↓] test for a range of integers.*/
do n=x to y; parse var n '' -1 _ /*obtain the last decimal digit of N. */
Line 1,799:
 
(Results are instantaneous.)
<lang rexx>/*REXX program determines if ana integernumber (in base 10) is a self-describing. number.*/
parse arg x y . /*obtain optional arguments from the CL*/
if x=='' | x=="," then exit /*Not specified? Then get out of Dodge*/
Line 1,807:
$= '1210 2020 21200 3211000 42101000 521001000 6210001000' /*the list of numbers.*/
/*test for a single integer. */
if x==y then do /*handle the case of a single number. */
say word("isn't is", wordpos(x, $) + 1) 'a self-describing number.'
exit
end
/* [↓] test for a range of integers.*/
do n=1 for words($); _=word($, n) /*look for integers that are in range. */
if _=word($, n) <x | _>y then iterate /*obtainif anot self-describing, singletry integeragain. from the list*/
ifsay _<x | right(_>y, thenw) iterate 'is /*if nota self-describing, try againnumber. */'
say right(_,w) 'is a self-describing number.'
end /*n*/ /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; is the same as the 1<sup>st</sup> REXX example.