Showing posts with label programing. Show all posts
Showing posts with label programing. 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. 


Friday, April 7, 2017

Retro Review: Hewlett Packard HP 20S and 21S


Retro Review:  Hewlett Packard HP 20S and 21S



Company:  Hewlett Packard
Type:  Scientific Programming
Memory:  9 Registers, 99 Programing Steps, 6 Pre-loaded programs
Years:  1988? – 1993? (21S); 1988 – 2002(?) (20S), original price was around $50
Operating System:  Algebraic
Batteries:  3 x LR44

HP 21S:  The Rarer Cousin of the HP 20S

The HP 21S is an algebraic, keystroke programming calculator.  I paid $35, which is not bad considering the keyboard and the display are in supreme quality, the keys are a pleasure to touch, and the learning curve is easy to operate the calculator.   

The HP 21S was originally released by Hewlett Packard along with the more familiar cousin the HP 20S.   Both had a great dark brown keyboard with white font for primary labels, orange and blue for shift key fonts (my favorite!).  This design has been used several Hewlett Packard calculators from the late 1980s/early 1990s, including the HP 32SII and the HP 48 SX.  Sometime during the 1990s, the HP 20S garnered a purple/green shift font scheme, to match the HP 48 G series. 


What the 20S and 21S Have in Common

Both are algebraic programming calculators.  Each calculator has 10 memory registers (R0 through R9) with storage arithmetic available (STO+, STO-, STO*, and STO÷).  Both models have the standard array of scientific functions including trigonometric, logarithmic, exponential, combination, permutation, integer and fraction parts, and decimal/hours-minutes-seconds conversions.  However, there is no fraction mode and all numbers are real numbers. 

One thing is to consider is that the factorial function (n!) only accepts positive integers. 

 

The INPUT Key

 It does take a little getting used to working the [INPUT] key, especially for those who work with HP calculators, since we’re used to RPN.  The INPUT key stores the number in a temporary “x” slot.  The INPUT key places a role in several functions:
 
Rectangular to Polar Conversion:  x, [INPUT], y, [left shift], [STO] (>P) 
(θ is displayed, r is stored)
 
Polar to Rectangular Conversion:  r, [INPUT], θ, [right shift], [STO] (>R) 
(y is displayed, x is stored)
 
Combinations/Permutations:  n, [INPUT], r, [right shift], [ 0 ] (combination) or [ . ] (permutation)
 
Percent Change: 
HP 20S:  old, [INPUT], new, [left shift], [ 1/x ] (%CHG)
HP 21S:  old, [INPUT], new, [right shift], [ 5 ] (%CHG)

 

Statistics

 
Both the HP 20S and HP 21S have one variable, one weighted variable, two variable statistics, and linear regression.  There is no separate mode to be entered, just enter data with the [ Σ+ ] and go.  Upon entering and clearing data though, the following registers become holders of statistical sums:


R4 = n

R5 = Σx

R6 = Σy

R7 = Σx^2

R8 = Σy^2

R9 = Σxy

 
What is really nice is that both models have the sums indicated in gray at the bottom right hand corner of the keys as reminders.

Programming
 
Both the HP 20S and the HP 21S are keystroke programmable.  Both allow for labels A – F, 0 – 9.  In addition there are two tests:  x=0? and x≤y?.

The x≤y? Test:  x, [INPUT]*, y, [right shift], [ 7 ] (x≤y?).   A way to remember this command is:  hidden value ≤ displayed valued?

 
* According to the manuals, any other arithmetic/pending operation can be used such as [ + ] and [ ÷ ] are also allowed.
 

Each digit takes a step.  For example, 250 takes three steps, one for the 2, the 5, and the 0.

 
Differences between the HP 20S and HP 21S
 

Scientific Operations

 
HP 20S
HP 21S
Hyperbolic functions, base conversions (binary, octal, hexadecimal, binary), metric/US conversions (kg/lb, °C/°F, cm/in, l/gal)
Upper tail areas and inverse (z, t, F, Chi-squared), random numbers, seeding

 
Preloaded Programs

 

The HP 20S and HP 21S have six-preloaded programs.  The can be loaded by the key sequence [left shift], [ ← ] (LOAD).  While the HP 21S programs can be loaded at any time, the HP 20S must be in programming mode to load programs.

 

