Post Reply 
HP 35s - How to write a for loop
02-11-2020, 05:35 PM
Post: #1
HP 35s - How to write a for loop
Hi!

I need to create a program to compute Euler's totient function but
i'm stuck in a for loop.
I have to write something like that:
Code:

for(i = 1; i < n; i++)
  // do stuff

I'm aware of the ISG operator and the loop-control number (i.e.
Code:
ccccccc.fffii
)
but i don't known how to integrate it with the i < n part. Is it possible? If so, can someone give
me an example of a for loop?

Thanks Smile
Find all posts by this user
Quote this message in a reply
02-11-2020, 08:32 PM
Post: #2
RE: HP 35s - How to write a for loop
Hello!

If you come from a high level programming language then programming loops and other control structures with calculators that rely on "keystroke programming" can be a very major PIA. But it is possible (to a degree).

(02-11-2020 05:35 PM)bxzj Wrote:  If so, can someone give me an example of a for loop?

This is an online version of the calculator manual: https://www.manualslib.com/manual/257003...228#manual
When you click forward two pages you can see a very simple example.

Have fun with it (so to say...)
Max
Find all posts by this user
Quote this message in a reply
02-11-2020, 08:44 PM
Post: #3
RE: HP 35s - How to write a for loop
(02-11-2020 08:32 PM)Maximilian Hohmann Wrote:  Hello!

If you come from a high level programming language then programming loops and other control structures with calculators that rely on "keystroke programming" can be a very major PIA. But it is possible (to a degree).

(02-11-2020 05:35 PM)bxzj Wrote:  If so, can someone give me an example of a for loop?

This is an online version of the calculator manual: https://www.manualslib.com/manual/257003...228#manual
When you click forward two pages you can see a very simple example.

Have fun with it (so to say...)
Max

The problem with the example provided by the manual is that it works with a “fixed limit”(for instance from 0 to 10 or from 0 to 50), what I want to do is a “dynamic” loop: something like “from 1 to x” with x inserted by the user. But I don’t know if it’s possible.
Find all posts by this user
Quote this message in a reply
02-11-2020, 10:39 PM
Post: #4
RE: HP 35s - How to write a for loop
I think so.

Simply take the user input upper limit and construct the control word.

User keys 499
R/S

Have the program take the 499, divide by 1000, then add one.

The control word would be 1.499 and the ISG loop would repeat the steps with the variable changing from 1 to 499.

If the user entered 50 or 75 or ? the loop would run from 1 to that supplied number of times. Something like this.

LBL T
1E3
/
1
+
STO 00
LBL L
Do something in here
ISG 00
GTO L
STOP


Is this what you are thinking about ?
Find all posts by this user
Quote this message in a reply
02-12-2020, 04:21 PM
Post: #5
RE: HP 35s - How to write a for loop
(02-11-2020 10:39 PM)Gene Wrote:  I think so.

Simply take the user input upper limit and construct the control word.

User keys 499
R/S

Have the program take the 499, divide by 1000, then add one.

The control word would be 1.499 and the ISG loop would repeat the steps with the variable changing from 1 to 499.

If the user entered 50 or 75 or ? the loop would run from 1 to that supplied number of times. Something like this.

LBL T
1E3
/
1
+
STO 00
LBL L
Do something in here
ISG 00
GTO L
STOP


Is this what you are thinking about ?

Exactly! thanks for your help.
btw, is there any advantages to use (I)(STO 0) or (J)(STO .) registers instead of A-Z one?
Find all posts by this user
Quote this message in a reply
02-17-2020, 10:20 AM
Post: #6
RE: HP 35s - How to write a for loop
Far better to use DSE instead of ISG, because
  • no need to prepare the loop (1000 / 1 + is not required)
  • not limited the loop end value to 999

Code:

LBL T
  STO A
  STO B
  LBL L
    RCL B
    RCL - A
    STOP
    DSE A
    GTO L
RTN

This is do the job also (counts from 0 to N-1). A is the counter register and B is for storing the user data to reverse the counting direction (or maybe it required later).


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




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