Tripartite Palindromic Partition of Integer (HP 50g) Challenge
|
03-04-2023, 06:05 PM
(This post was last modified: 03-15-2023 07:43 PM by Albert Chan.)
Post: #44
|
|||
|
|||
RE: Tripartite Palindromic Partition of Integer (HP 50g) Challenge
(03-03-2023 10:57 AM)Gerald H Wrote: I believe ALGO5 is not necessary. It may not be necessary for this particular number, but we want to be safe in general. With center 0, it may lead to a palindrome with invalid center, which may be messy to fix. I like my simpler "palindrome" pair algorithm. At the center, there are less special cases to consider. If no carry, center cell is always vaild = (0 .. 9) - (0 .. 9) = (-9 .. 9) Even with carry, if (n-y) center is not 0 or 9, we are also safe: (1 .. 8) - (0 .. 9) ± 1 = (-9 .. 9) Only 2 cases may lead to problem: (n-y) - x = z + (non-zero carry) 0 - 9 = (−10) + 1 9 - 0 = (+10) − 1 To get into these situations are rare. Almost always, no backtracking fix needed. Here is an example that generated invalid center (±10), required backtracking to fix. NOTE: n is quoted number, but with center adjusted, to purposely get into trouble. n = 12267420150203532444 (20 digits) y = 10^19 - 1 (we always pick odd digits y) (n-y) also 19 digits, so we search for n = (x+y, both 19 digits) + (z, 18 digits) (*) Code: n-y 2 2 6 7 4 2 0 1 5 0 2 0 3 5 3 2 4 4 5 Except for center, x digits are generated from LHS, without carry, thus always valid. With (n-y) center = 0 AND (last z = 9) AND (carry = +1), x center is invalid. There is no need to backtrack a lot. All we needed is to avoid last z = 9. z center of 229 → 218 is enough, guaranteed valid x center. Code: n-y 2 2 6 7 4 2 0 1 5 0 2 0 3 5 3 2 4 4 5 12267420150203532444 = 9899550931390559989 + 2014000000000004102 + 353869218812968353 (*) We only switch to n = x + (y+z) if (n-y) > y (i.e. balance with more digits) Otherwise, we do n = (x+y) + z, pad with zeroes if necessary. Above example, n of 122... changed to 100... Code: 7998995386835998997 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)