Post Reply 
idivis inWP34s
11-27-2015, 07:26 PM
Post: #1
idivis inWP34s
Hi,

I am working to calcul all divisors of a number positiv and different of zero, in this last case all numbers are divisors of course. It is because I love fun arithmetics thing like perfects numbers and so on.

I have do a flowchart (I hope attachement will run, I encounter somme issues this later days with this).

I know it is not a obligation to calcul over N/2, but to day I don't find the program solution even with Dieter's divmod, yes it run but the steps are very huges !
So I am nervous to not find.

If you please help me, even my very old program HP 67 don't work in the WP34s.

I try to attachement my flowchart.


Attached File(s) Thumbnail(s)
   

Gérard.
Find all posts by this user
Quote this message in a reply
11-27-2015, 08:24 PM (This post was last modified: 11-27-2015 08:35 PM by Dieter.)
Post: #2
RE: idivis inWP34s
(11-27-2015 07:26 PM)ggauny@live.fr Wrote:  I am working to calcul all divisors of a number positiv and different of zero, in this last case all numbers are divisors of course. It is because I love fun arithmetics thing like perfects numbers and so on.

So you want all divisors, sorted in ascending order?
E.g. 1235 should return 1 5 13 19 65 95 247 and finally 1235?

(11-27-2015 07:26 PM)ggauny@live.fr Wrote:  I have do a flowchart (I hope attachement will run, I encounter somme issues this later days with this).

What about posting your program?

An implementation of your flowchart could look like this:

Code:
FILL
Clx
STO L     // store divisor i in L
x<> L
INC X     // i:=i+1
x≥y?      // i >= n?
RTN       // then quit (should display n as the last divisor)
RMDR      // calculate n mod i (while i stays in L)
x≠0?      // n not divisble by i ?
BACK 006  // test next divisor
x<> L
STOP      // else display i
BACK 008

And here is a version that tests up to n/2:

Code:
FILL
Clx
STO L     // store divisor i in L
x<> L
INC X     // i:=i+1
RCL+X     // compare n and 2*i while keeping i in L
x≤y?      // 2i ≤ n  resp.  i ≤ n/2 ?
SKIP 002  // then continue
DROP
RTN       // else display n and quit 
x<> L     // recall i again
RMDR      // calculate n mod i (while i stays in L)
x≠0?      // n not divisble by i ?
BACK 010  // test next divisor
x<> L
STOP      // else display i
BACK 012

Now, how did you do it?

Dieter
Find all posts by this user
Quote this message in a reply
11-27-2015, 08:34 PM (This post was last modified: 11-27-2015 08:36 PM by walter b.)
Post: #3
RE: idivis in WP 34S
Bonsoir Gérard,

based on your flowchard, I'd suggest the following straight forward code:
Code:

LBL 'IDV'
STO 00
0
STO 01
LBL 00
INC 01
RCL 00
RCL 01
RMDR
x =! 0?
GTO 01
RCL L
PSE 10
GTO 00
LBL 01
RCL 00
2
/
RCL 01
x <= y?
GTO 00
RTN

I didn't test it ...

d:-)

Edit: Dieter was faster and more elegant than me.
Find all posts by this user
Quote this message in a reply
11-27-2015, 08:53 PM (This post was last modified: 11-27-2015 09:08 PM by Dieter.)
Post: #4
RE: idivis inWP34s
(11-27-2015 08:34 PM)walter b Wrote:  based on your flowchard, I'd suggest the following straight forward code:

Which can be improved e.g. by prestoring n/2 in R02 and comparing the divisor with this register. I tried a similar version where the divisors are displayed by a PSE statement, and I noticed the following difference to classic HPs:

Usually PSE (or here PSE 10) interrupts program execution for a second and displays X. Then the display blanks out again (showing flickering LEDs, the flying goose, "running" or the like) until the next result is displayed.

The 34s seems to behave differently. Here the value stays in the display even after the PSE is terminated and the program continues. At first I thought this was because the 34 is so fast, but even if there are several seconds between two divisors the latter continues to stay in the display. It looks like VIEW X PSE on a HP41. Only a MSG 00 cancels the display again, much like a CLD on an HP41.

Which is nice here, but I wonder if is this the intended behaviour since it is completely different from the way the classic HPs do it.

Dieter
Find all posts by this user
Quote this message in a reply
11-27-2015, 10:27 PM
Post: #5
RE: idivis inWP34s
(11-27-2015 08:53 PM)Dieter Wrote:  ... I wonder if is this the intended behaviour since it is completely different from the way the classic HPs do it.

