Self numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added escuse)
m (→‎generator dictionary: pqhead -> gdhead)
Line 612: Line 612:
Aside: the getd_index() check is often worth trying with phix dictionaries: if there is a high probability that
Aside: the getd_index() check is often worth trying with phix dictionaries: if there is a high probability that
the key already exists, it will yield a win, but with a low probability it will just be unhelpful overhead.
the key already exists, it will yield a win, but with a low probability it will just be unhelpful overhead.
<lang Phix>integer gd = new_dict(), g = 1, pqhead = 2, n = 0
<lang Phix>integer gd = new_dict(), g = 1, gdhead = 2, n = 0


function ng(integer n)
function ng(integer n)
Line 627: Line 627:
integer nxt
integer nxt
n += 1
n += 1
while n=pqhead do
while n=gdhead do
while g<=pqhead do
while g<=gdhead do
nxt = ng(g)
nxt = ng(g)
if getd_index(nxt, gd)=NULL then -- (~25% gain)
if getd_index(nxt, gd)=NULL then -- (~25% gain)
Line 635: Line 635:
g += 1
g += 1
end while
end while
integer waspqhead = pqhead
integer wasgdhead = gdhead
while true do
while true do
pqhead = pop_dict(gd)[1]
gdhead = pop_dict(gd)[1]
if pqhead!=waspqhead then exit end if
if gdhead!=wasgdhead then exit end if
-- ?{"ding",waspqhead} -- 2, once only...
-- ?{"ding",wasgdhead} -- 2, once only...
end while
end while
nxt = ng(pqhead)
nxt = ng(gdhead)
-- if getd_index(nxt, gd)=NULL then -- (~1% loss)
-- if getd_index(nxt, gd)=NULL then -- (~1% loss)
setd(nxt,0,gd)
setd(nxt,0,gd)
-- end if
-- end if
n += (n!=pqhead)
n += (n!=gdhead)
end while
end while
return n
return n