Unfortunately, user-created programs cannot be stored into permanent memory which would have been nice.

 

Preloaded Programs HP 20S

 

A
Root Finder
62 steps
B
Numerical Integration
58 steps
C
Complex Arithmetic
75 steps
D
3 x 3 Matrices
98 steps
E
Quadratic Equation
65 steps
F
Curve Fitting (logarithmic, exponential, power)
77 steps

 

Preloaded Programs HP 21S

 

A
One Sample Stat Tests
82 steps
B
Two Sample Stat Tests
82 steps
C
Linear Regression Stat Tests (used with One Sample Tests)
86 steps
D
Chi Square Tests
53 steps
E
Binomial Distribution
39 steps
F
Time Value of Money Solver (Finance)
99 steps

 

Final Verdict
 

Do I recommend buying the HP 21S and/or the HP 20S?  Assuming the price is reasonable, Yes, Yes, and Yes!  It is shame that the HP 20S and HP 21S are no longer produced because these are nice calculators.


The original price was around $45 - $55.    
 

Let’s get to the features.  I think it would be appropriate comparing the features of both the HP 21S and HP 20S at the same time. 
 

Eddie
 

This blog is property of Edward Shore, 2017

Monday, October 24, 2011

RPL Programming Tutorial - Part 16 - HP 49g+/50g: Choose Box

Giving Yourself Choices

Welcome to Part 16 of the RPL programming tutorial. Today we will work with CHOOSE boxes.

Note: The CHOOSE box is not available on the HP 48S and HP 28C.

The first two programs, TOREC and TOPOL, will be the subroutines that will be used in a third program, RPCONV.

TOREC: converts polar coordinates to rectangular coordinates

Level 2: θ to y
Level 1: r to x

y = r sin( θ )
x = r cos( θ )

TOPOL: converts rectangular coordinates to polar coordinates

Level 2: y to θ
Level 1: x to r

r = abs(x + yi) = √(x^2 + y^2)
θ = arg(x + yi) = arctan(y/x) where 0 ≤ θ ≤ π (radians) or 0 ≤ θ ≤ 180º (degrees)

The CHOOSE Command

Keystroke sequence for CHOOSE: [LS] [EVAL] (PRG) [NXT] [F5] (IN) [F3] (CHOOS)

Format:

"title of the choose box"
{ {"selection 1" program 1}
{"selection 2" program 2}
...
{"selection n" program n} }
1 CHOOSE


If a selection is made, 1 is returned. If the selection is canceled, 0 is returned. This requires further processing. One way to handle the output of the choose box is:

"title of the choose box"
{ {"selection 1" program 1}
{"selection 2" program 2}
...
{"selection n" program n} }
1 CHOOSE
IF 1 ==
THEN EVAL
ELSE KILL
END


The KILL command stops program execution.

Keystroke sequence for KILL: [LS] [EVAL] (PRG) [NXT] [NXT] [F3] (RUN) [F6] (EVAL)

The Program TOREC

Keystrokes:

[RS] [ + ] ( << >> )
[LS] [EVAL] (PRG) [F1] (STACK) [NXT] [NXT] [F1] (DUP2)

* DUP2 duplicates the contents on Levels 1 and 2
[NXT] [NXT] [F2] (SWAP)
* go the SWAP command from DUP2
[SIN] [ x ] [RS] [ENTER] (→NUM)
* Calculate y = r sin( θ )
[F6] (UNROT)
* rolls down a 3-level stack
[F2] (SWAP) [COS] [ x ] [RS] [ENTER] (→NUM) [ENTER]
* Calculate x = r sin( θ )

[ ' ] [ALPHA] [ALPHA] [COS] (T) [ ' ] (O) [ √ ] (R) [F5] (E) [F3] (C) [ENTER] [STO>]

The completed program:

<< DUP2 SWAP SIN * →NUM UNROT SWAP COS * →NUM >>


The Program TOPOL

Keystrokes:

[RS] [ + ] ( << >> )
[LS] [EVAL] (PRG) [F1] (STACK) [F2] (SWAP)
[LS] [TOOL] ( i ) [ x ] [ + ]

