Showing posts with label integers. Show all posts
Showing posts with label integers. Show all posts

Saturday, August 9, 2025

RPN: HP 15C and DM32: Integer Length and Digit Extraction

RPN: HP 15C and DM32: Integer Length and Digit Extraction (up to 99,999,999)



The following programs are utilities for positive integers:


(1) Integer Length: how many digits in a positive integer

(2) Digit Extraction: Get the nth digit of a positive integer


Note:  Do not use more than 8 digits (integers up to 99,999,999).   Beyond this, the program may return inaccurate answers.  

Thanks to J-F Garnier for pointing out the limitation of the programs.  


Integer Length


Find the number of digits of a positive integer. The integer is decimal base (base 10).


For a positive integer n, the number of the digits can be easily found by the formula:


L = int(log(x)) + 1



DM32, HP 32S, HP 32SII Code


L01 LBL L

L02 LOG

L03 IP

L04 1

L05 +

L06 RTN


HP 15C, DM15 Code


001

42, 21, 11

LBL A

002

43, 13

LOG

003

43, 44

INT

004

1

1

005

40

+

006

43, 32

RTN


The program finds the number of digits in the positive integer in the X stack.


Examples:


X = 436782; Length: 6

X = 5195008; Length: 7

X = 23156956; Length: 8


Digit Extraction


Extract the nth digit of a positive integer. Digit positions go from left to right. For example: for the integer 4582, the 1st digit is 4, the 2nd digit is 5, 3rd digit is 8, and 4th digit is 2.


Steps that this program follows:

Step 1: Find the length of the positive integer. L = int(log(x)) + 1.

Step 2: Divide X by 10^(L – n + 1). D = X / (10^(L – n + 1))

Step 3: Extract the fractional part. D = frac(D)

Step 4: Multiple the result by 10 and extract the integer part. D = int(10 * D)


Setting up the stack:

Y: integer

X: nth digit to extract


Variables Used:

DM32, HP 32S, HP 32S II

HP 15C, DM15

X

R1

N

R2

L

R3



DM32, HP 32S, HP 32SII Code


E01 LBL E

E02 STO N

E03 R↓

E04 STO X

E05 LOG

E06 IP

E07 1

E08 +

E09 STO L

E10 RCL X

E11 RCL L

E12 RCL- N

E13 1

E14 +

E15 10^x

E16 ÷

E17 FP

E18 10

E19 ×

E20 IP

E21 RTN


HP 15C, DM15 Code


001

42, 21, 12

LBL B

002

44, 2

STO 2

003

33

R↓

004

44, 1

STO 1

005

43, 13

LOG

006

43, 44

INT

007

1

1

008

40

+

009

44, 3

STO 3

010

45, 1

RCL 1

011

45, 3

RCL 3

012

45, 30, 2

RCL- 2

013

1

1

014

40

+

015

13

10^x

016

10

÷

017

42, 44

FRAC

018

1

1

019

0

0

020

20

×

021

43, 44

INT

022

43, 32

RTN


Examples


Integers

Length

2nd Digit

4th Digit

5th Digit

436782

6

3

7

8

5195008

7

1

5

0

23156956

8

3

5

6



Eddie


All original content copyright, © 2011-2025. 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.


All posts are 100% generated by human effort.  The author does not use AI engines and never will.


Sunday, May 26, 2024

TI 30Xa Algorithms: Greatest Common Divisor

TI 30Xa Algorithms: Greatest Common Divisor


To find the greatest common divisor between two positive integers U and V:


Let U ≥ V. Let U = A * V + R where A is the quotient of U / V and R is the remainder. If R≠0, then V becomes the new U and R become the new V. The process repeats until R=0. At that point the value of V prior to the last calculation is the greatest common divisor (GCD) of U and V.


Example:

gcd(166, 78)

U = 166, V = 78


Algorithm Loop:

  1. A = int(U / V)

  2. R = U – V * int(U / V)



A

R

U

V

Start

n/a

n/a

166

78

A = int(166 / 78) = 2

R = 166 – 2 * 78 = 10

2

10

78

10

A = int(78 / 10) = 7,

R = 78 – 7 * 10 = 8

7

8

10

8

A = int(10 / 8) = 1

R = 10 – 1 * 8 = 2

1

2

8

2

A = int(8 / 2) = 4

R = 8 – 4 * 2 = 0

4

0 *STOP*





Procedure


  1. Store the greater of the two numbers in memory register 1: [ STO ] [ 1 ].

  2. Store the lesser of the two numbers in memory register 2: [ STO ] [ 2 ].

  3. Divide memory register 1 by memory register 2. Store the integer part (no fractional part) in memory register 3: [ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ], (integer part) [ STO ] [ 3 ]

  4. Figure the remainder and store the result in memory 3: [ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ] [ STO ] [ 3 ]

  5. If the remainder is 0, stop. The GCD is stored in memory 2.

  6. If the remainder is non-zero, then store memory 2 into memory 1 then memory 3 into memory 2. You need to do it in this order. [ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]. Go back to Step 3 and repeat.


Examples


Example 1: GCD(26, 14)

M1 = 26, M2 = 14



M1

M2

M3

26 [ STO ] [ 1 ], 14 [ STO ] [ 2 ]

26

14


[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.857142857

1 [ STO ] [ 3 ]

26

14

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 12

[ STO ] [ 3 ]

R is not zero, so we continue.

26

14

12

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

14

12

12

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.166666667

1 [ STO ] [ 3 ]

14

12

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 2

[ STO ] [ 3 ]

R is not zero, so we continue.

14

12

2

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

12

2

2

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 6

6 [ STO ] [ 3 ]

12

2

6

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 0

[ STO ] [ 3 ]

R is zero, so we stop.

GCD: [ RCL ] [ 2 ]: GCD(26, 14) = 2

12

2

0



Example 2: GCD(27, 15)

M1 = 27, M2 = 15




M1

M2

M3

27 [ STO ] [ 1 ], 15 [ STO ] [ 2 ]

27

15


[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.8

1 [ STO ] [ 3 ]

27

15

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 12

[ STO ] [ 3 ]

R is not zero, so we continue.

27

15

12

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

15

12

12

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 1.25

1 [ STO ] [ 3 ]

15

12

1

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 3

[ STO ] [ 3 ]

R is not zero, so we continue.

15

12

3

[ RCL ] [ 2 ] [ STO ] [ 1 ], [ RCL ] [ 3 ] [ STO ] [ 2 ]

12

3

3

[ RCL ] [ 1 ] [ ÷ ] [ RCL ] [ 2 ] [ = ]

Result: 4

4 [ STO ] [ 3 ]

12

3

4

[ RCL ] [ 1 ] [ - ] [ RCL ] [ 2 ] [ × ] [ RCL ] [ 3 ] [ = ]

Result: 0

[ STO ] [ 3 ]

R is zero, so we stop.

GCD: [ RCL ] [ 2 ]: GCD(27, 15) = 3

12

3

0



I hope you find this useful. What I hope to do with the monthly series is to demonstrate various calculations with the TI-30Xa.


Note: For June and July 2024, I will be posting on Saturdays only. I plan to resume the Saturday-Sunday schedule in August.



Eddie


All original content copyright, © 2011-2024. 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.

Basic vs. Python: Circle Inscribed in Circle [HP 71B, Casio fx-CG 100]

Basic vs. Python: Circle Inscribed in Circle Calculators Used: Basic: HP 71B Python: Casio fx-CG 100 Introduction ...