Find LCM of 2 numbers without using GCD
08-01-2018, 08:15 PM (This post was last modified: 08-01-2018 08:22 PM by Dieter.)
Post: #21
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Find LCM of 2 numbers without using GCD
Albert Chan wrote:
Quote:Sorry if it look stupid ... This is my first HP-12C program (only HP calc I own)

Register Y X --> X Mod(Y, X)

Keep pressing R/S, eventually it display 0, previous Mod was Gcd (Rotate key to get it back)

Great – I love stack-only programs. ;-)

For the record: here's a complete program that directly returns the GCD. The initial lines have been optimized.

Code:
01 ENTER 02 ENTER 03 CLX 04 + 05 R↓ 06 ÷ 07 LstX 08 X<>Y 09 INTG 10 x 11 - 12 X=0? 13 GTO 15 14 GTO 01 15 + 16 GTO 00

Dieter
08-01-2018, 09:04 PM (This post was last modified: 08-01-2018 11:28 PM by Albert Chan.)
Post: #22
 Albert Chan Senior Member Posts: 768 Joined: Jul 2018
RE: Find LCM of 2 numbers without using GCD
uh, did it miss a subtract on line 3 ? looks like Y pushed away

when X and Y are both negative, the mod will stay negative all the way to the end

I still like the mod, and would like program to pull double duty.
How about it only does the loop only if X was negative ?

so, this will get the gcd

54321 CHS Enter 12345 CHS R/S --> -3

that will be awesome ...

Edit: my mistake, it was CLX not LstX. That was a nice trick ...
08-02-2018, 01:25 AM (This post was last modified: 08-02-2018 06:58 PM by Albert Chan.)
Post: #23
 Albert Chan Senior Member Posts: 768 Joined: Jul 2018
RE: Find LCM of 2 numbers without using GCD
Here is my revised Mod/Gcd program for HP-12C

If Y, X both negative, it does Gcd(Y, X)
If Y, X both positive, it does Mod(Y, X)
If X = 0, return Y

Example:

54321 CHS Enter 12345 CHS
R/S => -3 ; Gcd

54321 Enter 12345
R/S => 4941 ; Mod(54321, 12345)
R/S => 2463 ; Mod(12345, 4941)
R/S => 15 ; Mod(4941, 2463)
R/S => 3 ; Mod(2463, 15)
R/S => 0 ; Mod(15, 3)
R/S => 3 ; Gcd

Code:
01 X = 0 02 GOTO 17 03 ENTER 04 ENTER 05 CLX 06 + 07 R↓ 08 / 09 Lstx 10 X<>Y 11 INTG 12 * 13 - 14 X <= Y    ; Mod ? 15 GOTO 00 16 GOTO 01 17 + 18 GOTO 00
08-02-2018, 01:03 PM
Post: #24
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Find LCM of 2 numbers without using GCD
Albert Chan wrote:
Quote:Here is my revised Mod/Gcd program for HP-12C

Fine, so this can be used for calculating the GCD as well as a simple MOD.

However, I'd suggest you use the common function names in your listings:
• "clear" is ambiguous. The 12C has CLEAR Σ, CLEAR PROGRAM, CLEAR FIN and CLEAR REG commands. You probably mean the CLX function here.
• "swap" may be clear to RPL-users, but not every 12C user may realize that this refers to the X<>Y key.
• "rotate" sounds like a rather obscure term for the "roll down" function, i.e. the R↓ key. If you can't insert an arrow, "Rv" is a common solution.
• Finally, the 12C has an "INT" key, but that's not what is meant here. The integer function is called INTG.
Sticking to standards usually is not a bad idea. ;-)

Dieter
08-02-2018, 01:26 PM
Post: #25
 Albert Chan Senior Member Posts: 768 Joined: Jul 2018
RE: Find LCM of 2 numbers without using GCD
Hi, Dieter.

BTW, I was trying to use your code as template, but this forum Quote not working.

