Post Reply 
(EL-5030) Prime Factors
04-21-2022, 03:01 PM
Post: #1
(EL-5030) Prime Factors
I got one of these odd little programmable Sharp calculators recently, and worked out how to do the typical mod-30 factor finder with its somewhat-limited "AER" (Algebraic Expression Reserve) programming language.

Notable limitations of AER as seen on this model:

- No arbitrary GOTO, only a looping structure resembling nested WHILE loops.
- IF/THEN/ELSE structure, but no loops allowed inside the THEN/ELSE clauses.
- Can do subroutines, but the subroutines can't contain any loops or conditionals.
- Only comparison operators are >, >=, and <> (not equal). To do other comparisons, swap the order of the arguments, or swap your THEN/ELSE bodies.

It does have arrays with indirect addressing, and that's enough to make this workable.

To run this, use the TITLE key to call up the factor program, press COMP, input a number to factor, and press COMP. The program will stop and display the first factor; press COMP to continue finding additional factors. The last factor will be displayed as a negative number.

Some translation notes, because AER uses a lot of symbols that aren't necessarily present in all fonts:

#1 : Reverse-video numbers (subroutines, e.g. 2ndF+Alpha+1)
=> : STO
\-> : Loop start (2ndF 0)
<-\ : Loop end (2ndF .)
<> : Not equal (2ndF 6)
=Y=>[, =N=>[ : THEN and ELSE clauses (2ndF 1, 2ndF 2)
/ : Division
Note that the last step should be negative X (unary minus, not subtraction).

This page has a nice history of Sharp's AER programming system:

http://www.arithmomuseum.com/szamologep....25&lang=en

I couldn't find a manual for the EL-5030 anywhere, but the EL-9000 manual is sufficient. The EL-5030 is largely equivalent to the AER I mode of the EL-9000.

Program code has been heavily broken up and indented for clarity, but this should be entered into the calculator without any spacing.

Code:
Main Body

M:f(X)=
#1
\->
    \->
        C+A[L]=>C
        \->
            X<>INT (X/C)*C
            =N=>[
                C,
                X/C=>X
                <-\
            ]
        L-1=>L
        L>0
        =Y=>[
            <-\
        ]
    X>C*C
    =Y=>[
        8=>L
        <-\
    ]
DIM A[1]
-X

Subroutine 1

#1:DIM A[12]
2=>A[12]
1=>A[11]
2=>A[10]
2=>A[9]
4=>A[8]
2=>A[7]
4=>A[6]
2=>A[5]
4=>A[4]
6=>A[3]
2=>A[2]
6=>A[1]
12=>L
0=>C
Visit this user's website Find all posts by this user
Quote this message in a reply
04-22-2022, 02:02 AM
Post: #2
RE: (EL-5030) Prime Factors
Very interesting.

Thanks
Find all posts by this user
Quote this message in a reply
04-22-2022, 04:17 PM
Post: #3
RE: (EL-5030) Prime Factors
Nice program! I've entered it on my EL-5030 but it returns 0 for any input.
I double checked the program in my EL-5030 vs. your listing and I don't see any difference.
However I'm wondering if the following sequence is correct:
Code:
            =N=>[
                C,
                X/C=>X
                <-\
            ]
What is the meaning of C, ?
Find all posts by this user
Quote this message in a reply
04-22-2022, 04:27 PM
Post: #4
RE: (EL-5030) Prime Factors
(04-22-2022 04:17 PM)Didier Lachieze Wrote:  Nice program! I've entered it on my EL-5030 but it returns 0 for any input.
I double checked the program in my EL-5030 vs. your listing and I don't see any difference.
However I'm wondering if the following sequence is correct:
Code:
            =N=>[
                C,
                X/C=>X
                <-\
            ]
What is the meaning of C, ?

I just double-checked the listing to make sure I didn't fat-finger anything when typing it in, and the listing looks okay.

The comma acts as "stop and display the result of the previous expression", so "C," is basically "PRINT C". In this program, C is the trial divisor, and if X<>INT (X/C)*C is false, that means C is a factor of X, so the program displays C, stores X/C into X, then repeats the trial division loop with the same value of C.
Visit this user's website Find all posts by this user
Quote this message in a reply
04-22-2022, 06:48 PM (This post was last modified: 04-22-2022 08:21 PM by C.Ret.)
Post: #5
RE: (EL-5030) Prime Factors
If we represent the Dave's nice AER program based on 3-loops and 3-conditionals by the following "inline-graphic":
   
Where the purple part is an outlaw AER syntax deliberately used for clarity by avoiding the element by element long list of A[..] affectations .

Code:
           ┌──────────────────────────────────────────────────────────────────────────​──────────────◄┐
           │ ┌────────────────────────────────────────────────────────────────◄┐                     │
           │ │          ┌─────────────────────────────◄┐                       │                     │
M:f(X)= #1 └►└►C+A[L]=>C└►X≠INT(X/C)*C =N=>[ C, X/C=>X ┘] ˽ L-1=>L ˽ L>0 =Y=>[ ┘] ˽ X>C*C =Y=>[ 8=>L ┘] ˽ DIM A[1] ˽ -X ■
#1:DIM A[12] ˽ { 6 2 6 4 2 4 2 4 2 2 1 2 }=>A[] ˽ 12=>L ˽ 0=>C



Then, we may consider this close related but shorter and faster AER code using only 2-loops and 2-conditionals :
   
Code:
           ┌──────────────────────────────────────────────────────────────────────────​───────────────────────◄┐
           │                                  ┌───────────────────────────────────────────────────────────◄┐  │
M:f(X)= #1 └►F+10*FRAC(A[I])=>F ˽ INT(A[I])=>I└►Q=X/F ˽ F>Q =Y=>[ DIM A[1] ˽ -X ■ ] Q≠INT(Q) =N=>[ F, Q=>X ┘] ┘ 
#1:DIM A[12] ˽ { 2.2 3.1 4.2 5.2 6.4 7.2 8.4 9.2 10.4 11.6 12.2 5.6 }=>A[] ˽ I=1 ˽ F=0


Please fill free to test and correct my code, I (still) have no SHARP EAR hardware...
... but expecting one in a near future Smile
Find all posts by this user
Quote this message in a reply
04-22-2022, 07:02 PM
Post: #6
RE: (EL-5030) Prime Factors
(04-22-2022 06:48 PM)C.Ret Wrote:  If we represent the Dave's nice AER program based on 3-loops and 3-conditionals by the following "inline-graphic":

Thanks, your graphical representation of the program looks good. Aside from the deliberate liberty taken on the array initialization, of course - would be nice if the calculator had a less-verbose method like that! In the Casio version that I based this on, I do 2->O~Z to initialize the whole array with 2 in one shot, then individually store the 6 elements that aren't 2, so it's a little bit shorter.

(04-22-2022 06:48 PM)C.Ret Wrote:  Please fill free to test and correct my code, I (still) have no SHARP EAR hardware...
... but expecting one in a near future Smile

Clever trick with stuffing the next array index into the registers! I'll have to try keying it in later to compare the speed and size.
Visit this user's website Find all posts by this user
Quote this message in a reply
04-22-2022, 08:07 PM (This post was last modified: 04-22-2022 08:12 PM by C.Ret.)
Post: #7
RE: (EL-5030) Prime Factors
(04-22-2022 07:02 PM)Dave Britten Wrote:  In the Casio version that I based this on, I do 2->O~Z to initialize the whole array with 2 in one shot, then individually store the 6 elements that aren't 2, so it's a little bit shorter.

On the SHARP, you may try the following to short your #1 Subroutine: I found same AER listings using multiple embedded store instructions, but I don't know if it's possible on the EL 5030 ?

Code:
#1: 12=>L ˽ DIM A[L] ˽ 6=>A[3]=>A[1] ˽ 4=>A[8]=>A[6]=>A[4] ˽ 2=>A[L]=>A[10]=>A[9]=>A[7]=>A[5]=>A[2] ˽ 1=>A[11] ˽ 0=>C
Find all posts by this user
Quote this message in a reply
04-22-2022, 09:37 PM
Post: #8
RE: (EL-5030) Prime Factors
(04-22-2022 04:17 PM)Didier Lachieze Wrote:  Nice program! I've entered it on my EL-5030 but it returns 0 for any input.

Ok, I’ve found my mistake, I had entered a ◣ at the end of the subroutine, so the program just stopped there. After removing it it works correctly, I get the different factors until it returns -1. For 15 I get 3, 5 and -1.

(04-22-2022 08:07 PM)C.Ret Wrote:  On the SHARP, you may try the following to short your #1 Subroutine: I found same AER listings using multiple embedded store instructions, but I don't know if it's possible on the EL 5030 ?

Code:
#1: 12=>L ˽ DIM A[L] ˽ 6=>A[3]=>A[1] ˽ 4=>A[8]=>A[6]=>A[4] ˽ 2=>A[L]=>A[10]=>A[9]=>A[7]=>A[5]=>A[2] ˽ 1=>A[11] ˽ 0=>C

Yes it works on the EL-5030 (without the spaces).

(04-22-2022 06:48 PM)C.Ret Wrote:  Then, we may consider this close related but shorter and faster AER code using only 2-loops and 2-conditionals :

Code:
           ┌──────────────────────────────────────────────────────────────────────────​───────────────────────◄┐
           │                                  ┌───────────────────────────────────────────────────────────◄┐  │
M:f(X)= #1 └►F+10*FRAC(A[I])=>F ˽ INT(A[I])=>I└►Q=X/F ˽ F>Q =Y=>[ DIM A[1] ˽ -X ■ ] Q≠INT(Q) =N=>[ F, Q=>X ┘] ┘ 
#1:DIM A[12] ˽ { 2.2 3.1 4.2 5.2 6.4 7.2 8.4 9.2 10.4 11.6 12.2 5.6 }=>A[] ˽ I=1 ˽ F=0


Please fill free to test and correct my code, I (still) have no SHARP EAR hardware...
... but expecting one in a near future Smile

This one works also with a few changes:
- Replacing Q=X/F by X/F=>Q , I=1 by 1=>I and F=0 by 0=>F
- Initializing individually each element of A[]

For 15 I get 3 and -5
Find all posts by this user
Quote this message in a reply
04-23-2022, 04:27 AM (This post was last modified: 04-23-2022 09:45 AM by C.Ret.)
Post: #9
RE: (EL-5030) Prime Factors
(04-22-2022 09:37 PM)Didier Lachieze Wrote:  This one works also with a few changes:
- Replacing Q=X/F by X/F=>Q , I=1 by 1=>I and F=0 by 0=>F
- Initializing individually each element of A[]

For 15 I get 3 and -5

Thanks for pointing out all those silly mistakes!
As I don't have an AER device, I try my code on BASIC SHARP and I accidentally mixed up codes in the notepad before copying and pasting.

Much better AER code here:
Code:
M:f(X)= #1 └►F+10*FRAC (A[I])=>F˽INT (A[I])=>I└►X/F=>Q˽F>Q =Y=>[DIM A[1]˽-X■]Q≠INT (Q)=N=>[F,Q=>X◄┐]◄┐
#1:DIM A[12]˽2.2=>A[1]˽3.1=>A[2]˽4.2=>A[3]˽5.2=>A[4]˽6.4=>A[5]˽7.2=>A[6]˽8.4=>A[7]˽9.2=>A[8]˽10.4=>A[9]˽11.6=>A[10]˽12.2=>A[11]˽5.6=>A[12]˽1=>I˽0=>F


This code is longer than it appears on the voluntarily reduced form!
I'm not sure it's shorter or faster anymore since the F>Q 'end-test' is done at each iteration; in the initial code the test X>C*C only takes place once after the 8th iteration. How much time does it save or lose?
I imagine the speed improvements will greatly depend on specific values ​​for X.

Can the sequence INT (A[I])=>I be replaced by a simpler A[I]=>I ?
Did the EL-5030 accept the reduced syntax INT A[I] or FRAC A[I] instead of INT (A[I]) or FRAC (A[I]) like the PC-1211 does?
Find all posts by this user
Quote this message in a reply
04-23-2022, 08:31 AM
Post: #10
RE: (EL-5030) Prime Factors
(04-23-2022 04:27 AM)C.Ret Wrote:  Can the sequence INT (A[I])=>I be replaced by a simpler A[I]=>I ?
Did the EL-5030 accept the reduced syntax INT A[I] or FRAC A[I] instead of INT (A[I]) or FRAC (A[I]) like the PC-1211 does?

Yes to both. So the updated code below works fine on the EL-5030:
Code:
M:f(X)= #1└►F+10*FRAC A[I]=>F˽A[I]=>I└►X/F=>Q˽F>Q =Y=>[DIM A[1]˽-X◣]Q≠INT Q=N=>[F,Q=>X◄┐]◄┐
#1:DIM A[12]˽2.2=>A[1]˽3.1=>A[2]˽4.2=>A[3]˽5.2=>A[4]˽6.4=>A[5]˽7.2=>A[6]˽8.4=>A[7]˽9.2=>A[8]˽10.4=>A[9]˽11.6=>A[10]˽12.2=>A[11]˽5.6=>A[12]˽1=>I˽0=>F
Find all posts by this user
Quote this message in a reply
04-23-2022, 05:07 PM (This post was last modified: 04-23-2022 05:43 PM by C.Ret.)
Post: #11
RE: (EL-5030) Prime Factors
We may spare one element in matrix/vector A[..]:
   

Code:
M:f(X)=#1└►└►X/F꞊>Q˽F>Q ꞊Y꞊>[-X◣]Q≠INT Q ꞊N꞊>[F,Q꞊>X◄┐]F+10*FRAC A[I]꞊>F˽A[I]꞊>I◄┐
1:DIM A[11]˽2.1꞊>A[1]˽3.2꞊>A[2]˽4.2꞊>A[3]˽5.4꞊>A[4]˽6.2꞊>A[5]˽7.4꞊>A[6]˽8.2꞊>A[7]˽9.4꞊>A[8]˽10.6꞊>A[9]˽11.2꞊>A[10]˽4.6꞊>A[11]˽1꞊>I˽2꞊>F
Find all posts by this user
Quote this message in a reply
Post Reply 




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