ความจริง 900 ไบต์
R(x)==>return x;p(r,a)==(n:=#(a::String);if r<0 then(a=0=>R a;n=1 or a=10^(n-1)=>R(a-1);a=10^(n-1)+1=>R(a-2));if r>0 then(n=1 and a<9=>R(a+1);a=10^n-1=>R(a+2));r=0 and n=1=>1;v:=a quo 10^(n quo 2);repeat(c:=v;w:=(n rem 2>0=>v quo 10;v);repeat(c:=10*c+w rem 10;w:=w quo 10;w=0=>break);r<0=>(c<a=>R c;v:=v-1);r>0=>(c>a=>R c;v:=v+1);R(c=a=>1;0));c)
b:List INT:=[];o:INT:=0
f(a:NNI):List INT==(free b,o;o:=p(-1,o);w:=0;c:=#b;if c>0 then w:=b.1;e:=a-o;e>10000000=>R[];if w<e then repeat(w:=p(1,w);w>e=>break;b:=cons(w,b));g:List INT:=[];for i in #b..1 by-1 repeat(b.i>e=>break;g:=cons(b.i,g));if o>e then g:=cons(o,g);n:=#g;for i in 1..n repeat(x:=g.i;x=a=>R[x];3*x<a=>break;for j in i..n repeat(y:=g.j;t:=x+y;t>a=>iterate;t=a=>R[x,y];t+y<a=>break;for k in j..n repeat(z:=t+g.k;z=a=>R[x,y,g.k];z<a=>break)));[])
D(a:NNI):List INT==(free o;p(0,a)=1=>[a];o:=a;for j in 1..10 repeat(t:=f(a);#t>0=>R t);[])
รหัสทดสอบ
--Lista random di n elmenti, casuali compresi tra "a" e "b"
randList(n:PI,a:INT,b:INT):List INT==
r:List INT:=[]
a>b =>r
d:=1+b-a
for i in 1..n repeat
r:=concat(r,a+random(d)$INT)
r
test()==
a:=randList(20,1,12345678901234)
[[i,D(i)] for i in a]
หากรหัสนี้จะต้องสลายตัวหมายเลข X ใน 1,2,3 palindrome รหัสนี้ทำอะไรมันจะลองใกล้ palindrome N <X และสลาย XN ใน 2 palindrome หากการสลายตัวของ XN นี้พบความสำเร็จกลับมา 3 palindrome ถ้ามันล้มเหลวให้ลองใช้ palindrome ก่อนหน้านี้ G <N <X แล้วลองสลาย XG ใน 2 palindrome เป็นต้น Ungolf code (แต่อาจเป็นไปได้ว่ามีข้อผิดพลาดบางอย่าง)
R(x)==>return x
-- se 'r'=0 ritorna 1 se 'a' e' palindrome altrimenti ritorna 0
-- se 'r'>0 ritorna la prossima palindrome >'a'
-- se 'r'<0 ritorna la prossima palindrome <'a'
p(r,a)==(n:=#(a::String);if r<0 then(a=0=>R a;n=1 or a=10^(n-1)=>R(a-1);a=10^(n-1)+1=>R(a-2));if r>0 then(n=1 and a<9=>R(a+1);a=10^n-1=>R(a+2));r=0 and n=1=>1;v:=a quo 10^(n quo 2);repeat(c:=v;w:=(n rem 2>0=>v quo 10;v);repeat(c:=10*c+w rem 10;w:=w quo 10;w=0=>break);r<0=>(c<a=>R c;v:=v-1);r>0=>(c>a=>R c;v:=v+1);R(c=a=>1;0));c)
b:List INT:=[] -- the list of palindrome
o:INT:=0 -- the start value for search the first is a
--Decompose 'a' in 1 or 2 or 3 palindrome beginning with prev palindrome of o
--if error or fail return []
f(a:NNI):List INT==
free b,o
-- aggiustamento di o, come palindrome piu' piccola di o
o:=p(-1,o)
-- aggiustamento di b come l'insieme delle palindromi tra 1..a-o compresa
w:=0;c:=#b
if c>0 then w:=b.1 --in w la massima palindrome presente in b
e:=a-o
output["e=",e,"w=",w,"o=",o,"#b=",#b]
e>10000000=>R[] --impongo che la palindrome massima e' 10000000-1
if w<e then --se w<a-o aggiungere a b tutte le palindromi tra w+1..a-o
repeat(w:=p(1,w);w>e=>break;b:=cons(w,b))
-- g e' l'insieme dei b palindromi tra 1..a-o,o
g:List INT:=[];for i in #b..1 by-1 repeat(b.i>e=>break;g:=cons(b.i,g))
if o>e then g:=cons(o,g)
--output["g=",g,b]
n:=#g
for i in 1..n repeat
x:=g.i
x=a =>R[x]
3*x<a=>break
for j in i..n repeat
y:=g.j;t:=x+y
t>a =>iterate
t=a =>R[x,y]
t+y<a =>break
for k in j..n repeat
z:=t+g.k
z=a =>R[x,y,g.k]
z<a =>break
[]
--Decompose 'a' in 1 or 2 or 3 palindrome
--if error or fail return []
dPal(a:NNI):List INT==
free o
p(0,a)=1=>[a]
o:=a -- at start it is o=a
for j in 1..10 repeat -- try 10 start values only
t:=f(a)
#t>0=>R t
[]
ผล:
(7) -> [[i,D(i)] for i in [5,15,21,42,132,345,1022,9265] ]
(7)
[[5,[5]], [15,[11,4]], [21,[11,9,1]], [42,[33,9]], [132,[131,1]],
[345,[343,2]], [1022,[999,22,1]], [9265,[9229,33,3]]]
Type: List List Any
Time: 0.02 (IN) + 0.02 (OT) = 0.03 sec
(8) -> test()
(8)
[[7497277417019,[7497276727947,624426,64646]],
[11535896626131,[11535888853511,7738377,34243]],
[2001104243257,[2001104011002,184481,47774]],
[3218562606454,[3218561658123,927729,20602]],
[6849377785598,[6849377739486,45254,858]],
[375391595873,[375391193573,324423,77877]],
[5358975936064,[5358975798535,136631,898]],
[7167932760123,[7167932397617,324423,38083]],
[11779002607051,[11779000097711,2420242,89098]],
[320101573620,[320101101023,472274,323]],
[5022244189542,[5022242422205,1766671,666]],
[5182865851215,[5182864682815,1158511,9889]],
[346627181013,[346626626643,485584,68786]],
[9697093443342,[9697092907969,443344,92029]],
[1885502599457,[1885502055881,542245,1331]], [10995589034484,[]],
[1089930852241,[1089930399801,375573,76867]],
[7614518487477,[7614518154167,246642,86668]],
[11859876865045,[11859866895811,9968699,535]],
[2309879870924,[2309879789032,81418,474]]]
Type: List List Any
Time: 0.25 (IN) + 115.17 (EV) + 0.13 (OT) + 28.83 (GC) = 144.38 sec