# GCD and LCM [fx-5800P]

3 replies to this topic

### #1 slugrustle

slugrustle

Newbie

• Members
• 8 posts
• Gender:Not Telling

• Calculators:
fx-5800P
fx-9860GII SD

Posted 24 November 2018 - 05:15 PM

Both programs below prompt the user for at least 2 input numbers and accept up to 27 inputs. After calculating, they display the result on the first line of the display and the input arguments on the next three lines of the display. You can scroll through the inputs if there are more than 3 of them.

I wrote these programs after finding a neat application for least common factor in my day job writing microcontroller firmware. I also put them on GitHub along with .pdf versions of the program listings. See https://slugrustle.github.io/

Greatest Common Divisor / Greatest Common Factor

```0➔DimZ:
27➔DimZ:
0➔A:
”ENTER -1 AFTER  LAST INPUT”:
While 1:
”NUMBER”?➔B:
B=-1⇒Break:
If B≠Int(B):
Then Cls:
”NUMBER MUST BE  AN INTEGER”:
Stop:
IfEnd:
If B<1 Or B≥1ₓ₁₀10:
Then Cls:
”NUMBER MUST BE  >0 And <1ₓ₁₀10”:
Stop:
IfEnd:
A+1➔A:
If A≤27:
Then B➔Z[A]:
Else Cls:
”SUPPORTS AT MOST27 NUMBERS”:
Stop:
IfEnd:
WhileEnd:
If A<2:
Then Cls:
”REQUIRES 2 OR   MORE NUMBERS”:
Stop:
IfEnd:
A➔D:
Z[D]➔B:
For D−1➔A To 1 Step -1:
Z[A]➔C:
While B≠C:
If B≥C:
Then B−C×Int(B÷C)➔B:
B=0⇒C➔B:
Else C−B×Int(C÷B)➔C:
C=0⇒B➔C:
IfEnd:
WhileEnd:
Next:
1➔C:
Int(D÷3)➔E:
D−3×E>0⇒E+1➔E:
Lbl 1:
Cls:
Locate 1,1,B:
Locate 12,1,C:
Locate 13,1,”:”:
Locate 14,1,E:
3×(C−1)+1➔A:
Locate 1,2,Z[A]:
A+1≤D⇒Locate 1,3,Z[A+1]:
A+2≤D⇒Locate 1,4,Z[A+2]:
While 1:
Getkey➔F:
If F=34 Or F=73:
Then Cls:
”DONE”:
Stop:
IfEnd:
If F=77 Or F=84 Or F=86 Or F=47:
Then C+1➔C:
C>E⇒1➔C:
Goto 1:
IfEnd:
If F=67 Or F=83 Or F=85:
Then C−1➔C:
C<1⇒E➔C:
Goto 1:
IfEnd:
WhileEnd
```

Program Outline

• 0➔DimZ:27➔DimZ: Set up memory for extra variables Z[α] where α∈[1,27].
• 0➔A: through A➔D: User input of arguments for GCD(Z[1], ..., Z[D]). D∈[2,27].
• Z[D]➔B: through Next: Evaluate B = GCD(Z[1],   ..., Z[D]). Uses GCD(β,γ,δ,ε) = GCD(GCD(GCD(β,γ),δ),ε).
• 1➔C: to end of program: Display result and inputs.

Variable Descriptions
A: Index into extra variable memory.
B: User input and GCD evaluation.
C: GCD evaluation and number of displayed input argument page.
D: Number of input arguments.
E: Number of input argument display pages (3 inputs per page).
F: Identifier of most recently pressed key.

Notes

• ”ENTER...":, "NUMBER...":, "SUPPORTS...":, "REQUIRES...": The weird spacing prevents text wrapping from occurring in the middle of a word.
• If B<1 Or B≥1ₓ₁₀10: The fx-5800P can only represent numbers on the range [−1ₓ₁₀10,1ₓ₁₀10] as exact integers.
• If F=34 Or F=73: Pressing DEL (34) or EXIT (73) ends the program.
• If F=77 Or F=84 Or F=86 Or F=47: Pressing + (77), ▲ (84), ▶ (86), or EXE (47) cycles to the next input argument display page.
• If F=67 Or F=83 Or F=85: Pressing − (67), ◀ (83), or ▼ (85) cycles to the previous input argument display page.

Least Common Multiple / Least Common Factor

