HP 50g: Programming Problem: Integer Partition in Palindromic Integers
|
01-26-2018, 03:50 PM
(This post was last modified: 02-07-2018 05:36 PM by Gerald H.)
Post: #1
|
|||
|
|||
HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Every integer can be written as the sum of palindromic integers, eg
542 = 535 + 7 & in fact 49 palindromes are sufficient to express any integer, as proven here https://arxiv.org/pdf/1508.04721.pdf The task I'm interested in is producing a stand alone User RPL programme for the 50g that will decompose any positive integer into a sum of palindromes, the fewer the better. For example the number 407374883553280653444058251937 can be decomposed in 120 palindromes: Code: :120:{ & in 6 Code: :6:{ & in 5 Code: :5:{ which may be minimal. The programme should produce a list ordered by decreasing magnitude & tagged with the number of elements. Smaller & faster programmes are desirable but the critical characteristic is lowest number of elements in the decomposition. As a standard test for the programme I suggest input of 13^1313 a 1,463 digit integer, correct decompositions being the target & least number of elements deciding the winner. |
|||
02-01-2018, 11:20 AM
(This post was last modified: 02-07-2018 05:38 PM by Gerald H.)
Post: #2
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Edited to "39g programme" from "38G programme" - sorry for the error.
Well, not much progress made on this, unless members are keeping their solutions secret. Meanwhile I have produced a complete solution for the 39g, although I do not claim that the partitions share the characteristic of having a minimum number of elements in the respective palindromic partition. The 39g programme has one sub programme, N2DS, which for natural number input Ans produces a list of digits in list L2, eg for input 23405 L2 will contain {5,0,4,3,2} which may in itself be useful. Code: N2DS The main programme, P24U, which for natural number input Ans produces a list of the palindromic partition members in descending order in list L1. eg For input 987654321678 L1 will contain Code: {899999999998,79999999997,6999999996,599999995,49999994,3999993,299992,19991,999,595,66,9,8,8,7,6,5,5,4,4,3,2,1} Code: P24U As the 39g is limited to 12 digit integers, the above programme solves the problem completely on that machine. However, as the algorithm produces two elements of the partition for each digit, the method becomes impractical on the 49G for , say, 6,000 digit numbers. I again encourage members to apply themselves to this interesting, if unimportant, problem. |
|||
02-02-2018, 08:21 AM
(This post was last modified: 02-02-2018 08:23 AM by Gerald H.)
Post: #3
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
A slightly modified version of the programme to remove the occurrence of 0 in the partition if the input ends in 0:
Code: P24U |
|||
02-04-2018, 08:13 AM
Post: #4
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Here a corrected version for the 38G:
Code: N2DS |
|||
02-05-2018, 12:22 AM
Post: #5
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
This program returns the Palindromic Integers of an integer's partition to the stack. The idea is simple, but the program got somehow complicated.
« WHILE DUP 10 > REPEAT DUP →STR DUPDUP SIZE 2 / SWAP 1 PICK3 SUB DUPDUP 1 5 PICK FLOOR SUB SREV + 4 ROLL OVER < { DROP DUP OBJ→ 1 - →STR DUP ROT SIZE SWAP SIZE ‹ -.5 * ROT + OVER 1 ROT FLOOR SUB SREV + } { NIP NIP } IFTE OBJ→ SWAP OVER - END DUP 10 == { DROP 9 1} IFT » |
|||
02-05-2018, 06:03 AM
(This post was last modified: 02-05-2018 06:06 AM by Gerald H.)
Post: #6
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Very nice & short, Juan14, well done!
& the number of elements in the partition is at most log2(SIZE(N)), so a 6,000 digit number requires about 12 palindromes, sadly the programme does not work correctly for all 6,000 digit numbers. |
|||
02-05-2018, 06:22 AM
(This post was last modified: 02-07-2018 05:25 PM by Gerald H.)
Post: #7
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Your programme, Juan14, works correctly for 13^1313 but not for the 6,000 digit number
Code: 668198125478294158910366586815548046466164478727862555826704455704418886847236053799016108003756517340227007193843219028656970976373874455727196843116706375052199239667522851896442043515465907397273648461935739704478199943218426705969942235257401744714720022737847016166580492233397131969116917288306140472383198752577990692700198497236330156353925076218807377030763724449363780675399365191167476169582818053343351137655276699778055322226035412321402822796233524437885844787133529911477653623327402902741552130823845142361524831430264356748988343250998688669161263344972666859723003039823015196359813329339690077780617656841850940344919334019421035083624243355546620757187435600466389666990363417499401360899828287607335669726440353875187096561026914473983983534761826735072045358790315646491636936152856919017054578117826469134909002800612633256328067256466736936238197585549566008406917733980492004752909340316527394417311071086756506579045704238946939409788312392491561495625038585507818736506777378108348021334935236466772696809505524893229634923284107070596542491968651192695696249524403650755313439788728053447656294952816412454117636015843597566697158474977436106830825672580482395909146817031001108311402077516036802115024264461604241242509287730425483329533779574494651766891299876331933901772645861983579836818227130806924003714546218222563808705804221009511110330306812204844221558747846243269750021472775798982630622906376248313683675609587650152860455630716344049472503213237525279327817978675277798730646877895985307550544728222988608864276566024696634319019583966008732738114716425919524707516927735083438439217030892775252006139144190437583838609457355057812415897351844060074106035847428182000847599383628681419758435295022725301828502238187523222522747714466622081095472801468221667981184110629903731391171237577507329191937197273663088873296701640062546882138496337487179941654001888791324033168251061004441374172652807874502642263334216137965038267152483353636535902717437281043105257109355741923503795733842703667952889420273043745470540325137860946005956322899257844329271673089919157421732978253815984880718473138186583017267153585000799272139130305270439316546139496867672913997834059143273664316209093898597315617101856381676217102460227272202760591381928653767121266487661071879163786558383410937042288004727311791273435353384053585786992253382341007285157441257411847716724043322994843840483423771733091455518841971778031692277246882036347594753042801392719473234356393305206960289783665712579887171699116221670864655690158632964665790415576797550618555464794929668107690428755575626138835308735087403117312342389020064334211381438398798264042144488612377121549934776554167717369854823949181643485171299632176126680510764662698462884263494571157270474657229111465698802778908100791511078017956691541039179139452438174957207712983944369444216250680499673781437472886974066976607557037342528525540996018266040632343561743327816877426044144700192382491024228880021607008461339554618894705135455584329604436603867949112268995253280945946965183763539240822981395209769042278160297503459623857954997438274573219439838270898278275103373370068521598569943151432379987963575827868248621388270211244998752340622507594450108860010494856694120038083089966782322187932345658490540842132185015720327330489402451600874267735783839914235350768250475216930289347055549754012751936317175168084943376843398565516041887487493797545433105008184142419850146384716562797827828102741885659003276765035538870986936362739242439685843597623432632113926240909222331070988423051888529004872317341139995865552645669623651371139361828447794098175781964018857025228866734028011689784239895657160826672798910849886583121000418458994722289449035325149079860960104427619669632385622100919625266370104505566176838390754219015790739098526318985994394845655734441162742074756888135201695851791867083353438312448555434459857592727806967357657609171514242789894460484850725820045799828924120921699583846473485538734390733701050516875854392464583582926829715770830117414058391741199453983916626122236510679753495474313333243562266766028969989679708974259308470054613427594010412598701903639416659442067403105138713127939247028357769164541080526126381707823829422833230713841130330767101669769134403020880868443213871501318433621437153148623167638821637154132894150308493514603168552976761072852111445261581472522081011568762215427394016735715930198344391439075786064819395682805450706475189093352576774590489304244342581042736781783076125429266252205339099108289557487905058597622666209820165432934383156696998304819141299264102331699873964764056349745572235289179066831250254439313364369970747766505455218229325390412903200724968037365655515277713020075690454659662854869478934887814512177491806691725516847474682208008810782406239842821989333856643236023185823423799396633607518388514237592982163831765528762192925738446383046833491029510686315667187931098448810184414228897505474963545228229885349246796090680562199116116843122516759500394519108956166882364353795933460804116413194154779001775954655762344431725393989077089146005967783153379923613902096160005726853489532496375361026154197884296956466728574397903473433788873076799027751125533936573975082593790642547653999604512656821832777552241992616532233506166953517012666908151700857092722376951144682027392076493935608175606828135808285419397209569478359789058729384579673050182457747620866564980285719423037641462394000741879055737257148831233362234814586565665638213561896207565560083092217763654421897167132757809448916393738540863948206860287028904714303748831615017780556672384105644180441008917898168043267026831821678687274798458712383186117770407509577052033143265018331248173772872997761530908754833918121926162323352605087518681906137149107549111424038675165489157616434850425980871739339461837793278640551186371166396883274331252590754445175229336387754546897880241578277859436250322859781775347221081614862605278644138005897576544471598504605 while I would expect it to return Code: :12: { |
|||
02-05-2018, 10:08 AM
(This post was last modified: 02-07-2018 05:26 PM by Gerald H.)
Post: #8
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Her are some smaller numbers that the programme can't process correctly, it might help with trouble-shooting:
Code: { |
|||
02-05-2018, 08:31 PM
Post: #9
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Juan14 - What is the first symbol in line 7 of your programme?
|
|||
02-07-2018, 05:31 PM
Post: #10
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Meanwhile here's my 50g programme which gives correct but perhaps not minimal answers to the problem.
The size of the returned partitions is again log2(SIZE(N)), as for Juan14's programme, but integers up to 40,000 digits have been processed correctly. Size: 557.5 Bytes CkSum: # 6711h Code: « 0 SWAP |
|||
02-08-2018, 12:17 PM
Post: #11
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Here a smaller & faster version of the programme, produces the same elements of partitions:
Size: 469.5 Bytes CkSum: # 44E7h Code: « 0 SWAP |
|||
02-09-2018, 05:43 AM
Post: #12
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
A further rationalisation of the programme, shorter & faster:
Size: 431. Bytes CkSum: # 8EABh Code: « 0 SWAP |
|||
02-10-2018, 12:47 AM
Post: #13
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
(02-05-2018 08:31 PM)Gerald H Wrote: Juan14 - What is the first symbol in line 7 of your programme? That symbol should be “≠”, sorry, something went wrong in the copy-paste process, it also returns zero when it finds a polindromic number. The program should have a size of 214.5 bytes and check-sum # FB55h. I tried to make it shorter but I couldn't. I don't dare to check those gigantic numbers you got. |
|||
02-10-2018, 06:12 AM
Post: #14
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Hello Juan14!
For even powers of 10 the programme works economically, eg for input 100 the programme returns 99 1 but for odd powers , eg input 1000 the programme returns 99 898 3 which is a correct partition but slightly extravagant. Are you planning to improve your programme? |
|||
02-10-2018, 06:16 AM
Post: #15
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
A much reduced version of my programme & faster:
Size: 300.5 Bytes CkSum: # 933Ch Code: « 0 SWAP |
|||
02-10-2018, 08:06 PM
Post: #16
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Hello, Gerald H, I modified the program, now it checks if the integer is a power of 10 at the beginning of the REPEAT portion of the program, with an IFTE command, the program is now 201 bytes long and the check-sum is # 480h
« WHILE DUP 9 > REPEAT DUP →STR SIZE OVER 1 - →STR SIZE == « DUP →STR DUPDUP SIZE 2 / SWAP 1 PICK3 SUB DUPDUP 1 5 PICK FLOOR SUB SREV + 4 ROLL OVER < { DROP OBJ→ 1 - →STR DUP 1 4 ROLL FLOOR SUB SREV + } { NIP NIP } IFTE OBJ→ SWAP OVER - » { 1 - 1 } IFTE END » |
|||
02-10-2018, 09:00 PM
Post: #17
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Very nice programme again, powers of ten now partitioned with 2 elements rather than 3.
The IFTE construction reduces the size of the programme but makes it slower than using IF THEN ELSE END. The programme still fails for larger, eg 6000 digit, numbers. I think the problem is in the part I have put in bold red below - I believe the comparison should be between the two numbers contained in the character strings rather than the character strings themselves. « WHILE DUP 9 > REPEAT DUP →STR SIZE OVER 1 - →STR SIZE == « DUP →STR DUPDUP SIZE 2 / SWAP 1 PICK3 SUB DUPDUP 1 5 PICK FLOOR SUB SREV + 4 ROLL OVER < { DROP OBJ→ 1 - →STR DUP 1 4 ROLL FLOOR SUB SREV + } { NIP NIP } IFTE OBJ→ SWAP OVER - » { 1 - 1 } IFTE END » |
|||
02-10-2018, 09:08 PM
Post: #18
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
For example
"11" "2" < returns 1. . |
|||
02-13-2018, 06:31 PM
Post: #19
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
(02-10-2018 06:16 AM)Gerald H Wrote: A much reduced version of my programme & faster: The version in post #12 is actually faster on my physical calculator and emulator. A neat program though, and surprisingly fast- less than 8 seconds for 32000-digit numbers on the physical calc! John |
|||
02-13-2018, 08:05 PM
Post: #20
|
|||
|
|||
RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers
Yes, you're right, P431 is faster than P300.5.
Would be pleased to read of improvements to the programme, I'm sure there are many possibilities. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)