Distinct palindromes within decimal numbers: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
m (→‎{{header|Raku}}: Whoops, important full stop)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 331:
character => True
palindrome? => False</pre>
 
=={{header|REXX}}==
This REXX version can handle strings or numbers, &nbsp; a hexadecimal string is included in the examples.
<lang>/*REXX program finds distinct palindromes contained in substrings (of decimal numbers). */
parse arg LO HI mL $$ /*obtain optional arguments from the CL*/
if LO='' | LO="," then LO= 100 /*Not specified? Then use the default.*/
if HI='' | HI="," then HI= 125 /* " " " " " " */
if mL='' | mL="," then mL= 2 /* " " " " " " */
if $$='' | $$="," then $$= 9 169 12769 1238769 12346098769 1234572098769 123456832098769,
12345679432098769 1234567905432098769 123456790165432098769 ,
83071934127905179083 'deadbeef' /* (hexadecimal) */ ,
1320267947849490361205695 /* special numbers.*/
w= length(HI) /*max width of LO ──► HI for alignment.*/
 
do j=LO to HI; #= Dpal(j, 1) /*get # distinct palindromes, minLen=1 */
say right(j, w) ' has ' # " palindrome"s(#)': ' $
end /*j*/
 
w= length( word($$, words($$) ) ) /*find the length of the last special #*/
if words($$)==0 then exit 0 /*No special words/numbers? Then exit.*/
say
do j=1 for words($$); z= word($$, j)
#= Dpal(z, mL) /*get # distinct palindromes, minLen=mL*/
_= left(':', #>0); @has= ' has '; @of='of length'
say right(z, w) @has # " palindrome"s(#,,' ') @of mL "or more"_ space($)
end /*j*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return arg(3); return word( arg(2) 's', 1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
Dpal: procedure expose @. !. $ w; parse arg x, mL; $=; !.= 0; #= 0; L= length(x)
do j=1 for L /*test for primality for all substrings*/
do k=1 to L-j+1 /*search for substrings (including X).*/
y= strip( substr(x, j, k) ) /*extract a substring from the X string*/
if length(y)<mL | y\==reverse(y) then iterate /*too short or ¬palindromic?*/
if \!.y then do; $= $ right(y, w); !.y= 1; #= # + 1; end
end /*k*/
end /*j*/
return #</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
100 has 3 palindromes: 1 0 00
101 has 3 palindromes: 1 101 0
102 has 3 palindromes: 1 0 2
103 has 3 palindromes: 1 0 3
104 has 3 palindromes: 1 0 4
105 has 3 palindromes: 1 0 5
106 has 3 palindromes: 1 0 6
107 has 3 palindromes: 1 0 7
108 has 3 palindromes: 1 0 8
109 has 3 palindromes: 1 0 9
110 has 3 palindromes: 1 11 0
111 has 3 palindromes: 1 11 111
112 has 3 palindromes: 1 11 2
113 has 3 palindromes: 1 11 3
114 has 3 palindromes: 1 11 4
115 has 3 palindromes: 1 11 5
116 has 3 palindromes: 1 11 6
117 has 3 palindromes: 1 11 7
118 has 3 palindromes: 1 11 8
119 has 3 palindromes: 1 11 9
120 has 3 palindromes: 1 2 0
121 has 3 palindromes: 1 121 2
122 has 3 palindromes: 1 2 22
123 has 3 palindromes: 1 2 3
124 has 3 palindromes: 1 2 4
125 has 3 palindromes: 1 2 5
 
9 has 0 palindromes of length 2 or more
169 has 0 palindromes of length 2 or more
12769 has 0 palindromes of length 2 or more
1238769 has 0 palindromes of length 2 or more
12346098769 has 0 palindromes of length 2 or more
1234572098769 has 0 palindromes of length 2 or more
123456832098769 has 0 palindromes of length 2 or more
12345679432098769 has 0 palindromes of length 2 or more
1234567905432098769 has 0 palindromes of length 2 or more
123456790165432098769 has 0 palindromes of length 2 or more
83071934127905179083 has 0 palindromes of length 2 or more
deadbeef has 1 palindrome of length 2 or more: ee
1320267947849490361205695 has 3 palindromes of length 2 or more: 202 494 949
</pre>
 
=={{header|Wren}}==