Reversing an Integer's Digits
|
09-20-2022, 05:18 AM
Post: #1
|
|||
|
|||
Reversing an Integer's Digits
What Should I Add To Reverse the Digits?
Let A, B, C, D, and E be individual digits (0-9) of an integer. AB would represent a two digit integer with the value of 10 * A + B. ABC would represent a three digit integer with the value of 100 * A + 10 * B + C. Reversing a Two Digit Integer AB + # = BA 10 * A + B + # = 10 * B + A # = 9 * (B - A) Example: Let AB = 76. A = 7, B = 6 # = 9 * (6 - 7) = -9 76 - 9 = 67 Reversing a Three Digit Integer ABC + # = CBA 100 * A + 10* B + C + # = 100 * C + 10 * B + A # = 99 * (C - A) Example: ABC = 469 # = 99 * (9 - 4) = 495 469 + 495 = 964 Reversing a Four Digit Integer ABCD + # = DCBA 1000 * A + 100 * B + 10 * C + D + # = 1000 * D + 100 * C + 10 * B + A # = 999 * (D - A) + 90 * (C - B) Example: ABCD = 7219 # = 999 * (9 - 7) + 90 * (1 - 2) = 1908 7219 + 1908 = 9127 Reversing a Five Digit Integer ABCDE + # = EDBCA 10000 * A + 1000 * B + 100 * C + 10 * D + E + # = 10000 * E + 1000 * D + 100 * C + 10 * B + A # = 9999 * (E - A) + 990 * (D - B) Example: ABCDE = 52693 # = 9999 * (3 - 5) + 990 * (9 - 2) = -13068 52693 - 13068 = 39625 Having the Calculator Do It The program REVINT reverses the digits of an integer, up to 11 digits. The program does not allow numbers that have non-zero fractional parts or integers more than 11 digits. Instead of solving for # (see above), the program splits the integers into a list in reverse order, and uses list processing to get the final answer. HP Prime Program: REVINT Caution: Integers that end or begin with zero may not return accurate results. My suggestion is not use 0s with this program. See examples below for more details. Code: EXPORT REVINT(N) Examples: REVINT(4321) returns 1234 REVINT(56765) returns 56765 (56765 is a palindrome, reversing the digits results in the same number) REVINT(42910) returns 1924 (01924 - be aware about integers ending or beginning with 0) REVINT(67.28) returns "NOT AN INTEGER" (error) |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Reversing an Integer's Digits - Eddie W. Shore - 09-20-2022 05:18 AM
RE: Reversing an Integer's Digits - Didier Lachieze - 09-20-2022, 06:01 AM
|
User(s) browsing this thread: 1 Guest(s)