lcm program on hp 33s



#6

does anybody know a program for finding the lcm of two numbers on the hp33s (or hp 32s or 32sii)?


#7

Hi Kevin,
I just banged out this very simple, brute force program for a 33s that seems to work. Enter yor two arguments into the X and Y stack registers and XEQ L. The LCM will show in the display when finished. The bigger the arguments and the higher the LCM, the longer it takes. I put in 5000 and 5002 and it took about a munute to return an lcm of 12505000. 50 and 57 took 3 seconds to return 2850. Best regards, Hal

The program:

lblL

x>y?

x<>y

stoA

x<>y

stoB

2

stoC

lblO

rclC

rclB

*

rclA

/

FP (fractional part)

x=0?

gtoQ

1

sto+C

gtoO

lblQ

rclC

rclB

*

rtn


#8

Actually the program I submitted in the posting above was predicated on the assumption that the LCM has to be at least twice the larger argument, which is actaully not true (my bad)...the LCM could be the larger argument itself. Easy fix...just change line seven to 1.


Hal :)

#9

Here are LCM and GCD routines for the 32SII/33S that I came up with. Note that LCM(m,n)= m x n / GCD(m,n) and LCM is computed here using Euclid's method. It's really fast, worst case is on the order of log base 2 of the smaller of m, n.

LCM: call with m,n in x,y return with LCM in x
uses: A, B, x,y,z,t

L01 LBL L
L02 STO A
L03 x<>y
L04 STOx A
L05 XEQ G
L06 RCL A
L07 x<>y
L08 /
L09 RTN

GCD: call with m,n in x,y return with GCD in x
uses: B, x,y,z,t

G01 LBL G
G02 x>y?
G03 x<>y
G04 STO B
G05 0 | these 7 instructions are just
G06 ENTER | to compute y mod x. On a 33S
G07 CMPLX+ | You can replace these with
G08 / | the single instruction:
G09 IP | RMDR
G10 x |
G11 - |
G12 RCL B
G13 x<>y
G14 x>0?
G15 GTO G
G16 RCL B
G17 RTN


#10

Hi, Katie:

In your 1st routine, namely:

      L01 LBL L
L02 STO A
L03 x<>y
L04 STOx A
L05 XEQ G
L06 RCL A
L07 x<>y
L08 /
L09 RTN
you can save an additional step by replacing the final
      RCL A, X<>Y, / 
by the simpler:
      L06 STO/ A
L07 X<> A
which is one step shorter, slightly faster, and leaves the GCD in A and the LCM in X. Or you can simply use
      L07 RCL A
instead of
      L07 X<> A
to get the GCD in Y and the LCM in X, also one step shorter.

Best regards from V.


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP Prime: run a program in another program Davi Ribeiro de Oliveira 6 330 11-11-2013, 08:28 PM
Last Post: Davi Ribeiro de Oliveira
  Maximum number of program steps in HP-42S, 33S, and 35S? Walter B 3 247 12-18-2012, 03:44 PM
Last Post: Eric Smith
  Program Coding Sheets for 33s/35s Matt Agajanian 6 229 03-03-2012, 04:03 PM
Last Post: David Hayden
  HP-41 MCODE: Making an MCODE program call another MCODE program Geir Isene 10 433 01-13-2008, 05:58 AM
Last Post: Raymond Del Tondo
  HP 33s: Addressing registers at the "Top" of the program list. Ted Madson 0 82 10-23-2007, 05:11 PM
Last Post: Ted Madson
  Program format from 33s to 35s romeo_charlie 10 332 09-16-2007, 05:02 PM
Last Post: Paul Dale
  17b2+ solver and the GCD/LCM formula (Don?) Gene Wright 25 678 08-10-2007, 12:50 PM
Last Post: Vincze
  Program Loss in HP-33S Ed Look 3 171 04-09-2007, 10:21 PM
Last Post: Ed Look
  33s: parentheses within program Adam Price 9 318 02-24-2007, 09:13 PM
Last Post: bill platt
  Integration Times "Old" 33s vs "New" 33s John Smitherman 21 788 12-14-2005, 12:04 AM
Last Post: Karl Schneider

Forum Jump: