Self-describing numbers: Difference between revisions
Content added Content deleted
(→{{header|Phix}}: added generator) |
|||
Line 1,629: | Line 1,629: | ||
42101000 |
42101000 |
||
done (21.78s) |
done (21.78s) |
||
</pre> |
|||
===generator=== |
|||
{{trans|Python}} |
|||
Not quite as fast as I hoped it would be, although a bazillion times faster than the above and a good five times faster than Python, the following self(20) completes in just over half a second whereas self(24) takes nearly 9s, and it continues getting exponentially slower after that. Curiously, it is the early stages (same output) that slow down, whereas the latter ones always complete fairly quickly. |
|||
<lang Phix>procedure impl(sequence d, c, integer m) |
|||
if m>=0 then |
|||
integer l = length(d) |
|||
if l and d == c[1..l] then |
|||
string ds = "" |
|||
for i=1 to l do |
|||
integer ch = d[i]+'0' |
|||
if ch>'9' then ch += 'a'-'9'-1 end if |
|||
ds &= ch |
|||
end for |
|||
printf(1,"%s\n",ds) |
|||
end if |
|||
sequence dd = d&0 |
|||
for i=c[l+1] to m do |
|||
dd[$] = i |
|||
if i>l or c[i+1]!=dd[i+1] then |
|||
c[i+1] += 1 |
|||
impl(dd,c,m-i) |
|||
c[i+1] -= 1 |
|||
end if |
|||
end for |
|||
end if |
|||
end procedure |
|||
procedure self(integer n) |
|||
impl({}, repeat(0,n+1), n) |
|||
end procedure |
|||
self(20)</lang> |
|||
{{out}} |
|||
<pre> |
|||
1210 |
|||
2020 |
|||
21200 |
|||
3211000 |
|||
42101000 |
|||
521001000 |
|||
6210001000 |
|||
72100001000 |
|||
821000001000 |
|||
9210000001000 |
|||
a2100000001000 |
|||
b21000000001000 |
|||
c210000000001000 |
|||
d2100000000001000 |
|||
e21000000000001000 |
|||
f210000000000001000 |
|||
g2100000000000001000 |
|||
</pre> |
</pre> |
||