* Forms the complex number x + yi
[F1] (DUP) [RS] [ ÷ ] (ARG) [RS] [ENTER] (→NUM)
* Calculate θ = arg (x + yi)
[F2] (SWAP) [LS] [ ÷ ] (ABS) [RS] [ENTER] (→NUM) [ENTER]
* Calculate r = abs (x + yi)

[ ' ] [ALPHA] [ALPHA] [COS] (T) [ ' ] (O) [SYMB] (P) [ ' ] (O) [NXT] (L) [ENTER] [STO>]

The completed program:

<< SWAP i * + DUP ARG →NUM SWAP ABS →NUM >>


Now that TOREC and TOPOL are complete, let's do the main program, RPCONV. We made TOREC and TOPOL global programs, so they can be used on their own.

The Program RPCONV

[RS] [ + ] ( << >> )
[RS] [ x ] ( " " )
[ALPHA] [ALPHA] [F3] (C) [LS] [ALPHA] [ ' ] (o) [EVAL] (n) [EEX] (v) [F5] (e) [ √ ] (r) [COS] (t) [F5] (e) [ √ ] (r) [LS] [ALPHA] [ALPHA]

* Titles the choose box "Converter"
[SPC] [LS] [ + ] ( { } )
[LS] [ + ] ( { } )
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [RS] [ 0 ] ( →) [SYMB] (P) [ ' ] (O) [NXT] (L) [F1] (A) [ √ ] (R) [ALPHA]
[ → ] [RS] [ + ] ( << >> ) [ALPHA] [ALPHA] [COS] (T) [ ' ] (O) [SYMB] (P) [ ' ] (O) [NXT] (L) [ALPHA]

* First menu choice, →POLAR. TOPOL is typed.
[ → ] [ → ] [ →] [ → ]
[LS] [ + ] ( { } )
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [RS] [ 0 ] ( → ) [ √ ] (R) [F5] (E) [F3] (C) [COS] (T) [ALPHA]
[ → ] [RS] [ + ] ( << >> ) [ALPHA] [ALPHA] [COS] (T) [ ' ] (O) [ √ ] (R) [F5] (E) [F3] (C) [ALPHA]

* Second menu choice, →RECT. TOREC is typed.
[ → ] [ → ] [ → ] [ → ] [ → ]
1 [LS] [EVAL] (PRG) [NXT] [F4] (IN) [F3] (CHOOS)

* Exits the nested list and the CHOOSE command
[LS] [EVAL] (PRG) [F3] (BRCH) [RS] [F1] (IF)
* Enters the IF-THEN-ELSE-END structure
1 [LS] [EVAL] (PRG) [F3] (TEST) [F1] ( == )
[ ↓ ] [EVAL]
[ ↓ ] [LS] [EVAL] (PRG) [NXT] [NXT] [F3] (RUN) [F6] (KILL)
[ENTER]

* finishes the CHOOSE structure and terminates program entry

[ ' ] [ALPHA] [ALPHA] [ √ ] (R) [SYMB] (P) [F3] (C) [ ' ] (O) [EVAL] (N) [EEX] (V) [ENTER] [STO>]

The completed instructions:

<< "Converter"
{ { "→POLAR" << TOPOL >> }
{ "→RECT" << TOREC >> } }
1 CHOOSE
IF 1 ==
THEN EVAL
ELSE KILL
END >>


Instructions:

Rectangular to Polar Conversion

1. Enter y
2. Press [ENTER]
3. Enter x
4. Run RPCONV
5. Select the →POLAR option

Polar to Rectangular Conversion

1. Enter θ
2. Press [ENTER]
3. Enter r
4. Run RPCONV
5. Select the →RECT option

Examples (the calculator is in Degrees mode):

1. Convert r = 3.8, θ = 30º to rectangular coordinates.

Result: x ≈ 3.2909, y ≈ 1.9000

2. Convert x = 3.2, y = 6.3 to polar coordinates.

Result: r ≈ 7.0661, θ ≈ 63.0723º

Below are screen shots of Example 1.

Next time we will explore system flags.

Have a great day,

Eddie


This tutorial is property of Edward Shore. Mass reproduction and distribution requires express permission of the author.

HP 65 Programs: Triangulation, Percentile, Roots of Unity, Partial Fractions

  HP 65 Programs: Triangulation, Percentile, Roots of Unity, Partial Fractions Triangulation (done with HP-65 Emulator for Windows, Ber...