Showing posts with label prime factorization. Show all posts
Showing posts with label prime factorization. Show all posts

Saturday, December 9, 2023

Swiss Micros DM32 and Casio fx-4000P: Prime Factorization

Swiss Micros DM32 and Casio fx-4000P:   Prime Factorization



Introduction 



On March 22, 2017, I posted a prime factorization program for the HP 15C:


https://edspi31415.blogspot.com/2017/03/hp-15c-prime-factorization.html



Today's blog is a port for the:


* Swiss Micros DM32 (and HP 32SII family)

* Casio fx-4000P



Swiss Micros DM32 Code:  Prime Factorization



Instructions:


1.  Enter a positive integer on the X stack. 

2.  Run the program (XEQ P) and a prime factor is displayed.  Press [ R/S ] to continue.  At this point the calculator is set to FIX 0 mode.   Repeat step 2.

3.  The program ends when the integer reappears.  The calculator is reset to FIX 4.  


3 labels are needed.



P01  LBL P

P02  FIX 0

P03  STO B

P04  STO A

P05  2

P06  STO F


B01  LBL B

B02  RCL A

B03  RCL÷ F

B04  ENTER

B05  FP

B06  x=0?

B07  GTO A

B08  1

B09  STO+ F

B10  GTO B


A01  LBL A

A02  RCL F

A03  STOP

A04  R↓

A05  R↓

A06  STO A

A07  1

A08  -

A09  x≠0?

A10 GTO B

A11 RCL B

A12 FIX 4

A13 RTN



Labels used:  P, B, A

Variables:  F (factor), A, B





Casio fx-4000P Code: Prime Factorization



Instructions:


