HP Forums

Full Version: (HP17BII) What way for a loop stop condition ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(HP17BII) What way for a loop stop condition ?

You know the Czech guess, of course (conjecture tchèque in French) ?

In fact, i have a question !
Just be a little patient...

A natural equation for the resolution of this sequence on the HP17BII would be as follows :

Code:

SUITE = IF(N>1 :
       IF(MOD(N:2)=0 : 
              L(N:N÷2) : 
              L(N:Nx3+1)
           ) :
       N
    )

with this use process :

[CALC] gives [SUITE] [N]

3 [N] --> N=3
[SUITE] --> SUITE=10
[SUITE] --> SUITE=5
[SUITE] --> SUITE=16
[SUITE] --> SUITE=8
[SUITE] --> SUITE=4
[SUITE] --> SUITE=2
[SUITE] --> SUITE=1

So, 3 requires 7 steps to become 1

But, if you want an easy way to follow the number of sequences from the initial N until 1, you could modify the equation as well :

Code:

SUITE = IF(N>1 :
       IF(MOD(N:2)=0 : 
              L(N:Nv2) : 
              L(N:Nx3+1)
           ) x SGN(L(NB:NB+1):
       N
    ) + NB÷1E4

with a very similar use process :

[CALC] gives [SUITE] [N] [NB]
DSP [FIX] 4 INPUT

3 [N] --> N=3.0000
[SUITE] --> SUITE=10.0001
[SUITE] --> SUITE=5.0002
[SUITE] --> SUITE=16.0003
[SUITE] --> SUITE=8.0004
[SUITE] --> SUITE=4.0005
[SUITE] --> SUITE=2.0006
[SUITE] --> SUITE=1.0007

and the same number of steps to move 3 to 1

This time, there is a satisfying equation... for a little sequence and if you have a good memory to remember the initial N !!

So, a step further, i propose you to evolve this equation by modifying it again.
Then, i use a internal variable to conserve the initial N and i do the insertion of a loop control to computerize the sequence from N to 1.

This is the equation :

Code:

CT = Σ(I : 0 : L(W:N)x0 + MAX : 1 :
       IF( G(W) > 1 : 
           IF( FP( L(W : G(W) ÷ 2)) <> 0 :
           SGN( L(W : G(W)x6 + 1) ) :
                   1
           ) :
               0
       )
      )

and the use process evolves as follow :

[CALC] gives [CT] [N] [MAX]
DSP [ALL]
10 [MAX]
3 [N]
[CT] --> CT=7

Now, you receive the number of sequence as a result of the equation with [CT].
That's OK.
What is more disputable is that :
- we need to know beforehand the maximum number of iterations to perform, before launching the evaluation of the equation
- if we have incorrectly evaluated the maximum number of iterations necessary, we will not know it
- if we choose for security a very large forecast number of iterations, the calculator will perform a very long processing without this being necessary
- to be fair, the maximum should be adjusted for each evaluation of the number of sequences with each number: compare CT(3)=7 with CT(837799)=524...

So, a new alternative to modifying the equation could be this :

Code:

CT = Σ(I : 0 : L(W:N)x0 + MAX : 1 :
       IF(W > 1 : 
           IF( FP( L(W : W ÷ 2)) <> 0 :
           SGN( L(W : Wx6 + 1) ) :
                   1
           ) :
               0
       )
      )

With the control of the variable [W], by RCL [W] at the end of the calculation, you should verify if the last term of the sequence is effectively 1.
You should initialize MAX to 1000 (1000 [MAX]) before enter N (3 [N] or 837799 [N]) and evaluate the equation by [CT].
However, the resolution time will always be the same with a fairly long time.

So, it's time for me to ask my question !!

Has anyone ever solved checking the end of the loop condition dynamically on the HP17BII or HP27S ? Or on the more recent HP17BII+ ?
Is there possible to have a end condition based on the evaluation of the last term of the sequence ?
Have you an other proposition to rewrite this equation in order to allow the dynamic resolution of this ?

(Is this possible stopping to loop as soon as the last term is 1 ?)
You can always terminate a solver sigma loop early on the 17bii and 17b by performing an illegal operation (like LOG(0) or /0), but that will also end the evaluation of the equation. In many cases you can save the value you want in a variable and then RCL that variable after the error message. I have used this method successfully many times, such as in my prime factor finder:

Code:

primef:if(mod(n:2)=0:L(fact:2)+L(n:n/2)/0:sigma(i:3:sqrt(n):2:if(mod(n:i)=0:L(fact:i)+L(n:n/i)/0:0))+n)-fact

This also works on the 27s.

I wouldn't try this on the 17bii+, however, unless you just want to make your life miserable.

This also works on the 19b and 19bii with the added advantage that you don't need to recall FACT, it will appear automatically.

Hope this helps.
If you're not already familiar with the Solver GET-LET document by Martin Hepperle, I would highly recommend it, along with the Technical Applications book used as a reference

https://www.hpmuseum.org/forum/thread-6573.html
(05-09-2020 02:27 PM)Don Shepherd Wrote: [ -> ]You can always terminate a solver sigma loop early on the 17bii and 17b by performing an illegal operation (like LOG(0) or /0), but that will also end the evaluation of the equation. In many cases you can save the value you want in a variable and then RCL that variable after the error message. I have used this method successfully many times, such as in my prime factor finder:

Code:

primef:if(mod(n:2)=0:L(fact:2)+L(n:n/2)/0:sigma(i:3:sqrt(n):2:if(mod(n:i)=0:L(fact:i)+L(n:n/i)/0:0))+n)-fact

Hope this helps.

(05-09-2020 02:36 PM)mfleming Wrote: [ -> ]If you're not already familiar with the Solver GET-LET document by Martin Hepperle, I would highly recommend it, along with the Technical Applications book used as a reference

https://www.hpmuseum.org/forum/thread-6573.html

Thank you very much Don Shepherd and mfleming!
So I'm going to insert a tiny presence of abrupt end in my equation!
I will reread with pleasure the precious document published by Martin Hepperle, augmented by Don Shepherd, thanks to which I had already progressed a few years ago!

That's nice !
Based on Don Shepherd's suggestions and a new evolution of my equation, here is what it gives:

Code:

CT = Σ(I : 0 : L(W:N) x L(NB:0) + MAX : 1 :
       IF(G(W) > 1 : 
          L(NB : NB + 
        IF(FP( L(W : G(W) ÷ 2)) <> 0 :
               SGN( L(W : G(W)x6 + 1) ) :
               1
            )
          ) :
          0 ÷ 0
       )
     )

and the process becomes as follows :

[CALC] gives [CT] [N] [MAX] [NB]
DSP [ALL]
1000 [MAX]

3 [N]
[CT] --> SOLUTION NOT FOUND
RCL [NB] NB=7

27 [N]
[CT] --> SOLUTION NOT FOUND
RCL [NB] NB=111

La fin justifie les moyens !! (so, the end justifies the means)
Thanks Don Shepherd !
Reference URL's