Post Reply 
idivis inWP34s
11-28-2015, 12:14 AM (This post was last modified: 11-28-2015 05:43 AM by Thomas Klemm.)
Post: #7
RE: idivis inWP34s
We can write a little function in Python:
Code:
def idivis(n):
    i = 0
    while i <= n:
        i += 1
        if n % i == 0:
            print i

Code:
>>> idivis(28)
1
2
4
7
14
28

Yay, it works!

So let's use the Python to FOCAL Compiler to translate it:
Code:
LBL "IDIVIS"
STO 00 ; n
RDN
0
STO 01 ; i
RDN
LBL 00
RCL 01 ; i
RCL 00 ; n
X<Y?
GTO 01
RCL 01 ; i
1
+
STO 01 ; i
RDN
RCL 00 ; n
RCL 01 ; i
MOD
0
X≠Y?
GTO 00
RCL 01 ; i
AVIEW
GTO 00
GTO 00
LBL 01
LBL 02
RTN

We can remove a few RDN commands here and there and LBLs we don't really need. We have to replace MOD by RMDR and only use 3 letters for the label. And then we have to fix the AVIEW command as we want to display register 01 instead.
Code:
LBL'IDV'
STO 00 ; n
0
STO 01 ; i
LBL 00
RCL 01 ; i
RCL 00 ; n
X<Y?
GTO 01
RCL 01 ; i
1
+
STO 01 ; i
RCL 00 ; n
RCL 01 ; i
RMDR
0
X≠Y?
GTO 00
VIEW 01
GTO 00
LBL 01
RTN

Next thing we can do is replace these lines
Code:
RCL 01 ; i
1
+
STO 01 ; i
by a single command
Code:
INC 01 ; i
How cool is that?

We can compare X directly with a register or 0.
And then we notice that we don't really need to jump to label 01 to exit the program but can use RTN directly.
Code:
LBL'IDV'
STO 00 ; n
0
STO 01 ; i
LBL 00
RCL 00 ; n
INC 01 ; i
X<? 01 ; i
RTN
RCL 01 ; i
RMDR
X≠0?
GTO 00
VIEW 01 ; i
PSE 05
GTO 00

As you can see I've added a PSE instruction to wait a little between displaying the factors.
We can get rid of the label using the BACK command. And let's use a fancy way to write the constant 0.
Code:
LBL'IDV'
STO 00 ; n
# 000
STO 01 ; i
RCL 00 ; n
INC 01 ; i
X<? 01 ; i
RTN
RCL 01 ; i
RMDR
X≠0?
BACK 007
VIEW 01 ; i
PSE 05
BACK 010

If we want to get rid of the registers and only use the stack we have to be careful as we modify it. Thus RCL Y can refer to i or n.
And then we notice one of the drawbacks that the comparison function X≠0? doesn't consume the operand: we can't just return in the loop but have to clean up the stack.
But since we have to do this in both cases we can just skip 2 lines. Just make sure to adjust the BACK command.
Code:
LBL'IDV'
# 000
RCL Y ; n
INC Y ; i
X<? Y ; i
RTN
RCL Y ; i
RMDR
X≠0?
SKIP 002
VIEW Y; i
PSE 05
DROP
BACK 011

Cheers
Thomas

PS: Did you notice where I cheated a little?
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
idivis inWP34s - ggauny@live.fr - 11-27-2015, 07:26 PM
RE: idivis inWP34s - Dieter - 11-27-2015, 08:24 PM
RE: idivis in WP 34S - walter b - 11-27-2015, 08:34 PM
RE: idivis inWP34s - Dieter - 11-27-2015, 08:53 PM
RE: idivis inWP34s - walter b - 11-27-2015, 10:27 PM
RE: idivis inWP34s - Dieter - 11-27-2015, 11:12 PM
RE: idivis inWP34s - Marcus von Cube - 11-28-2015, 11:14 AM
RE: idivis inWP34s - walter b - 11-28-2015, 12:01 PM
RE: idivis inWP34s - Thomas Klemm - 11-28-2015 12:14 AM
RE: idivis inWP34s - ggauny@live.fr - 11-28-2015, 06:30 AM
RE: idivis inWP34s - Dieter - 11-28-2015, 07:35 PM
RE: idivis inWP34s - ggauny@live.fr - 11-28-2015, 07:28 AM
RE: idivis inWP34s - Thomas Klemm - 11-28-2015, 11:25 AM
RE: idivis inWP34s - Didier Lachieze - 11-29-2015, 12:25 PM
RE: idivis inWP34s - ggauny@live.fr - 03-27-2016, 09:34 AM
RE: idivis inWP34s - ggauny@live.fr - 11-29-2015, 12:34 PM
RE: idivis inWP34s - ggauny@live.fr - 11-29-2015, 12:47 PM
RE: idivis inWP34s - Paul Dale - 03-27-2016, 10:26 AM
RE: idivis inWP34s - ggauny@live.fr - 03-27-2016, 10:43 AM
RE: idivis inWP34s - Didier Lachieze - 03-27-2016, 11:17 AM
RE: idivis inWP34s - Paul Dale - 03-27-2016, 11:14 AM
RE: idivis inWP34s - ggauny@live.fr - 03-27-2016, 12:00 PM



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