It is intended - please see the chapter titled 'Programmed Input and Output, User Interaction and Dialogues' in your manual. Though I don't remember the reasoning behind it anymore. Maybe Pauli or Marcus do.

d:-)
Find all posts by this user
Quote this message in a reply
11-27-2015, 11:12 PM (This post was last modified: 11-27-2015 11:13 PM by Dieter.)
Post: #6
RE: idivis inWP34s
(11-27-2015 10:27 PM)walter b Wrote:  It is intended - please see the chapter titled 'Programmed Input and Output, User Interaction and Dialogues' in your manual.

Yes, I read this section of the manual, but...

(11-27-2015 10:27 PM)walter b Wrote:  Though I don't remember the reasoning behind it anymore. Maybe Pauli or Marcus do.

...I don't see any reason for this either. The idea behind a PSE statement is: display X for a moment, then continue. This is not what PSE does on the 34s. Of course PSE can also be used to delay program execution for a moment, e.g. while a message is displayed, but this can be done by combining it with the VIEW... commands. That's exactly how it is done on the 41 or 42. Which I thought were a kind of standard for the way the 34s was supposed to work.

In other words: the way PSE is implemented on the 34s feels ..."wrong" to me. It's simply not how PSE works on HP calculators.

Dieter
Find all posts by this user
Quote this message in a reply
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
11-28-2015, 06:30 AM
Post: #8
RE: idivis inWP34s
Hi kinds friends,

When I thing that now I am no more capable to do this !!! Even more, I am no longer "new in box", I know, but ... Damned where have I put my brain ?!!!!

Thank's.

Gérard.
Find all posts by this user
Quote this message in a reply
11-28-2015, 07:28 AM
Post: #9
RE: idivis inWP34s
Dear Thomas,

You have wrot like this :

"PS: Did you notice where I cheated a little?"

No I have not see where you are cheated.

Good day for alls.

Gérard.
Find all posts by this user
Quote this message in a reply
11-28-2015, 11:14 AM
Post: #10
RE: idivis inWP34s
(11-27-2015 11:12 PM)Dieter Wrote:  In other words: the way PSE is implemented on the 34s feels ..."wrong" to me. It's simply not how PSE works on HP calculators.

I'd say: "This is how PSE works on the 34S." Smile

The word "running" does not convey any useful information to the user so we (or was it me alone?) decided to leave the display in the state of the most recent VIEW or PSE statement. In an interactive program that uses PSE together with KEY? in a loop, an irritating flicker would occur after the PSE terminates. As already mentioned, MSG 00 restores the "running" text for those who prefer it. Just add the statement after each PSE call!

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
Find all posts by this user
Quote this message in a reply
11-28-2015, 11:25 AM
Post: #11
RE: idivis inWP34s
(11-28-2015 07:28 AM)ggauny@live.fr Wrote:  No I have not see where you are cheated.

The comparison in the while-loop should be i < n instead of i <= n:
Code:
def idivis(n):
    i = 0
    while i < n:
        i += 1
        if n % i == 0:
            print i

Otherwise the last test will be with i = n+1 instead of i = n. But I was lazy when I noticed my mistake and just moved the INC command in front of the comparison:
Code:
LBL 00
RCL 01 ; i
RCL 00 ; n
X<Y?
GTO 01
RCL 01 ; i
1
+
STO 01 ; i

Code:
LBL 00
RCL 00 ; n
INC 01 ; i
X<? 01 ; i
RTN

But in the end it doesn't matter much since n+1 is not a divider of n.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
11-28-2015, 12:01 PM
Post: #12
RE: idivis inWP34s
(11-28-2015 11:14 AM)Marcus von Cube Wrote:  "This is how PSE works on the 34S." Smile

Makes sense. Smile

d:-)
Find all posts by this user
Quote this message in a reply
11-28-2015, 07:35 PM (This post was last modified: 11-28-2015 07:59 PM by Dieter.)
Post: #13
RE: idivis inWP34s
(11-28-2015 06:30 AM)ggauny@live.fr Wrote:  When I thing that now I am no more capable to do this !!!

Of course you are.

(11-28-2015 06:30 AM)ggauny@live.fr Wrote:  Even more, I am no longer "new in box", I know, but ... Damned where have I put my brain ?!!!!

Maybe this will help a bit. I have taken your flowchart and added the corresponding steps (in blue). Take a look at it and you will see that it's quite easy.

