Vampire number: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed some comments and whitespace.) |
|||
Line 2,686: | Line 2,686: | ||
if N>0 then do j=1260 until # >= N /*search until N vampire numbers found.*/ |
if N>0 then do j=1260 until # >= N /*search until N vampire numbers found.*/ |
||
if length(j) // 2 then do; j=j*10 - 1; iterate; end /*adjust J*/ |
if length(j) // 2 then do; j=j*10 - 1; iterate; end /*adjust J*/ |
||
_=right( |
_=right(j,1); if j<!._ then iterate /*is number tenable based on last dig? */ |
||
f=vampire(j); if f=='' then iterate /*Are fangs null? Yes, not vampire. */ |
f=vampire(j); if f=='' then iterate /*Are fangs null? Yes, not vampire. */ |
||
#=#+1 /*bump the vampire count, Vlad. */ |
#=#+1 /*bump the vampire count, Vlad. */ |
||
say 'vampire number' right(#,length(N)) "is: " j', fangs=' f |
say 'vampire number' right(#,length(N)) "is: " j', fangs=' f |
||
end /*j*/ /* [↑] process a range of numbers. */ |
end /*j*/ /* [↑] process a range of numbers. */ |
||
else do; N=abs(N); f=vampire(N) /* [↓] process |
else do; N=abs(N); f=vampire(N) /* [↓] process a number; obtain fangs.*/ |
||
if f=='' then say N " isn't a vampire number." |
if f=='' then say N " isn't a vampire number." |
||
else say N " is a vampire number, fangs=" f |
else say N " is a vampire number, fangs=" f |
||
end |
end |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
vampire: procedure; parse arg ?,, $. !! bot; L=length(?) |
vampire: procedure; parse arg ?,, $. !! bot; L=length(?) |
||
if L//2 then return !! /*Is L an odd length? Then ¬vampire.*/ |
|||
do k=1 for L; _=substr(?,k,1); $._=$._ || _; end /*k*/ |
|||
do m=0 for 10; bot=bot || $.m; end /*m*/ |
|||
w=L % 2 |
|||
top=left( reverse(bot), w); bot=left(bot, w) /*determine limits of search*/ |
top=left( reverse(bot), w); bot=left(bot, w) /*determine limits of search*/ |
||
inc=?//2 + 1 /*? is odd? INC=2. No? INC=1*/ |
inc=?//2 + 1 /*? is odd? INC=2. No? INC=1*/ |
||
beg=max(bot, 10**(w-1)); if inc=2 then if beg//2==0 then beg=beg + 1 |
|||
/* [↑] odd |
/* [↑] odd BEG if odd INC*/ |
||
do d= |
do d=beg to min(top, 10**w - 1) by inc |
||
if ?//d\==0 |
if ? // d \==0 then iterate |
||
if verify(d, ?) \==0 then iterate |
if verify(d, ?) \==0 then iterate |
||
q=?%d; |
q=? % d; if d>q then iterate |
||
if q*d//9\==(q+d)//9 then iterate |
if q*d//9 \== (q+d)//9 then iterate /*modulo 9 congruence test. */ |
||
if verify(q, ?) \==0 then iterate |
if verify(q, ?) \==0 then iterate |
||
if right(q, 1) ==0 then if right(d, 1)==0 then iterate |
if right(q, 1) ==0 then if right(d, 1)==0 then iterate |
||
if length(q)\==w then iterate |
if length(q) \== w then iterate |
||
dq=d || q; t=? |
dq=d || q; t=? |
||
do i=1 for L; |
do i=1 for L; p=pos( substr(dq, i, 1), t) |
||
if p==0 then iterate d; |
if p==0 then iterate d; t=delstr(t, p, 1) |
||
end /*i*/ |
end /*i*/ |
||
!!=!! '['d"∙"q']' |
!!=!! '['d"∙"q']' |
||
end /*d*/ |
end /*d*/ |
||
return !!</lang> |
return !!</lang> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
|||
<pre style="height:50ex"> |
|||
vampire number 1 is: 1260, fangs= [21∙60] |
vampire number 1 is: 1260, fangs= [21∙60] |
||
vampire number 2 is: 1395, fangs= [15∙93] |
vampire number 2 is: 1395, fangs= [15∙93] |