```0➔DimZ:
27➔DimZ:
0➔A:
”ENTER -1 AFTER LAST INPUT”:
While 1:
”NUMBER”?➔B:
B=-1⇒Break:
If B≠Int(B):
Then Cls:
”NUMBER MUST BE AN INTEGER”:
Stop:
IfEnd:
If B<1 Or B≥1ₓ₁₀10:
Then Cls:
”NUMBER MUST BE >0 And <1ₓ₁₀10”:
Stop:
IfEnd:
A+1➔A:
If A≤27:
Then B➔Z[A]:
Else Cls:
”SUPPORTS AT MOST27 NUMBERS”:
Stop:
IfEnd:
WhileEnd:
If A<2:
Then Cls:
”REQUIRES 2 OR MORE NUMBERS”:
Stop:
IfEnd:
A➔D:
Z[D]➔B:
For D−1➔A To 1 Step -1:
B➔E:
Z[A]➔C:
While B≠C:
If B≥C:
Then B−C×Int(B÷C)➔B:
B=0⇒C➔B:
Else C−B×Int(C÷B)➔C:
C=0⇒B➔C:
IfEnd:
WhileEnd:
If E≥Z[A]:
Then (E÷B)×Z[A]➔B:
Else (Z[A]÷B)×E➔B:
IfEnd:
If B≥1ₓ₁₀10:
Then Cls:
”OVERFLOW”:
Stop:
IfEnd:
Next:
Int(D÷3)➔E:
D−3×E>0⇒E+1➔E:
1➔C:
Lbl 1:
Cls:
Locate 1,1,B:
Locate 12,1,C:
Locate 13,1,”:”:
Locate 14,1,E:
3×(C−1)+1➔A:
Locate 1,2,Z[A]:
A+1≤D ⇒ Locate 1,3,Z[A+1]:
A+2≤D ⇒ Locate 1,4,Z[A+2]:
While 1:
Getkey➔F:
If F=34 Or F=73:
Then Cls:
”DONE”:
Stop:
IfEnd:
If F=84 Or F=86 Or F=77 Or F=47:
Then C+1➔C:
C>E⇒1➔C:
Goto 1:
IfEnd:
If F=83 Or F=85 Or F=67:
Then C−1➔C:
C<1⇒E➔C:
Goto 1:
IfEnd:
WhileEnd
```

Program Outline

• 0➔DimZ:27➔DimZ: Set up memory for extra variables Z[α] where α∈[1,27].
• 0➔A: through A➔D: User input of arguments for LCM(Z[1], ..., Z[D]). D∈[2,27].
• Z[D]➔B: through Next: Evaluate B = LCM(Z[1], ..., Z[D]). Uses LCM(β,γ) = (β×γ)÷GCD(β,γ) = (β÷GCD(β,γ))×γ = (γ÷GCD(β,γ))×β and LCM(β,γ,δ,ε) = LCM(LCM(LCM(β,γ),δ),ε).
• 1➔C: to end of program: Display result and inputs.

Variable Descriptions

A: Index into extra variable memory.
B: User input and LCM evaluation.
C: LCM evaluation and number of displayed input argument page.
D: Number of input arguments.
E: LCM evaluation and number of input argument display pages (3 inputs per page).
F: Identifier of most recently pressed key.

Notes

• ”ENTER...":, "NUMBER...":, "SUPPORTS...":, "REQUIRES...": The weird spacing prevents text wrapping from occurring in the middle of a word.
• If B<1 Or B≥1ₓ₁₀10:, If B≥1ₓ₁₀10: The fx-5800P can only represent numbers on the range [−1ₓ₁₀10,1ₓ₁₀10] as exact integers.
• If F=34 Or F=73: Pressing DEL (34) or EXIT (73) ends the program.
• If F=84 Or F=86 Or F=77 Or F=47: Pressing ▲ (84), ▶ (86), + (77), or EXE (47) cycles to the next input argument display page.
• If F=83 Or F=85 Or F=67: Pressing ◀ (83), ▼ (85), or − (67) cycles to the previous input argument display page.

### #2 slugrustle

slugrustle

Newbie

• Members
• 8 posts
• Gender:Not Telling

• Calculators:
fx-5800P
fx-9860GII SD

Posted 12 December 2018 - 04:50 AM

I have also added an integer factorization program called FACTOR, which is described in a reply to the thread https://community.ca...casio-fx-5800p/

CCE and CCL files for GCD, LCM, and FACTOR are available at https://slugrustle.github.io/ and https://github.com/s.../fx-5800P_progs. These files can be used with with Takumako's CcEditor, CcLinker, and USB Dongle, about which see https://cclinker.web.fc2.com/

### #3 Tritonio

Tritonio

• Members
• 63 posts
• Gender:Male

• Calculators:
FX-5800P, FX-991ES+, FX-991EX, HP Prime

Posted 10 September 2019 - 02:14 PM

Oh this is a fast version that uses the Wheeler factorization or whatever it's called? I went ahead and made some slow but simple LCM GCD progs but I could have typed these in instead.

### #4 CalcLoverHK

CalcLoverHK

Casio Freak

• Members
• 244 posts
• Gender:Male
• Location:Hong Kong
• Interests:Playing video games (Minecraft, krunker.io), calculators, programming in Casio Basic/C.Basic, Scratch (formal), IT research (sometimes)

• Calculators:
fx-50FHII (2019/1/30)
fx-9750GIII (2020/6/13)

Posted 10 September 2019 - 02:17 PM

Oh, I forgot to tell you this post. I have looked a bit only

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users