Express 314159265358979323846 as a sum of positive palindromes, the idea being to use as few summands as you can.
Take the first half of the original number and append that half's reverse to that half to get a palindrome. Include the middle digit separately between the forward and reverse if an odd number of digits. Lower the middle digit or two of that created palindrome if necessary to make sure the palindrome is less than or equal to the number. Use that palindrome, and subtract from the original number; then continue this process working on what remains each time, until there is nothing left:
314159265353562951413
5416336145
36263
22
3
5 open "palinsum.txt" for output as #2
10 dim Pal(30)
20 Num$="314159265358979323846"
30 N=val(Num$)
40 gosub *Palinate(1)
50 print Pal(1)+Pal(2)+Pal(3)+Pal(4)+Pal(5)
60 close #2
999 end
1010 *Palinate(Wh)
1015 P$=left(Num$,ceil(len(Num$)/2)):P1$=P$
1020 for I=floor(len(Num$)/2) to 1 step -1
1030 P$=P$+mid(Num$,I,1)
1040 next
1050 if val(Num$)-val(P$)<0 then
1060 :P$=cutspc(str(val(P1$)-1))
1070 :for I=floor(len(Num$)/2) to 1 step -1
1080 :P$=P$+mid(Num$,I,1)
1090 :next
1100 :endif
1150 Pal(Wh)=val(P$)
1160 Num$=cutspc(str(val(Num$)-val(P$)))
1170 if val(Num$)=0 then
1180 :for I=1 to Wh
1190 :print Pal(I):print #2,Pal(I)
1200 :next
1210 :else
1220 :gosub *Palinate(Wh+1)
1230 :endif
1260 return
|
Posted by Charlie
on 2017-08-08 10:13:42 |