Dieter

   
Find all posts by this user
Quote this message in a reply
11-29-2015, 12:25 PM (This post was last modified: 11-29-2015 10:19 PM by Didier Lachieze.)
Post: #14
RE: idivis inWP34s
In 15 steps with the test up to n/2:

Code:
01 LBL A    n
02 #000     0     n
03 INC X    i     n
04 cENTER   i     n     i     n
05 RCL+X    2i    n     i     n
06 x<>T     n     n     i     2i
07 X<=? T   n     n     i     2i
08 RTN      n     n
09 RCL Z    i     n     n     i
10 RMDR     r     n     i
11 RCL+Z    i+r   n     i
12 x=? Z
13 R/S      i     n     i
14 x<> Z    i     n
15 BACK 12

Each time the program stops the divisor is in X and the initial number in Y. So you can see it with YDON.
Find all posts by this user
Quote this message in a reply
11-29-2015, 12:34 PM
Post: #15
RE: idivis inWP34s
Hi Dieter,

Of course it is a big help ! A very good idea as usual from you. Now when I draw a flochart I annoted it as you, very clear like this ! I was never at all thinking to do like this.

Btw, it is of nothing interest for forum, but this past night, I was reflected that soon I will be a "Kaprekar number man" !

Yes because in next february I will be 81 years, and then it is 9^2=81 and 8+1=9 !

Funny no ? It is only one time in life of man, for thoses they reach there, because the second Kaprekar is 45^2 and = 2025 years !!!!

In attachement it is me 3 years ago. I was King for "Epiphanie"

Tchuss !

Gérard.
Find all posts by this user
Quote this message in a reply
11-29-2015, 12:47 PM
Post: #16
RE: idivis inWP34s
Hi Didier,
A another usefull routine. You know, me I do as past time, I inscriv all into a school boy notebook, with a pencil as this I can ruber if I do mistake, to remember well what you have alls wroted and I am studiying with deep.

I you remerciate for this new routine.

Trully for alls.

Smile

Gérard.
Find all posts by this user
Quote this message in a reply
03-27-2016, 09:34 AM
Post: #17
RE: idivis inWP34s
(11-29-2015 12:25 PM)Didier Lachieze Wrote:  In 15 steps with the test up to n/2:

Code:
01 LBL A    n
02 #000     0     n
03 INC X    i     n
04 cENTER   i     n     i     n
05 RCL+X    2i    n     i     n
06 x<>T     n     n     i     2i
07 X<=? T   n     n     i     2i
08 RTN      n     n
09 RCL Z    i     n     n     i
10 RMDR     r     n     i
11 RCL+Z    i+r   n     i
12 x=? Z
13 R/S      i     n     i
14 x<> Z    i     n
15 BACK 12

Each time the program stops the divisor is in X and the initial number in Y. So you can see it with YDON.

In this concise and elegant routine for idivis on wp34s, I encounter a little issue.
The very last divisor dont' appears (but is in LAST X register).

Trying with 6, for example, we are going to see : 1, R/S 2, R/S 6. The 3 we wait to see is oculted.

I think it is dûe to the test in step 07. I am going to try no issue.

Gérard.
Find all posts by this user
Quote this message in a reply
03-27-2016, 10:26 AM
Post: #18
RE: idivis inWP34s
Step 07 will stop when i == 3, in this case 2*3 <= 6.


Pauli
Find all posts by this user
Quote this message in a reply
03-27-2016, 10:43 AM
Post: #19
RE: idivis inWP34s
Hi,
Thank for your answer, yes and I understand, but is it a way to see the number 3
or the last divisor on the display ? Do you see what I mean ?

It is for my routine for see all perfects numbers. I think if I see all divisors, I can STO+ them in a register and comparing them with n, the departure number, if it is egal then it is a perfect number.

Gérard.
Find all posts by this user
Quote this message in a reply
03-27-2016, 11:14 AM
Post: #20
RE: idivis inWP34s
I'd try x<? T instead there & see what happens.

Of course there are easier ways to find all the known perfect numbers, the proof of this is pretty straight foward & was taught to me in first year number theory at university. It also generates all the factors in sequence.

More importantly, Euler proved that these are the only even perfect numbers.

To date, there are no known odd perfect numbers. There are a lot of known conditions one must adhere to and the general consensus is that they do not exist, even though this is a major open problem in mathemtics.


- Pauli
Find all posts by this user
Quote this message in a reply
Post Reply 




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