Anonymous user
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=
say y word("is isn't", noYes+1) 'a self-describing number.'
exit
Line 1,742:
do n=x to y
if
say right(n,w) 'is a self-describing number.' /*is it? */
end /*n*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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
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
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
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
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($,
if _
end /*n*/ /*stick a fork in it, we're all done. */</lang>
'''output''' is the same as the 1<sup>st</sup> REXX example.
|