Monday, May 30, 2022

Plus42: The FOR Function

Plus42:  The FOR Function



About the Plus42 App


Author:  Thomas Okken


App: 

Android:  $9.99

iOS:  $9.99

PC/MacOS/Linux:  Free

Donations Accepted


Link:  https://thomasokken.com/plus42/


Please give Okken some love!   Free42 and Plus42 are both awesome apps and emulators, both emulate the HP 42S engine.  The Plus42 adds the solver of the HP 17B/19B/27S and unit conversions of the HP 48/49/50 family.   The Plus42 can also graph functions.   You can find a lot more information on the link above!


(Disclaimer:  I am not being paid.)


I am now including apps like the Plus42 in my rotation for blogs, please be on the look out for those.  


Today's blog entry will cover one of the additional solver functions for the Plus42:  FOR.


Plus42 FOR Syntax


FOR(INIT:COND:NEXT:EXPR)


INIT:  Initial commands


COND:  condition

If the condition is true:  execute EXPR (there can be more than one EXPR statements), then NEXT

If the condition is false:  the loop ends


NEXT:  next command,  this is where the counter variable is incremented or decremented


EXPR:  the main loop


Examples 


Example 1:  Add from 1 to 4.


HP 39G/Prime Code:

A:=0;

FOR K FROM 1 TO 4 DO  (STEP 1)

A:=A+K;

END;


Plus42 Equation Code:

S=FOR((L(A:0)+L(I:1))×0:G(I)≤4:L(I:G(I)+1):L(A:G(A)+G(I)))


Variables:

S = sum_final answer

A = sum

I = counter variable


Result:  S = 10


S=:   set the result to the variable S, S = A

INIT:  (L(A:0)+L(I:1))×0;  set A = 0 and I = 1

COND:  G(I)≤4;  test if I≤4; if true continue, if false exit the loop

NEXT:  L(I:G(I)+1); this happens at the end of the loop, this is like the general STEP incr/decr command

EXPR: L(A:G(A)+G(I))


The use of the Get function allows the variable to used automatically and not be displayed in the CALC menu.  


Something that threw me off is that the order of NEXT and EXPR, which the EXPR (which I think there could be more than one EXPR statements), I am used to NEXT either at the end of the FOR loop or implied (end of indentation in Python, for example).


Example 2:  Product


Calculate Π(n/4, n=1 to m) = 1/4 * 2/4 * 3/4 * ... * m/4


Plus42 Equation Code:

P=FOR((L(A:1)+L(N:1)+M)×0:G(N)≤IP(M):L(N:G(N)+1):L(A:G(A)×(G(N)÷4)))


Variables:

P = product_final answer

N = counter

M = higher limit


P=:   set the result to the variable P, P = A

INIT:  (L(A:1)+L(N:1)+M)×0; set A = 1, N = 1, ask for M

COND: G(N)≤IP(M); if N ≤ IP(M); if true continue, if false exit the loop

NEXT:  L(N:G(N)+1)

EXPR: L(A:G(A)×(G(N)÷4))


Results:

3 → M; P → 0.0938

6 → M; P → 0.1758

12 → M; P → 28.5507


Example 3:  Recursive


Given an initial condition u_0, calculate the recursion:


u_n = 2 * u_n-1 - 3


for n terms.


Plus42 Equation Code:

S=FOR((U+N+L(I:1))×0:G(I)≤N:L(I:G(I)+1):L(U:2×G(U)-3))


Variables:

S = final answer

U = recursive variable

N = number of terms


S=:  final answer

INIT:  (U+N+L(I:1))×0

COND: G(I)≤N

NEXT:  L(I:G(I)+1)

EXPR:  L(U:2×G(U)-3)


Results, enter U, N first before calculating for S:

2 → U, 3 → N; S → -5

5 → U, 2 → N; S → 11


Enjoy and hope this helps,


Eddie 


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.