1.  Run program (P#) in RUN Mode (Mode 1).  

2.  Enter a positive integer at the "N?" prompt.  

3.  A prime factor is displayed.  Press [ EXE] to continue. Repeat step 3.

4.  The program ends when the integer reappears.  



"N" : ? → N : N → T : 2 → F : 

Lbl 3 : Frac( T ÷ F ) = 0 ⇒ Goto 2 :

F + 1 → F : Goto 3 :

Lbl 2 : F ◢ T ÷ F → T : T - 1 ≠ 0 ⇒ Goto 3 : N


Variables:  N, T, F  




Examples


26:  2, 13, 26   (26 = 2 × 13)


89:  89   (89 is prime)


175:  5, 5, 7, 175   (175 = 5^2 × 7)


1020:  2, 2, 3, 5, 17, 1020  (1020 = 2^2 × 3 × 5 × 17)




Eddie


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


Wednesday, March 22, 2017

HP 15C: Prime Factorization



HP 15C: Prime Factorization

This program does exactly this: factors an integer.  Fix 0 mode is activated during execution.  Each factor is displayed by pressing [R/S].  The calculator is returned to Fix 4 mode when the program is completed.  If the integer is a prime number, the program just returns the integer entered.  

Step
Key
Code
001
LBL B
42, 21, 22
002
FIX 0
42, 7, 0
003
STO 2
44, 2
004
STO 0
44, 0
005
2
2
006
STO 1
44, 1
007
LBL 3
42, 21, 3
008
RCL 0
45, 0
009
RCL÷ 1
45, 10, 1
010
ENTER
36
011
FRAC
42, 44
012
X=0
43, 20
013
GTO 2
22, 2
014
1
1
015
STO+ 1
44, 40, 1
016
GTO 3
22, 3
017
LBL 2
42, 21, 2
018
RCL 1
45, 1
019
R/S
31
020
R↓
33
021
R↓
33
022
STO 0
44, 0
023
1
1
024
-
30
025
X≠0
43, 30, 0  (TEST 0)
026
GTO 3
22, 3
027*
RCL 2
45, 2
028
FIX 4
42, 7, 4 
029
RTN
43, 32

Example:  150.  Factors:  2, 3, 5, 5 (when the display reads 150.0000 the factorization ends)
 
* Thanks to Ross Barnes to pointing the typo to me.  The key is now correct.  - Eddie 11/13/2021

This blog is property of Edward Shore, 2017.

Saturday, August 20, 2016

HP 12C: Heron’s Formula, Ulam’s Conjecture, and Prime Factorization

HP 12C:  Heron’s Formula, Ulam’s Conjecture, and Prime Factorization

The following programs have been ported to the HP 12C from various programs presented in the 1980 book, “Programmable Pocket Calculators” by Henry Mullish and Stephen Kochan.  Please see the reference at the end of this entry.

HP 12C:  Heron’s Formula

Heron’s Formula calculates the area of a triangle with side measurements a, b, and c.  

Area = √( s * (s-a) *(s-b) *(s-c)), with s = (a + b + c)/2

Based on the original program for HP 67 (pg. 170 of Source)

STEP
CODE
KEY
01
44, 1
STO 1
02
31
R/S
03
44, 2
STO 2
04
31
R/S
05
44, 3
STO 3
06
45, 1
RCL 1
07
40
+
08
45, 2
RCL 2
09
40
+
10
2
2
11
10
÷
12
44, 4
STO 4
13
45, 1
RCL 1
14
30
-
15
45, 4
RCL 4
16
20
*
17
45, 4
RCL 4
18
45, 2
RCL 2
19
30
-
20
20
*
21
45, 4
RCL 4
22
45, 3
RCL 3
23
30
-
24
20
*
25
43, 21
26
43, 33, 00
GTO 00

Instructions:

Input a, press [R/S], input b, press [R/S], input c, [R/S].  The area will be calculated.

Test 1:  a = 4.00, b = 5.00, c = 3.00  Result:  6.00

Test 2:  a = 10.00, b= 15.00, c = 20.00   Result:  72.62
Test 3:  a = 7.00, b = 8.00, c = 9.00  Result:  26.83

HP 12C:  Ulam’s Conjecture

This program counts the many steps needed to reduce a positive integer to 1, by the following rules:

If the integer is even, divide by 2.
If the integer is odd, multiply it by 3 and add 1.
If the next integer is 1, stop.

Based on the original program for HP 33E (pg. 239 of Source)

STEP
CODE
KEY
01
42, 0
FIX 0 ( [ f ], 0)
02
44, 1
STO 1
03
0
0
04
44, 0
STO 0
05
1
1
06
45, 1
RCL 1
07
30
-
08
43, 35
x=0
09
43, 33, 28
GTO 28
10
45, 1
RCL 1
11
2
2
12
10
÷
13
43, 24
FRAC
14
43, 25
x=0
15
43, 33, 32
GTO 22
16
45, 1
RCL 1
17
3
3
18
20
*
19
1
1
20
40
+
21
43, 33, 23
GTO 23
22
43, 36
LSTx
23
43, 31
PSE
24
44, 1
STO 1
25
1
1
26
44, 40, 0
STO+ 0
27
43, 33, 05
GTO 05
28
45, 0
RCL 0
29
42, 2
FIX 2 ( [ f ], 2)
30
43, 33, 00
GTO 00

Instructions:

Enter the integer, press [R/S].  The number of steps is calculated.
Test 1:  5 takes 5 steps to get to 1 through Ulam’s Conjecture.
Test 2:  21 takes 7 steps to get to 1 through Ulam’s Conjecture.
Test 3: 39 takes 34 steps to get to 1 through Ulam’s Conjecture.

HP 12C Prime Factorization

This is the prime factorization of a prime integer.  The program ends when the original integer is returned, and FIX 2 mode is set.  Another change I made was instead of the pause command in the original program, I made it a run/stop command so that you write down the prime factors at your leisure. 

Based on the original program for HP 25/25C (pg. 77 of Source)

STEP
CODE
KEY
01
42, 0
FIX 0  ( [ f ], 0 )
02
44, 2
STO 2
03
44, 0
STO 0
04
2
2
05
44, 1
STO 1
06
45, 0
RCL 0
07
45 ,1
RCL 1
08
10
÷
09
36
ENTER
10
43, 24
FRAC
11
43, 35
x=0
12
43, 33, 16
GTO 16
13
1
1
14
44, 40, 1
STO+ 1
15
43, 33, 06
GTO 06
16
45, 1
RCL 1
17
31
R/S
18
33
R-down, R↓
19
33
R-down, R↓
20
44, 0
STO 0
21
1
1
22
30
-
23
43, 35
x=0
24
43, 33, 26
GTO 26
25
43, 33, 06
GTO 06
26
45, 2
RCL 2
27
42, 2
FIX 2 ( [ f ], 2)
28
43, 33, 00
GTO 00

Instructions:  Enter the integer, press [R/S].  Each factor is displayed, while in FIX 0 mode.  Press [R/S] for each factor.  If the number is prime, then only the integer is shown.
Test 1: 100.   Results: 2, [R/S], 2, [R/S], 5, [R/S], 5, [R/S], 100.00
100 = 2^2 * 5^2

Test 2:  255.  Results:  3, [R/S], 5, [R/S], 17, [R/S], 255.00
255 = 3 * 5 * 17

Test 3:  11.  Results: 11 [R/S], 11.00
11 is prime

Source:

Mullins, Henry and Stephen Kochan.  Programmable Pocket Calculators  Hayden Book Company.  Rochelle Park, NJ  1980.

Eddie


This blog is property of Edward Shore, 2016.

RPN HP 12C: Fibonacci and Lucas Sequences

  RPN HP 12C: Fibonacci and Lucas Sequences Golden Ratio, Formulas, and Sequences Let φ be the Golden Ratio: φ = (1 + √5) ÷ 2...