Saturday, November 12, 2022

HP Prime: Reversing an Integer's Digits

HP Prime:  Reversing an Integer's Digits


(Inspired by the HHC 2022 programming contest)


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.  


EXPORT REVINT(N)

BEGIN

// 2022-09-18 EWS

// reverse the integer N

// up to 12 digits

LOCAL D,P,A,I,M,L;

L:={};

P:=XPON(N);


// check size 

  IF P>11 THEN

  RETURN "TOO BIG";

  KILL;

  END;

 

// check type

  IF FP(N) THEN

  RETURN "NOT AN INTEGER";

  KILL;

  END;

   

D:=N;


// loop

  FOR I FROM P DOWNTO 0 DO

  A:=D/ALOG(I);

  L:=CONCAT({IP(A)},L);

  D:=D-IP(A)*ALOG(I); 

  END;

  

// rebuild 

M:=ΣLIST(MAKELIST(ALOG(X),X,P,0,−1)*L);

RETURN M; 

END;


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)



Eddie


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Sum of Sequential Integers (featuring Swiss Micros DM32)

  Sum of Sequential Integers ( featuring Swiss Micros DM32) What is the sum of the following: 100 + 101 + 102 + 103 + 104 + 105 + … + ...