Where can I learn how to add non-ascii characters in post ?
08-02-2018, 03:52 PM
Post: #26
 Thomas Okken Senior Member Posts: 923 Joined: Feb 2014
RE: Find LCM of 2 numbers without using GCD
(08-02-2018 01:26 PM)Albert Chan Wrote:  BTW, I was trying to use your code as template, but this forum Quote not working.

What do you mean? When I press the Quote button below a post, a Post a New Reply window opens, with the original post surrounded by quote and /quote tags. Does it behave differently in your browser?

(08-02-2018 01:26 PM)Albert Chan Wrote:  Where can I learn how to add non-ascii characters in post ?

My preferred way is to type the characters in Free42 (i.e. enter them into the alpha register using the ALPHA menu) and then use Copy and Paste to copy them to the Post text box. Free42 and the Forum are both support Unicode, so that should work smoothly, but of course it is limited to the HP-42S character set. For really unusual characters, I google them by name and then use the first hit from http://www.fileformat.info, and copy and paste from there. For example, google "unicode black right pointing triangle" gets you the hit https://www.fileformat.info/info/unicode.../index.htm; scroll down to the Java Data section, and you can copy the character from string.toUpperCase() or string.toLowerCase().
08-02-2018, 04:35 PM
Post: #27
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Find LCM of 2 numbers without using GCD
(08-02-2018 03:52 PM)Thomas Okken Wrote:
(08-02-2018 01:26 PM)Albert Chan Wrote:  BTW, I was trying to use your code as template, but this forum Quote not working.

What do you mean? When I press the Quote button below a post, a Post a New Reply window opens, with the original post surrounded by quote and /quote tags. Does it behave differently in your browser?

There was a problem with the "Quote" button and other forum functions. Obviously this has been fixed now (around noon UTC today it still was not working). Take a look at the "Forum Issues and Administration" forum for more details.

(08-02-2018 03:52 PM)Thomas Okken Wrote:
(08-02-2018 01:26 PM)Albert Chan Wrote:  Where can I learn how to add non-ascii characters in post ?

My preferred way is to type the characters in Free42 (i.e. enter them into the alpha register using the ALPHA menu) and then use Copy and Paste to copy them to the Post text box.

My method is a bit simpler: I have a textfile here that includes all the special characters that I often use: arrows, mathematical symbols, greek characters and some more. To prepare such a file I simply copied these characters from the Windows "charmap" utility.

Service:
here are some characters that you may directly copy into a textfile for later use.

Code:
± × ÷ − ≈ ≠ ≤ ≥ ∞ ∫ ½ ¼ ¹ ² ³ √ ← ↑ → ↓ ↔ ↕ ⇄ ├ … α β γ δ ε λ π σ Γ Δ Σ

Dieter
08-02-2018, 05:48 PM
Post: #28
 Albert Chan Senior Member Posts: 768 Joined: Jul 2018
RE: Find LCM of 2 numbers without using GCD
Thanks Thomas Okken,

Quote is working now :-)

Thanks Dieter,

charmap chars helps, as I am typing from an ipad.

just discovered ipad keyboard have hidden gems, if I press the key a bit longer
this pops out when a is pressed: æ ã å ā à á â ä
08-02-2018, 05:54 PM
Post: #29
 Thomas Okken Senior Member Posts: 923 Joined: Feb 2014
RE: Find LCM of 2 numbers without using GCD
(08-02-2018 05:48 PM)Albert Chan Wrote:  [...] typing from an ipad.

just discovered ipad keyboard have hidden gems, if I press the key a bit longer
this pops out when a is pressed: æ ã å ā à á â ä

You can also add additional keyboards, under Settings -> General -> Keyboard -> Keyboards. I keep a Greek keyboard in there, so I can type Greek letters in mathematical equations without having to copy and paste. (If you have multiple keyboards selected, you can switch between them by tapping or long-pressing the "globe" key left of the space bar.)
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)